Class: Wechat::AccessToken

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAccessToken

Returns a new instance of AccessToken.



5
6
7
8
9
10
11
12
# File 'lib/wechat/access_token.rb', line 5

def initialize
  raise 'Wechat.api_appid not set' if Wechat.api_appid.nil?
  raise 'Wechat.api_appsecret not set' if Wechat.api_appsecret.nil?

  @appid = Wechat.api_appid
  @secret = Wechat.api_appsecret
  
end

Instance Attribute Details

#appidObject

Returns the value of attribute appid.



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

def appid
  @appid
end

#secretObject

Returns the value of attribute secret.



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

def secret
  @secret
end

Instance Method Details

#refreshObject



18
19
20
21
22
# File 'lib/wechat/access_token.rb', line 18

def refresh
  url = "#{Wechat.api_endpoint}/token"
  data = RestClient.get(url, params:{grant_type: "client_credential", appid: @appid, secret: @secret})
  return valid_token(data)
end

#tokenObject



14
15
16
# File 'lib/wechat/access_token.rb', line 14

def token
  Wechat.access_token ||= self.refresh
end