Class: Cpro::Auth::Token
- Inherits:
-
Object
- Object
- Cpro::Auth::Token
- Defined in:
- lib/cpro/auth/token.rb
Constant Summary collapse
- EXPIRY_MARGIN =
Marge de renouvellement anticipé, pour absorber la latence réseau et la dérive d'horloge.
60- DEFAULT_LIFETIME =
3600
Instance Attribute Summary collapse
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #expired?(now: Time.now) ⇒ Boolean
-
#initialize(value:, expires_at:) ⇒ Token
constructor
A new instance of Token.
- #inspect ⇒ Object (also: #to_s)
Constructor Details
#initialize(value:, expires_at:) ⇒ Token
Returns a new instance of Token.
21 22 23 24 |
# File 'lib/cpro/auth/token.rb', line 21 def initialize(value:, expires_at:) @value = value @expires_at = expires_at end |
Instance Attribute Details
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
10 11 12 |
# File 'lib/cpro/auth/token.rb', line 10 def expires_at @expires_at end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
10 11 12 |
# File 'lib/cpro/auth/token.rb', line 10 def value @value end |
Class Method Details
.from_response(body, now: Time.now) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/cpro/auth/token.rb', line 12 def self.from_response(body, now: Time.now) payload = body.is_a?(Hash) ? body : {} value = payload["access_token"] raise AuthenticationError.new("réponse PISTE sans access_token", body: body) if value.nil? || value.empty? lifetime = (payload["expires_in"] || DEFAULT_LIFETIME).to_i new(value: value, expires_at: now + lifetime) end |
Instance Method Details
#expired?(now: Time.now) ⇒ Boolean
26 27 28 |
# File 'lib/cpro/auth/token.rb', line 26 def expired?(now: Time.now) now >= (expires_at - EXPIRY_MARGIN) end |
#inspect ⇒ Object Also known as: to_s
30 31 32 |
# File 'lib/cpro/auth/token.rb', line 30 def inspect "#<#{self.class.name} expires_at=#{expires_at.inspect}>" end |