Class: Lifen::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/lifen/token.rb

Instance Method Summary collapse

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/lifen/token.rb', line 24

def active?
  valid? and !needs_to_be_refreshed?
end

#has_expired?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
# File 'lib/lifen/token.rb', line 38

def has_expired?
  return true if expires_at.nil?

  return expires_at < Time.now.to_i
end

#needs_to_be_refreshed?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/lifen/token.rb', line 32

def needs_to_be_refreshed?
  return true if has_expired?

  return (expires_at - expiration_margin) < Time.now.to_i
end

#refreshObject



44
45
46
47
48
49
# File 'lib/lifen/token.rb', line 44

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_neededObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/lifen/token.rb', line 51

def refresh_once_if_needed
  Lifen.configuration.token_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_sObject



20
21
22
# File 'lib/lifen/token.rb', line 20

def to_s
  value
end

#valid?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/lifen/token.rb', line 28

def valid?
  !value.nil? and value.length > 0 and !expires_at.nil?
end