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
Returns a new instance of AccessTokenBase.
6 7 8 9 10 11 12 |
# File 'lib/wechat/token/access_token_base.rb', line 6 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.
4 5 6 |
# File 'lib/wechat/token/access_token_base.rb', line 4 def access_token @access_token end |
#appid ⇒ Object (readonly)
Returns the value of attribute appid.
4 5 6 |
# File 'lib/wechat/token/access_token_base.rb', line 4 def appid @appid end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
4 5 6 |
# File 'lib/wechat/token/access_token_base.rb', line 4 def client @client end |
#got_token_at ⇒ Object (readonly)
Returns the value of attribute got_token_at.
4 5 6 |
# File 'lib/wechat/token/access_token_base.rb', line 4 def got_token_at @got_token_at end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
4 5 6 |
# File 'lib/wechat/token/access_token_base.rb', line 4 def secret @secret end |
#token_file ⇒ Object (readonly)
Returns the value of attribute token_file.
4 5 6 |
# File 'lib/wechat/token/access_token_base.rb', line 4 def token_file @token_file end |
#token_life_in_seconds ⇒ Object (readonly)
Returns the value of attribute token_life_in_seconds.
4 5 6 |
# File 'lib/wechat/token/access_token_base.rb', line 4 def token_life_in_seconds @token_life_in_seconds end |
Instance Method Details
#read_token ⇒ Object
40 41 42 |
# File 'lib/wechat/token/access_token_base.rb', line 40 def read_token JSON.parse(File.read(token_file)) end |
#token ⇒ Object
14 15 16 17 18 19 |
# File 'lib/wechat/token/access_token_base.rb', line 14 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
44 45 46 |
# File 'lib/wechat/token/access_token_base.rb', line 44 def write_token(token_hash) File.write(token_file, token_hash.to_json) end |