Class: Square::Configuration

Inherits:
CoreLibrary::HttpClientConfiguration
  • Object
show all
Defined in:
lib/square/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.

{
  'production' => {
    'default' => 'https://connect.squareup.com'
  },
  'sandbox' => {
    'default' => 'https://connect.squareupsandbox.com'
  },
  'custom' => {
    'default' => '{custom_url}'
  }
}.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, environment: 'production', custom_url: 'https://connect.squareup.com', access_token: nil, bearer_auth_credentials: nil, square_version: '2024-11-20', user_agent_detail: '', additional_headers: {}) ⇒ Configuration

Returns a new instance of Configuration.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
# File 'lib/square/configuration.rb', line 21

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, environment: 'production',
  custom_url: 'https://connect.squareup.com', access_token: nil,
  bearer_auth_credentials: nil, square_version: '2024-11-20',
  user_agent_detail: '', additional_headers: {}
)

  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

  # Current API environment
  @environment = String(environment)

  # Sets the base URL requests are made to. Defaults to `https://connect.squareup.com`
  @custom_url = custom_url

  # The OAuth 2.0 Access Token to use for API requests.
  @access_token = access_token

  # Square Connect API versions
  @square_version = square_version

  # Additional headers to add to each API request
  @additional_headers = additional_headers.clone

  # Initializing OAuth 2 Bearer token credentials with the provided auth parameters
  @bearer_auth_credentials = create_auth_credentials_object(
    access_token, bearer_auth_credentials
  )

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

  # User agent detail, to be appended with user-agent header.
  @user_agent_detail = get_user_agent(user_agent_detail)
end

Class Attribute Details

.environmentsObject (readonly)

Returns the value of attribute environments.



18
19
20
# File 'lib/square/configuration.rb', line 18

def environments
  @environments
end

Instance Attribute Details

#bearer_auth_credentialsObject (readonly)

The attribute readers for properties.



10
11
12
# File 'lib/square/configuration.rb', line 10

def bearer_auth_credentials
  @bearer_auth_credentials
end

#custom_urlObject (readonly)

The attribute readers for properties.



10
11
12
# File 'lib/square/configuration.rb', line 10

def custom_url
  @custom_url
end

#environmentObject (readonly)

The attribute readers for properties.



10
11
12
# File 'lib/square/configuration.rb', line 10

def environment
  @environment
end

#square_versionObject (readonly)

The attribute readers for properties.



10
11
12
# File 'lib/square/configuration.rb', line 10

def square_version
  @square_version
end

#user_agent_detailObject (readonly)

The attribute readers for properties.



10
11
12
# File 'lib/square/configuration.rb', line 10

def user_agent_detail
  @user_agent_detail
end

Instance Method Details

#access_tokenObject



5
6
7
# File 'lib/square/configuration.rb', line 5

def access_token
  @bearer_auth_credentials.access_token
end

#additional_headersObject



13
14
15
# File 'lib/square/configuration.rb', line 13

def additional_headers
  @additional_headers.clone
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, environment: nil, custom_url: nil, access_token: nil, bearer_auth_credentials: nil, square_version: nil, user_agent_detail: nil, additional_headers: nil) ⇒ Object



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
93
94
95
96
97
98
# File 'lib/square/configuration.rb', line 63

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,
               environment: nil, custom_url: nil, access_token: nil,
               bearer_auth_credentials: nil, square_version: nil,
               user_agent_detail: nil, additional_headers: 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
  environment ||= self.environment
  custom_url ||= self.custom_url
  square_version ||= self.square_version
  user_agent_detail ||= self.user_agent_detail
  additional_headers ||= self.additional_headers
  bearer_auth_credentials = create_auth_credentials_object(
    access_token, bearer_auth_credentials || self.bearer_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, environment: environment,
                    custom_url: custom_url, square_version: square_version,
                    user_agent_detail: user_agent_detail,
                    additional_headers: additional_headers,
                    bearer_auth_credentials: bearer_auth_credentials)
end

#create_auth_credentials_object(access_token, bearer_auth_credentials) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/square/configuration.rb', line 107

def create_auth_credentials_object(access_token, bearer_auth_credentials)
  return bearer_auth_credentials if access_token.nil?

  warn('The \'access_token\' params are deprecated. Use \'bearer_auth_cred'\
       'entials\' param instead.')

  unless bearer_auth_credentials.nil?
    return bearer_auth_credentials.clone_with(
      access_token: access_token
    )
  end

  BearerAuthCredentials.new(access_token: access_token)
end

#get_base_uri(server = 'default') ⇒ String

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

Parameters:

  • server (Configuration::Server) (defaults to: 'default')

    The server enum for which the base URI is

Returns:

  • (String)

    The base URI.



139
140
141
142
143
144
145
146
# File 'lib/square/configuration.rb', line 139

def get_base_uri(server = 'default')
  parameters = {
    'custom_url' => { 'value' => custom_url, 'encode' => false }
  }
  APIHelper.append_url_with_template_parameters(
    ENVIRONMENTS[environment][server], parameters
  )
end

#get_user_agent(user_agent_detail) ⇒ Object

Raises:

  • (ArgumentError)


100
101
102
103
104
105
# File 'lib/square/configuration.rb', line 100

def get_user_agent(user_agent_detail)
  raise ArgumentError, 'The length of user-agent detail should not exceed 128 characters.' unless
    user_agent_detail.length < 128

  user_agent_detail
end