class MotionWechat::AccessToken

Attributes

client[R]
expires_in[R]
openid[R]
options[RW]
params[R]
refresh_token[RW]
token[R]

Public Class Methods

from_hash(client, hash) click to toggle source

Initializes an AccessToken from a Hash

Example:

MotionWechat::AccessToken.from_hash @client, {}

Arguments:

client: (MotionWechat::Client)
hash: (Hash)
# File lib/motion-wechat/access_token.rb, line 17
def from_hash(client, hash)
  new(client, hash.delete('access_token') || hash.delete(:access_token), hash)
end
new(client, token, opts = {}) click to toggle source

Initalize an AccessToken

Example:

MotionWechat::AccessToken.new @client, 'token', {}

Arguments:

client: (MotionWechat::Client)
token: (String)
hash: (Hash)
# File lib/motion-wechat/access_token.rb, line 33
def initialize(client, token, opts = {})
  @client = client
  @token = token.to_s
  [:refresh_token, :expires_in, :openid].each do |arg|
    instance_variable_set("@#{arg}", opts.delete(arg) || opts.delete(arg.to_s))
  end
  @expires_in ||= opts.delete('expires')
  @expires_at ||= Time.now.to_i + @expires_in.to_i
  @params = opts
end

Public Instance Methods

get(path, opts = {}, &block) click to toggle source

Get information using access token

Example:

@token.get "/sns/userinfo", { |res| ... }

Arguments:

path: (String)
opts: (hash)
# File lib/motion-wechat/access_token.rb, line 53
def get(path, opts = {}, &block)
  @client.get path, opts.merge({access_token: @token}), &block
end
get_user_info(&block) click to toggle source

Get user information

Example:

@token.get_user_info { |info| ... }
# File lib/motion-wechat/access_token.rb, line 62
def get_user_info(&block)
  get "/sns/userinfo", openid: @openid do |info|
    block.call oauth2_wrapp(info)
  end
end

Private Instance Methods

oauth2_wrapp(info) click to toggle source

Wrap with OAuth2 structure

# File lib/motion-wechat/access_token.rb, line 71
def oauth2_wrapp(info)
  {
    uid: @openid,
    provider: "weixin",
    info: {
      image: info['headimgurl'],
      nickname: info['nickname'],
      name: info['nickname']
    },
    extra: info
  }
end