Class: StrixRuby::TokenManager

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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
   = nil
end

Instance Attribute Details

#token_account_idObject (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 
  
end

Instance Method Details

#access_tokenString

Get a valid access token, refreshing if necessary

Returns:

  • (String)

    the access token



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
   = nil
end

#refresh_tokenString

Force a token refresh

Returns:

  • (String)

    the new access token



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

Returns:

  • (Boolean)

    true if token needs refresh



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