Class: TRMNLP::OAuth::TokenBundle

Inherits:
Data
  • Object
show all
Defined in:
lib/trmnlp/oauth/token_bundle.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token

Returns:

  • (Object)

    the current value of access_token



5
6
7
# File 'lib/trmnlp/oauth/token_bundle.rb', line 5

def access_token
  @access_token
end

#expires_atObject (readonly)

Returns the value of attribute expires_at

Returns:

  • (Object)

    the current value of expires_at



5
6
7
# File 'lib/trmnlp/oauth/token_bundle.rb', line 5

def expires_at
  @expires_at
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token

Returns:

  • (Object)

    the current value of refresh_token



5
6
7
# File 'lib/trmnlp/oauth/token_bundle.rb', line 5

def refresh_token
  @refresh_token
end

#token_typeObject (readonly)

Returns the value of attribute token_type

Returns:

  • (Object)

    the current value of token_type



5
6
7
# File 'lib/trmnlp/oauth/token_bundle.rb', line 5

def token_type
  @token_type
end

Class Method Details

.from_h(hash) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/trmnlp/oauth/token_bundle.rb', line 6

def self.from_h(hash)
  new(
    access_token: hash['access_token'],
    refresh_token: hash['refresh_token'],
    expires_at: hash['expires_at'],
    token_type: hash['token_type'] || 'Bearer'
  )
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
# File 'lib/trmnlp/oauth/token_bundle.rb', line 15

def expired?
  return false unless expires_at

  # Treat as expired 5 minutes early so a token never dies mid-request.
  expires_at.to_i - 300 <= Time.now.to_i
end

#merge_refresh(fresh) ⇒ Object

A refresh response often omits the rotated refresh_token (and sometimes other fields); the prior values remain valid, so carry them forward.



24
25
26
27
28
29
30
31
# File 'lib/trmnlp/oauth/token_bundle.rb', line 24

def merge_refresh(fresh)
  with(
    access_token: fresh.access_token,
    refresh_token: fresh.refresh_token || refresh_token,
    expires_at: fresh.expires_at || expires_at,
    token_type: fresh.token_type || token_type
  )
end