Class: ModernTreasury::Configuration

Inherits:
CoreLibrary::HttpClientConfiguration
  • Object
show all
Defined in:
lib/modern_treasury/configuration.rb

Overview

All configuration including auth info and base URI for the API access are configured in this class.

Constant Summary collapse

ENVIRONMENTS =

All the environments the SDK can run in.

{
  Environment::PRODUCTION => {
    Server::DEFAULT => 'http://localhost:3000'
  },
  Environment::ENVIRONMENT2 => {
    Server::DEFAULT => 'https://app.moderntreasury.com'
  }
}.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection: nil, adapter: :net_http_persistent, timeout: 60, max_retries: 0, retry_interval: 1, backoff_factor: 2, retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524], retry_methods: %i[get put],, http_callback: nil, proxy_settings: nil, environment: Environment::PRODUCTION, basic_auth_user_name: nil, basic_auth_password: nil, basic_auth_credentials: nil) ⇒ Configuration

Returns a new instance of Configuration.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/modern_treasury/configuration.rb', line 62

def initialize(
  connection: nil, adapter: :net_http_persistent, timeout: 60,
  max_retries: 0, retry_interval: 1, backoff_factor: 2,
  retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
  retry_methods: %i[get put], http_callback: nil, proxy_settings: nil,
  environment: Environment::PRODUCTION, basic_auth_user_name: nil,
  basic_auth_password: nil, basic_auth_credentials: nil
)
  super connection: connection, adapter: adapter, timeout: timeout,
        max_retries: max_retries, retry_interval: retry_interval,
        backoff_factor: backoff_factor, retry_statuses: retry_statuses,
        retry_methods: retry_methods, http_callback: http_callback,
        proxy_settings: proxy_settings

  # Current API environment
  @environment = String(environment)

  # The username to use with basic authentication
  @basic_auth_user_name = basic_auth_user_name

  # The password to use with basic authentication
  @basic_auth_password = basic_auth_password

  # Initializing Basic Authentication credentials with the provided auth parameters
  @basic_auth_credentials = create_auth_credentials_object(
    basic_auth_user_name, basic_auth_password, basic_auth_credentials
  )

  # The Http Client to use for making requests.
  set_http_client CoreLibrary::FaradayClient.new(self)
end

Class Attribute Details

.environmentsObject (readonly)

Returns the value of attribute environments.



59
60
61
# File 'lib/modern_treasury/configuration.rb', line 59

def environments
  @environments
end

Instance Attribute Details

#basic_auth_credentialsObject (readonly)

The attribute readers for properties.



56
57
58
# File 'lib/modern_treasury/configuration.rb', line 56

def basic_auth_credentials
  @basic_auth_credentials
end

#environmentObject (readonly)

The attribute readers for properties.



56
57
58
# File 'lib/modern_treasury/configuration.rb', line 56

def environment
  @environment
end

Class Method Details

.build_default_config_from_envObject

Builds a Configuration instance using environment variables.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/modern_treasury/configuration.rb', line 166

def self.build_default_config_from_env
  # === Core environment ===
  environment = Environment.from_value(ENV.fetch('ENVIRONMENT', 'production'))
  timeout = (ENV['TIMEOUT'] || 60).to_f
  max_retries = (ENV['MAX_RETRIES'] || 0).to_i
  retry_interval = (ENV['RETRY_INTERVAL'] || 1).to_f
  backoff_factor = (ENV['BACKOFF_FACTOR'] || 2).to_f
  retry_statuses = ENV.fetch('RETRY_STATUSES',
                             '[408, 413, 429, 500, 502, 503, 504, 521, 522, 524]').gsub(/[\[\]]/, '')
                                      .split(',')
                                      .map(&:strip)
                                      .map do |item|
                                        item.match?(/\A\d+\z/) ? item.to_i : item.downcase
                                      end
  retry_methods = ENV.fetch('RETRY_METHODS', '%i[get put]').gsub(/[\[\]]/, '')
                                      .split(',')
                                      .map(&:strip)
                                      .map do |item|
                                        item.match?(/\A\d+\z/) ? item.to_i : item.downcase
                                      end

  # === Authentication credentials ===
  basic_auth_credentials = BasicAuthCredentials.from_env

  # === Proxy settings ===
  proxy_settings = ProxySettings.from_env

  Configuration.new(
    environment: environment,
    timeout: timeout,
    max_retries: max_retries,
    retry_interval: retry_interval,
    backoff_factor: backoff_factor,
    retry_statuses: retry_statuses,
    retry_methods: retry_methods,
    basic_auth_credentials: basic_auth_credentials,
    proxy_settings: proxy_settings
  )
end

Instance Method Details

#basic_auth_passwordObject



51
52
53
# File 'lib/modern_treasury/configuration.rb', line 51

def basic_auth_password
  @basic_auth_credentials.password
end

#basic_auth_user_nameObject



47
48
49
# File 'lib/modern_treasury/configuration.rb', line 47

def basic_auth_user_name
  @basic_auth_credentials.username
end

#clone_with(connection: nil, adapter: nil, timeout: nil, max_retries: nil, retry_interval: nil, backoff_factor: nil, retry_statuses: nil, retry_methods: nil, http_callback: nil, proxy_settings: nil, environment: nil, basic_auth_user_name: nil, basic_auth_password: nil, basic_auth_credentials: nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/modern_treasury/configuration.rb', line 94

def clone_with(connection: nil, adapter: nil, timeout: nil,
               max_retries: nil, retry_interval: nil, backoff_factor: nil,
               retry_statuses: nil, retry_methods: nil, http_callback: nil,
               proxy_settings: nil, environment: nil,
               basic_auth_user_name: nil, basic_auth_password: nil,
               basic_auth_credentials: nil)
  connection ||= self.connection
  adapter ||= self.adapter
  timeout ||= self.timeout
  max_retries ||= self.max_retries
  retry_interval ||= self.retry_interval
  backoff_factor ||= self.backoff_factor
  retry_statuses ||= self.retry_statuses
  retry_methods ||= self.retry_methods
  http_callback ||= self.http_callback
  proxy_settings ||= self.proxy_settings
  environment ||= self.environment
  basic_auth_credentials = create_auth_credentials_object(
    basic_auth_user_name, basic_auth_password,
    basic_auth_credentials || self.basic_auth_credentials
  )

  Configuration.new(connection: connection, adapter: adapter,
                    timeout: timeout, max_retries: max_retries,
                    retry_interval: retry_interval,
                    backoff_factor: backoff_factor,
                    retry_statuses: retry_statuses,
                    retry_methods: retry_methods,
                    http_callback: http_callback,
                    proxy_settings: proxy_settings,
                    environment: environment,
                    basic_auth_credentials: basic_auth_credentials)
end

#create_auth_credentials_object(basic_auth_user_name, basic_auth_password, basic_auth_credentials) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/modern_treasury/configuration.rb', line 128

def create_auth_credentials_object(basic_auth_user_name,
                                   basic_auth_password,
                                   basic_auth_credentials)
  return basic_auth_credentials if basic_auth_user_name.nil? && basic_auth_password.nil?

  warn('The \'basic_auth_user_name\', \'basic_auth_password\' params are d'\
       'eprecated. Use \'basic_auth_credentials\' param instead.')

  unless basic_auth_credentials.nil?
    return basic_auth_credentials.clone_with(
      username: basic_auth_user_name,
      password: basic_auth_password
    )
  end

  BasicAuthCredentials.new(username: basic_auth_user_name,
                           password: basic_auth_password)
end

#get_base_uri(server = Server::DEFAULT) ⇒ String

Generates the appropriate base URI for the environment and the server. required.

Parameters:

  • server (Configuration::Server) (defaults to: Server::DEFAULT)

    The server enum for which the base URI is

Returns:

  • (String)

    The base URI.



161
162
163
# File 'lib/modern_treasury/configuration.rb', line 161

def get_base_uri(server = Server::DEFAULT)
  ENVIRONMENTS[environment][server].clone
end