Class: Lifen::Token
- Inherits:
-
Object
- Object
- Lifen::Token
- Defined in:
- lib/lifen/token.rb
Instance Method Summary collapse
- #active? ⇒ Boolean
- #has_expired? ⇒ Boolean
- #needs_to_be_refreshed? ⇒ Boolean
- #refresh ⇒ Object
- #refresh_once_if_needed ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
Instance Method Details
#active? ⇒ Boolean
14 15 16 |
# File 'lib/lifen/token.rb', line 14 def active? valid? and !needs_to_be_refreshed? end |
#has_expired? ⇒ Boolean
28 29 30 31 32 |
# File 'lib/lifen/token.rb', line 28 def has_expired? return true if expires_at.nil? return expires_at < Time.now.to_i end |
#needs_to_be_refreshed? ⇒ Boolean
22 23 24 25 26 |
# File 'lib/lifen/token.rb', line 22 def needs_to_be_refreshed? return true if has_expired? return (expires_at - expiration_margin) < Time.now.to_i end |
#refresh ⇒ Object
34 35 36 37 38 39 |
# File 'lib/lifen/token.rb', line 34 def refresh json = client.post("/oauth/admin/third_party/access_token?accountUuid=#{user.uuid}") self.value = json["access_token"] self.expires_at = Time.now.to_i + json["expires_in"].to_i end |
#refresh_once_if_needed ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/lifen/token.rb', line 41 def refresh_once_if_needed return if active? refresh raise Error, "Token can't be refreshed" if !active? end |
#to_s ⇒ Object
10 11 12 |
# File 'lib/lifen/token.rb', line 10 def to_s value end |
#valid? ⇒ Boolean
18 19 20 |
# File 'lib/lifen/token.rb', line 18 def valid? !value.nil? and value.length > 0 and !expires_at.nil? end |