Class: Wechat::Token::AccessTokenBase
- Inherits:
-
Object
- Object
- Wechat::Token::AccessTokenBase
- Defined in:
- lib/wechat/token/access_token_base.rb,
lib/generators/wechat/templates/config/initializers/wechat_redis_store.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#appid ⇒ Object
readonly
Returns the value of attribute appid.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#got_token_at ⇒ Object
readonly
Returns the value of attribute got_token_at.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
-
#token_file ⇒ Object
readonly
Returns the value of attribute token_file.
-
#token_life_in_seconds ⇒ Object
readonly
Returns the value of attribute token_life_in_seconds.
Instance Method Summary collapse
-
#initialize(client, appid, secret, token_file) ⇒ AccessTokenBase
constructor
A new instance of AccessTokenBase.
- #read_token ⇒ Object
- #token ⇒ Object
- #write_token(token_hash) ⇒ Object
Constructor Details
#initialize(client, appid, secret, token_file) ⇒ AccessTokenBase
8 9 10 11 12 13 14 |
# File 'lib/wechat/token/access_token_base.rb', line 8 def initialize(client, appid, secret, token_file) @appid = appid @secret = secret @client = client @token_file = token_file @random_generator = Random.new end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
6 7 8 |
# File 'lib/wechat/token/access_token_base.rb', line 6 def access_token @access_token end |
#appid ⇒ Object (readonly)
Returns the value of attribute appid.
6 7 8 |
# File 'lib/wechat/token/access_token_base.rb', line 6 def appid @appid end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
6 7 8 |
# File 'lib/wechat/token/access_token_base.rb', line 6 def client @client end |
#got_token_at ⇒ Object (readonly)
Returns the value of attribute got_token_at.
6 7 8 |
# File 'lib/wechat/token/access_token_base.rb', line 6 def got_token_at @got_token_at end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
6 7 8 |
# File 'lib/wechat/token/access_token_base.rb', line 6 def secret @secret end |
#token_file ⇒ Object (readonly)
Returns the value of attribute token_file.
6 7 8 |
# File 'lib/wechat/token/access_token_base.rb', line 6 def token_file @token_file end |
#token_life_in_seconds ⇒ Object (readonly)
Returns the value of attribute token_life_in_seconds.
6 7 8 |
# File 'lib/wechat/token/access_token_base.rb', line 6 def token_life_in_seconds @token_life_in_seconds end |
Instance Method Details
#read_token ⇒ Object
42 43 44 |
# File 'lib/wechat/token/access_token_base.rb', line 42 def read_token JSON.parse(File.read(token_file)) end |
#token ⇒ Object
16 17 18 19 20 21 |
# File 'lib/wechat/token/access_token_base.rb', line 16 def token # Possible two worker running, one worker refresh token, other unaware, so must read every time read_token_from_store refresh if remain_life_seconds < @random_generator.rand(30..(3 * 60)) access_token end |
#write_token(token_hash) ⇒ Object
46 47 48 |
# File 'lib/wechat/token/access_token_base.rb', line 46 def write_token(token_hash) File.write(token_file, token_hash.to_json) end |