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.
-
#record ⇒ Object
readonly
Returns the value of attribute record.
-
#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, record = nil) ⇒ 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, record = nil) ⇒ AccessTokenBase
Returns a new instance of AccessTokenBase.
8 9 10 11 12 13 14 15 |
# File 'lib/wechat/token/access_token_base.rb', line 8 def initialize(client, appid, secret, token_file, record = nil) @appid = appid @secret = secret @client = client @token_file = token_file @record = record @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 |
#record ⇒ Object (readonly)
Returns the value of attribute record.
6 7 8 |
# File 'lib/wechat/token/access_token_base.rb', line 6 def record @record 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
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/wechat/token/access_token_base.rb', line 43 def read_token if record_based_token? throw_error_if_missing_attributes! { 'access_token' => record.access_token, 'got_token_at' => record.got_token_at, 'token_expires_in' => record.token_expires_in } else JSON.parse(File.read(token_file)) end end |
#token ⇒ Object
17 18 19 20 21 22 |
# File 'lib/wechat/token/access_token_base.rb', line 17 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
57 58 59 60 61 62 63 |
# File 'lib/wechat/token/access_token_base.rb', line 57 def write_token(token_hash) if record_based_token? write_token_to_record(token_hash) else File.write(token_file, token_hash.to_json) end end |