class WechatPublic::AccessToken

Attributes

appid[R]
client[R]
secret[R]
token_data[R]
token_file[R]

Public Class Methods

new(client, appid, secret, token_file) click to toggle source
# File lib/wechat/access_token.rb, line 5
def initialize(client, appid, secret, token_file)
  @appid = appid
  @secret = secret
  @client = client
  @token_file = token_file
end

Public Instance Methods

refresh() click to toggle source
# File lib/wechat/access_token.rb, line 28
def refresh
  data = client.get("token", params:{grant_type: "client_credential", appid: appid, secret: secret})
  #File.open(token_file, 'w'){|f| f.write(data.to_s)} if valid_token(data)
  Object.send(token_file.fetch(:set_token),data) if valid_token(data)
  return @token_data = data
end
token() click to toggle source
# File lib/wechat/access_token.rb, line 12
def token
  begin
    #@token_data ||= JSON.parse(File.read(token_file))
    data = Object.send(token_file.fetch(:get_token))
    @token_data ||= data;
  rescue
    self.refresh
  end
  return valid_token(@token_data)
end

Private Instance Methods

valid_token(token_data) click to toggle source
# File lib/wechat/access_token.rb, line 36
def valid_token token_data

  access_token = token_data["access_token"]
  raise "Response didn't have access_token" if  access_token.blank?
  return access_token
end