Class: WechatPublic::AccessToken

Inherits:
Object
  • Object
show all
Defined in:
lib/wechat/access_token.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, appid, secret, token_file) ⇒ AccessToken

Returns a new instance of AccessToken.



5
6
7
8
9
10
# 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

Instance Attribute Details

#appidObject (readonly)

Returns the value of attribute appid.



3
4
5
# File 'lib/wechat/access_token.rb', line 3

def appid
  @appid
end

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/wechat/access_token.rb', line 3

def client
  @client
end

#secretObject (readonly)

Returns the value of attribute secret.



3
4
5
# File 'lib/wechat/access_token.rb', line 3

def secret
  @secret
end

#token_dataObject (readonly)

Returns the value of attribute token_data.



3
4
5
# File 'lib/wechat/access_token.rb', line 3

def token_data
  @token_data
end

#token_fileObject (readonly)

Returns the value of attribute token_file.



3
4
5
# File 'lib/wechat/access_token.rb', line 3

def token_file
  @token_file
end

Instance Method Details

#refreshObject



28
29
30
31
32
33
# 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

#tokenObject



12
13
14
15
16
17
18
19
20
21
# 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