Class: WordsApi::Configuration

Inherits:
CoreLibrary::HttpClientConfiguration
  • Object
show all
Defined in:
lib/words_api/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 => 'https://wordsapiv1.p.rapidapi.com'
  }
}.freeze

Class Attribute Summary collapse

Instance Attribute 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, logging_configuration: nil, environment: Environment::PRODUCTION, x_rapid_api_key: nil, custom_header_authentication_credentials: nil) ⇒ Configuration

Returns a new instance of Configuration.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/words_api/configuration.rb', line 35

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,
  logging_configuration: nil, environment: Environment::PRODUCTION,
  x_rapid_api_key: nil, custom_header_authentication_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,
        logging_configuration: logging_configuration

  # Current API environment
  @environment = String(environment)

  # This is an API key from RapidAPI.
  @x_rapid_api_key = x_rapid_api_key

  # Initializing Custom Header Signature credentials with the provided auth parameters
  @custom_header_authentication_credentials = create_auth_credentials_object(
    x_rapid_api_key, custom_header_authentication_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.



32
33
34
# File 'lib/words_api/configuration.rb', line 32

def environments
  @environments
end

Instance Attribute Details

#custom_header_authentication_credentialsObject (readonly)

The attribute readers for properties.



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

def custom_header_authentication_credentials
  @custom_header_authentication_credentials
end

#environmentObject (readonly)

The attribute readers for properties.



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

def environment
  @environment
end

Instance Method Details

#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, logging_configuration: nil, environment: nil, x_rapid_api_key: nil, custom_header_authentication_credentials: nil) ⇒ Object



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
93
94
95
# File 'lib/words_api/configuration.rb', line 65

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,
               logging_configuration: nil, environment: nil,
               x_rapid_api_key: nil,
               custom_header_authentication_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
  logging_configuration ||= self.logging_configuration
  environment ||= self.environment
  custom_header_authentication_credentials = create_auth_credentials_object(
    x_rapid_api_key,
    custom_header_authentication_credentials || self.custom_header_authentication_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,
    logging_configuration: logging_configuration, environment: environment,
    custom_header_authentication_credentials: custom_header_authentication_credentials
  )
end

#create_auth_credentials_object(x_rapid_api_key, custom_header_authentication_credentials) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/words_api/configuration.rb', line 97

def create_auth_credentials_object(x_rapid_api_key,
                                   custom_header_authentication_credentials)
  return custom_header_authentication_credentials if x_rapid_api_key.nil?

  warn('The \'x_rapid_api_key\' params are deprecated. Use \'custom_header'\
       '_authentication_credentials\' param instead.')

  unless custom_header_authentication_credentials.nil?
    return custom_header_authentication_credentials.clone_with(
      x_rapid_api_key: x_rapid_api_key
    )
  end

  CustomHeaderAuthenticationCredentials.new(
    x_rapid_api_key: x_rapid_api_key
  )
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.



126
127
128
# File 'lib/words_api/configuration.rb', line 126

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

#x_rapid_api_keyObject



24
25
26
# File 'lib/words_api/configuration.rb', line 24

def x_rapid_api_key
  @custom_header_authentication_credentials.x_rapid_api_key
end