Class: XiWechatCorp::API::AccessToken
- Inherits:
-
Object
- Object
- XiWechatCorp::API::AccessToken
- Defined in:
- lib/xi_wechat_corp/api/access_token.rb
Constant Summary collapse
- EXPIRE_SECONDS =
Leave 5 minutes buffer
7200 - 300
Instance Attribute Summary collapse
-
#expired_at ⇒ Object
readonly
Returns the value of attribute expired_at.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #assign(token, expired_at = nil) ⇒ Object (also: #refresh)
- #expired? ⇒ Boolean
-
#initialize(token = nil, expired_at = nil) ⇒ AccessToken
constructor
A new instance of AccessToken.
- #present? ⇒ Boolean
- #to_s ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(token = nil, expired_at = nil) ⇒ AccessToken
Returns a new instance of AccessToken.
10 11 12 |
# File 'lib/xi_wechat_corp/api/access_token.rb', line 10 def initialize(token = nil, expired_at = nil) assign(token, expired_at) end |
Instance Attribute Details
#expired_at ⇒ Object (readonly)
Returns the value of attribute expired_at.
8 9 10 |
# File 'lib/xi_wechat_corp/api/access_token.rb', line 8 def expired_at @expired_at end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
7 8 9 |
# File 'lib/xi_wechat_corp/api/access_token.rb', line 7 def token @token end |
Instance Method Details
#assign(token, expired_at = nil) ⇒ Object Also known as: refresh
14 15 16 17 18 19 20 21 |
# File 'lib/xi_wechat_corp/api/access_token.rb', line 14 def assign(token, expired_at = nil) @token = token.to_s if !@token.empty? @expired_at = expired_at || (token.respond_to?(:expired_at) ? token.expired_at : Time.now.to_i + EXPIRE_SECONDS) else @expired_at = 0 end end |
#expired? ⇒ Boolean
29 30 31 |
# File 'lib/xi_wechat_corp/api/access_token.rb', line 29 def expired? @token.empty? || @expired_at < Time.now.to_i end |
#present? ⇒ Boolean
25 26 27 |
# File 'lib/xi_wechat_corp/api/access_token.rb', line 25 def present? @token.size > 0 end |
#to_s ⇒ Object
37 38 39 |
# File 'lib/xi_wechat_corp/api/access_token.rb', line 37 def to_s @token end |
#valid? ⇒ Boolean
33 34 35 |
# File 'lib/xi_wechat_corp/api/access_token.rb', line 33 def valid? !expired? end |