Class: Lifen::Token
- Inherits:
-
Object
- Object
- Lifen::Token
- Defined in:
- lib/lifen/token.rb
Constant Summary collapse
- @@refresh_lock =
Mutex.new
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
20 21 22 |
# File 'lib/lifen/token.rb', line 20 def active? valid? and !needs_to_be_refreshed? end |
#has_expired? ⇒ Boolean
34 35 36 37 38 |
# File 'lib/lifen/token.rb', line 34 def has_expired? return true if expires_at.nil? return expires_at < Time.now.to_i end |
#needs_to_be_refreshed? ⇒ Boolean
28 29 30 31 32 |
# File 'lib/lifen/token.rb', line 28 def needs_to_be_refreshed? return true if has_expired? return (expires_at - expiration_margin) < Time.now.to_i end |
#refresh ⇒ Object
40 41 42 43 44 45 |
# File 'lib/lifen/token.rb', line 40 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
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/lifen/token.rb', line 47 def refresh_once_if_needed @@refresh_lock.synchronize do load_from_db.call(self) if load_from_db.is_a? Proc return if active? refresh save_to_db.call(self) if save_to_db.is_a? Proc raise Error, "Token can't be refreshed" if !active? end end |
#to_s ⇒ Object
16 17 18 |
# File 'lib/lifen/token.rb', line 16 def to_s value end |
#valid? ⇒ Boolean
24 25 26 |
# File 'lib/lifen/token.rb', line 24 def valid? !value.nil? and value.length > 0 and !expires_at.nil? end |