Class: LsaTdxFeedback::OAuthClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/lsa_tdx_feedback/oauth_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(configuration = LsaTdxFeedback.configuration) ⇒ OAuthClient

Returns a new instance of OAuthClient.



8
9
10
11
12
13
14
15
16
17
# File 'lib/lsa_tdx_feedback/oauth_client.rb', line 8

def initialize(configuration = LsaTdxFeedback.configuration)
  @configuration = configuration
  @configuration.validate!

  self.class.base_uri(@configuration.oauth_url)
  self.class.headers({
    'Content-Type' => 'application/x-www-form-urlencoded',
    'Accept' => 'application/json'
  })
end

Instance Method Details

#get_access_tokenObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lsa_tdx_feedback/oauth_client.rb', line 19

def get_access_token
  # Check cache first
  cached_token = Rails.cache.read(cache_key)
  return cached_token if cached_token

  # Get new token
  token_response = fetch_new_token

  if token_response.success?
    token_data = token_response.parsed_response
    access_token = token_data['access_token']
    expires_in = token_data['expires_in'].to_i

    # Cache the token with some buffer time (subtract 5 minutes)
    cache_duration = [expires_in - 300, 300].max
    Rails.cache.write(cache_key, access_token, expires_in: cache_duration)

    access_token
  else
    handle_error_response(token_response)
  end
end