Class: StrixRuby::TokenManager
- Inherits:
-
Object
- Object
- StrixRuby::TokenManager
- Defined in:
- lib/strix_ruby/token_manager.rb
Overview
Manages OAuth token lifecycle: obtains, caches, and refreshes tokens
Constant Summary collapse
- EXPIRATION_BUFFER =
Buffer time (in seconds) before actual expiration to refresh token
60
Instance Attribute Summary collapse
-
#token_account_id ⇒ Object
readonly
The account_id extracted from the token’s meta.business_account_id.
Instance Method Summary collapse
-
#access_token ⇒ String
Get a valid access token, refreshing if necessary.
-
#clear! ⇒ Object
Clear the cached token.
-
#initialize(connection:, username:, password:) ⇒ TokenManager
constructor
A new instance of TokenManager.
-
#refresh_token ⇒ String
Force a token refresh.
-
#token_expired? ⇒ Boolean
Check if the current token is expired or not present.
Constructor Details
#initialize(connection:, username:, password:) ⇒ TokenManager
Returns a new instance of TokenManager.
15 16 17 18 19 20 21 22 |
# File 'lib/strix_ruby/token_manager.rb', line 15 def initialize(connection:, username:, password:) @connection = connection @username = username @password = password @current_token = nil @expires_at = nil @token_account_id = nil end |
Instance Attribute Details
#token_account_id ⇒ Object (readonly)
The account_id extracted from the token’s meta.business_account_id
13 14 15 |
# File 'lib/strix_ruby/token_manager.rb', line 13 def token_account_id @token_account_id end |
Instance Method Details
#access_token ⇒ String
Get a valid access token, refreshing if necessary
26 27 28 29 |
# File 'lib/strix_ruby/token_manager.rb', line 26 def access_token refresh_token if token_expired? @current_token end |
#clear! ⇒ Object
Clear the cached token
49 50 51 52 53 |
# File 'lib/strix_ruby/token_manager.rb', line 49 def clear! @current_token = nil @expires_at = nil @token_account_id = nil end |
#refresh_token ⇒ String
Force a token refresh
33 34 35 36 37 38 |
# File 'lib/strix_ruby/token_manager.rb', line 33 def refresh_token response = request_new_token @current_token = response["access_token"] extract_token_data(@current_token) @current_token end |
#token_expired? ⇒ Boolean
Check if the current token is expired or not present
42 43 44 45 46 |
# File 'lib/strix_ruby/token_manager.rb', line 42 def token_expired? return true if @current_token.nil? || @expires_at.nil? Time.now.to_i >= (@expires_at - EXPIRATION_BUFFER) end |