Class: DirectDeposit::Configuration

Inherits:
Common::Client::Configuration::REST show all
Defined in:
lib/lighthouse/direct_deposit/configuration.rb

Overview

HTTP client configuration for the Service, sets the base path, the base request headers, and a service name for breakers and metrics.

Constant Summary collapse

API_SCOPES =
%w[direct.deposit.read direct.deposit.write].freeze
DIRECT_DEPOSIT_PATH =
'services/direct-deposit-management/v1/direct-deposit'
TOKEN_PATH =
'oauth2/direct-deposit-management/system/v1/token'

Instance Attribute Summary

Attributes inherited from Common::Client::Configuration::Base

#base_request_headers, #open_timeout, #read_timeout, #request_types, #user_agent

Instance Method Summary collapse

Methods inherited from Common::Client::Configuration::Base

#breakers_error_threshold, #breakers_exception_handler, #breakers_matcher, #breakers_service, #create_new_breakers_service, #current_module, #request_options, #service_exception

Instance Method Details

#access_tokenObject



73
74
75
# File 'lib/lighthouse/direct_deposit/configuration.rb', line 73

def access_token
  token_service.get_token if get_access_token?
end

#base_pathString

Returns Base path for direct_deposit URLs.

Returns:

  • (String)

    Base path for direct_deposit URLs.



29
30
31
# File 'lib/lighthouse/direct_deposit/configuration.rb', line 29

def base_path
  "#{settings.host}/#{DIRECT_DEPOSIT_PATH}"
end

#connectionFaraday::Connection

Creates a Faraday connection with parsing json and breakers functionality.

Returns:

  • (Faraday::Connection)

    a Faraday connection instance.



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lighthouse/direct_deposit/configuration.rb', line 59

def connection
  @conn ||= Faraday.new(base_path, headers: base_request_headers, request: request_options) do |faraday|
    faraday.use :breakers
    faraday.use Faraday::Response::RaiseError
    faraday.request :json

    faraday.response :betamocks if use_mocks?
    faraday.response :snakecase, symbolize: false
    faraday.response :json, content_type: /\bjson/

    faraday.adapter Faraday.default_adapter
  end
end

#get(path, params = {}) ⇒ Faraday::Response

Returns response from GET request.

Returns:

  • (Faraday::Response)

    response from GET request



43
44
45
# File 'lib/lighthouse/direct_deposit/configuration.rb', line 43

def get(path, params = {})
  connection.get(path, params, { Authorization: "Bearer #{access_token}" })
end

#get_access_token?Boolean (private)

Returns:

  • (Boolean)


86
87
88
# File 'lib/lighthouse/direct_deposit/configuration.rb', line 86

def get_access_token?
  !use_mocks? || Settings.betamocks.recording
end

#put(path, body = {}) ⇒ Faraday::Response

Returns response from POST request.

Returns:

  • (Faraday::Response)

    response from POST request



50
51
52
# File 'lib/lighthouse/direct_deposit/configuration.rb', line 50

def put(path, body = {})
  connection.put(path, body, { Authorization: "Bearer #{access_token}" })
end

#service_nameString

Returns Service name to use in breakers and metrics.

Returns:

  • (String)

    Service name to use in breakers and metrics.



36
37
38
# File 'lib/lighthouse/direct_deposit/configuration.rb', line 36

def service_name
  'LIGHTHOUSE_DIRECT_DEPOSIT'
end

#settingsConfig::Options

Returns Settings for direct_deposit API.

Returns:

  • (Config::Options)

    Settings for direct_deposit API.



22
23
24
# File 'lib/lighthouse/direct_deposit/configuration.rb', line 22

def settings
  Settings.lighthouse.direct_deposit
end

#token_serviceAuth::ClientCredentials::Service (private)

Returns Service used to generate access tokens.

Returns:



93
94
95
96
97
98
99
100
# File 'lib/lighthouse/direct_deposit/configuration.rb', line 93

def token_service
  url = "#{settings.host}/#{TOKEN_PATH}"
  token = settings.access_token

  @token_service ||= Auth::ClientCredentials::Service.new(
    url, API_SCOPES, token.client_id, token.aud_claim_url, token.rsa_key, 'direct-deposit'
  )
end

#use_mocks?Boolean (private)

Returns Should the service use mock data in lower environments.

Returns:

  • (Boolean)

    Should the service use mock data in lower environments.



82
83
84
# File 'lib/lighthouse/direct_deposit/configuration.rb', line 82

def use_mocks?
  settings.use_mocks || false
end