Class: EdFi::Client::AccessToken
- Inherits:
-
Object
- Object
- EdFi::Client::AccessToken
- Defined in:
- lib/ed_fi/client/access_token.rb
Overview
The EdFi::Client::AccessToken represents an access token, as returned by “/oauth/token” calls.
Instance Method Summary collapse
-
#expires_at ⇒ Time
Gives the token's calculated expiration Time.
-
#initialize(access_token:, token_type:, issued_at: Time.current, expires_in:) ⇒ AccessToken
constructor
An EdFi::Client::AccessToken can be initiialized with the “/oauth/token” response Hash.
-
#token ⇒ String
Gives a copy of the token value.
-
#valid? ⇒ true, false
Denotes whether the token is still “valid”, per its (calculated) expiration timesstamp.
Constructor Details
#initialize(access_token:, token_type:, issued_at: Time.current, expires_in:) ⇒ AccessToken
An EdFi::Client::AccessToken can be initiialized with the “/oauth/token” response Hash. If given, an additional “issued_at” Time value helps to more accurately calculate the token's expiration Time.
27 28 29 30 31 32 |
# File 'lib/ed_fi/client/access_token.rb', line 27 def initialize(access_token:, token_type:, issued_at: Time.current, expires_in:) @access_token = access_token.dup @token_type = token_type.dup @issued_at = issued_at.dup @expires_in = expires_in.dup end |
Instance Method Details
#expires_at ⇒ Time
Gives the token's calculated expiration Time.
48 49 50 51 |
# File 'lib/ed_fi/client/access_token.rb', line 48 def expires_at return 1.second.ago if @access_token.blank? (@issued_at + @expires_in.seconds) end |
#token ⇒ String
Gives a copy of the token value.
39 40 41 |
# File 'lib/ed_fi/client/access_token.rb', line 39 def token @access_token.dup end |
#valid? ⇒ true, false
Denotes whether the token is still “valid”, per its (calculated) expiration timesstamp. Note that a 5-second window is allotted for the request using the token to complete.
59 60 61 62 |
# File 'lib/ed_fi/client/access_token.rb', line 59 def valid? safety_window = 5.seconds Time.current <= (expires_at - safety_window) end |