Class: Wechat::AccessToken
- Inherits:
-
Object
- Object
- Wechat::AccessToken
- Defined in:
- lib/wechat/access_token.rb
Instance Attribute Summary collapse
-
#appid ⇒ Object
readonly
Returns the value of attribute appid.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
-
#token_data ⇒ Object
readonly
Returns the value of attribute token_data.
-
#token_file ⇒ Object
readonly
Returns the value of attribute token_file.
Instance Method Summary collapse
-
#initialize(client, appid, secret, token_file) ⇒ AccessToken
constructor
A new instance of AccessToken.
- #refresh ⇒ Object
- #token ⇒ Object
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
#appid ⇒ Object (readonly)
Returns the value of attribute appid.
3 4 5 |
# File 'lib/wechat/access_token.rb', line 3 def appid @appid end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
3 4 5 |
# File 'lib/wechat/access_token.rb', line 3 def client @client end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
3 4 5 |
# File 'lib/wechat/access_token.rb', line 3 def secret @secret end |
#token_data ⇒ Object (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_file ⇒ Object (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
#refresh ⇒ Object
21 22 23 24 25 |
# File 'lib/wechat/access_token.rb', line 21 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) return @token_data = data end |
#token ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/wechat/access_token.rb', line 12 def token begin @token_data ||= JSON.parse(File.read(token_file)) rescue self.refresh end return valid_token(@token_data) end |