square.rb 6.3.0.20200826 → 17.1.0.20220120
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +86 -51
- data/lib/square/api/apple_pay_api.rb +12 -9
- data/lib/square/api/bank_accounts_api.rb +21 -24
- data/lib/square/api/base_api.rb +20 -9
- data/lib/square/api/bookings_api.rb +391 -0
- data/lib/square/api/cards_api.rb +170 -0
- data/lib/square/api/cash_drawers_api.rb +13 -6
- data/lib/square/api/catalog_api.rb +195 -85
- data/lib/square/api/checkout_api.rb +7 -5
- data/lib/square/api/customer_groups_api.rb +34 -16
- data/lib/square/api/customer_segments_api.rb +21 -9
- data/lib/square/api/customers_api.rb +102 -55
- data/lib/square/api/devices_api.rb +20 -8
- data/lib/square/api/disputes_api.rb +156 -144
- data/lib/square/api/employees_api.rb +7 -3
- data/lib/square/api/gift_card_activities_api.rb +133 -0
- data/lib/square/api/gift_cards_api.rb +297 -0
- data/lib/square/api/inventory_api.rb +290 -37
- data/lib/square/api/invoices_api.rb +61 -57
- data/lib/square/api/labor_api.rb +127 -93
- data/lib/square/api/locations_api.rb +36 -25
- data/lib/square/api/loyalty_api.rb +134 -87
- data/lib/square/api/merchants_api.rb +8 -4
- data/lib/square/api/mobile_authorization_api.rb +9 -7
- data/lib/square/api/o_auth_api.rb +41 -32
- data/lib/square/api/orders_api.rb +132 -54
- data/lib/square/api/payments_api.rb +133 -75
- data/lib/square/api/refunds_api.rb +51 -30
- data/lib/square/api/sites_api.rb +43 -0
- data/lib/square/api/snippets_api.rb +146 -0
- data/lib/square/api/subscriptions_api.rb +216 -26
- data/lib/square/api/team_api.rb +81 -65
- data/lib/square/api/terminal_api.rb +166 -16
- data/lib/square/api/transactions_api.rb +32 -194
- data/lib/square/api/v1_transactions_api.rb +53 -103
- data/lib/square/api_helper.rb +38 -43
- data/lib/square/client.rb +54 -24
- data/lib/square/configuration.rb +61 -21
- data/lib/square/http/api_response.rb +3 -1
- data/lib/square/http/faraday_client.rb +34 -5
- data/lib/square/utilities/date_time_helper.rb +151 -0
- data/lib/square/utilities/file_wrapper.rb +1 -2
- data/lib/square.rb +65 -61
- data/spec/user_journey_spec.rb +2 -5
- data/test/api/api_test_base.rb +1 -6
- data/test/api/test_catalog_api.rb +1 -4
- data/test/api/test_customers_api.rb +1 -4
- data/test/api/test_employees_api.rb +1 -4
- data/test/api/test_labor_api.rb +2 -6
- data/test/api/test_locations_api.rb +21 -35
- data/test/api/test_merchants_api.rb +1 -4
- data/test/api/test_payments_api.rb +3 -6
- data/test/api/test_refunds_api.rb +3 -6
- data/test/http_response_catcher.rb +0 -5
- data/test/test_helper.rb +1 -6
- metadata +40 -18
- data/lib/square/api/v1_employees_api.rb +0 -723
- data/lib/square/api/v1_items_api.rb +0 -1686
- data/lib/square/api/v1_locations_api.rb +0 -65
data/lib/square/api_helper.rb
CHANGED
@@ -8,13 +8,14 @@ module Square
|
|
8
8
|
def self.serialize_array(key, array, formatting: 'indexed')
|
9
9
|
tuples = []
|
10
10
|
|
11
|
-
|
11
|
+
case formatting
|
12
|
+
when 'unindexed'
|
12
13
|
tuples += array.map { |element| ["#{key}[]", element] }
|
13
|
-
|
14
|
+
when 'indexed'
|
14
15
|
tuples += array.map.with_index do |element, index|
|
15
16
|
["#{key}[#{index}]", element]
|
16
17
|
end
|
17
|
-
|
18
|
+
when 'plain'
|
18
19
|
tuples += array.map { |element| [key, element] }
|
19
20
|
else
|
20
21
|
raise ArgumentError, 'Invalid format provided.'
|
@@ -36,21 +37,26 @@ module Square
|
|
36
37
|
# Return if there are no parameters to replace.
|
37
38
|
return query_builder if parameters.nil?
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
replace_value = ''
|
42
|
-
|
43
|
-
if value.nil?
|
40
|
+
parameters.each do |key, val|
|
41
|
+
if val.nil?
|
44
42
|
replace_value = ''
|
45
|
-
elsif value.instance_of? Array
|
46
|
-
|
47
|
-
|
43
|
+
elsif val['value'].instance_of? Array
|
44
|
+
if val['encode'] == true
|
45
|
+
val['value'].map! { |element| CGI.escape(element.to_s) }
|
46
|
+
else
|
47
|
+
val['value'].map!(&:to_s)
|
48
|
+
end
|
49
|
+
replace_value = val['value'].join('/')
|
48
50
|
else
|
49
|
-
replace_value =
|
51
|
+
replace_value = if val['encode'] == true
|
52
|
+
CGI.escape(val['value'].to_s)
|
53
|
+
else
|
54
|
+
val['value'].to_s
|
55
|
+
end
|
50
56
|
end
|
51
57
|
|
52
58
|
# Find the template parameter and replace it with its value.
|
53
|
-
query_builder = query_builder.gsub(
|
59
|
+
query_builder = query_builder.gsub("{#{key}}", replace_value)
|
54
60
|
end
|
55
61
|
query_builder
|
56
62
|
end
|
@@ -58,9 +64,7 @@ module Square
|
|
58
64
|
# Appends the given set of parameters to the given query string.
|
59
65
|
# @param [String] The query string builder to add the query parameters to.
|
60
66
|
# @param [Hash] The parameters to append.
|
61
|
-
|
62
|
-
def self.append_url_with_query_parameters(query_builder, parameters,
|
63
|
-
array_serialization: 'indexed')
|
67
|
+
def self.append_url_with_query_parameters(query_builder, parameters)
|
64
68
|
# Perform parameter validation.
|
65
69
|
unless query_builder.instance_of? String
|
66
70
|
raise ArgumentError, 'Given value for parameter \"query_builder\"
|
@@ -70,20 +74,23 @@ module Square
|
|
70
74
|
# Return if there are no parameters to replace.
|
71
75
|
return query_builder if parameters.nil?
|
72
76
|
|
77
|
+
array_serialization = 'indexed'
|
78
|
+
|
73
79
|
parameters.each do |key, value|
|
74
80
|
seperator = query_builder.include?('?') ? '&' : '?'
|
75
81
|
unless value.nil?
|
76
82
|
if value.instance_of? Array
|
77
83
|
value.compact!
|
78
|
-
query_builder +=
|
84
|
+
query_builder += case array_serialization
|
85
|
+
when 'csv'
|
79
86
|
"#{seperator}#{key}=#{value.map do |element|
|
80
87
|
CGI.escape(element.to_s)
|
81
88
|
end.join(',')}"
|
82
|
-
|
89
|
+
when 'psv'
|
83
90
|
"#{seperator}#{key}=#{value.map do |element|
|
84
91
|
CGI.escape(element.to_s)
|
85
92
|
end.join('|')}"
|
86
|
-
|
93
|
+
when 'tsv'
|
87
94
|
"#{seperator}#{key}=#{value.map do |element|
|
88
95
|
CGI.escape(element.to_s)
|
89
96
|
end.join("\t")}"
|
@@ -109,7 +116,7 @@ module Square
|
|
109
116
|
raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
|
110
117
|
|
111
118
|
# Ensure that the urls are absolute.
|
112
|
-
matches = url.match(%r{^(https
|
119
|
+
matches = url.match(%r{^(https?://[^/]+)})
|
113
120
|
raise ArgumentError, 'Invalid Url format.' if matches.nil?
|
114
121
|
|
115
122
|
# Get the http protocol match.
|
@@ -120,7 +127,7 @@ module Square
|
|
120
127
|
|
121
128
|
# Remove redundant forward slashes.
|
122
129
|
query = url[protocol.length...(!index.nil? ? index : url.length)]
|
123
|
-
query.gsub!(%r{
|
130
|
+
query.gsub!(%r{//+}, '/')
|
124
131
|
|
125
132
|
# Get the parameters.
|
126
133
|
parameters = !index.nil? ? url[url.index('?')...url.length] : ''
|
@@ -132,7 +139,7 @@ module Square
|
|
132
139
|
# Parses JSON string.
|
133
140
|
# @param [String] A JSON string.
|
134
141
|
def self.json_deserialize(json)
|
135
|
-
|
142
|
+
JSON.parse(json, symbolize_names: true)
|
136
143
|
rescue StandardError
|
137
144
|
raise TypeError, 'Server responded with invalid JSON.'
|
138
145
|
end
|
@@ -146,8 +153,8 @@ module Square
|
|
146
153
|
# Form encodes a hash of parameters.
|
147
154
|
# @param [Hash] The hash of parameters to encode.
|
148
155
|
# @return [Hash] A hash with the same parameters form encoded.
|
149
|
-
def self.form_encode_parameters(form_parameters
|
150
|
-
|
156
|
+
def self.form_encode_parameters(form_parameters)
|
157
|
+
array_serialization = 'indexed'
|
151
158
|
encoded = {}
|
152
159
|
form_parameters.each do |key, value|
|
153
160
|
encoded.merge!(APIHelper.form_encode(value, key, formatting:
|
@@ -161,6 +168,7 @@ module Square
|
|
161
168
|
a.each do |key, value_a|
|
162
169
|
b.each do |k, value_b|
|
163
170
|
next unless key == k
|
171
|
+
|
164
172
|
x[k] = []
|
165
173
|
if value_a.instance_of? Array
|
166
174
|
value_a.each do |v|
|
@@ -202,13 +210,12 @@ module Square
|
|
202
210
|
elsif obj.instance_of? Array
|
203
211
|
if formatting == 'indexed'
|
204
212
|
obj.each_with_index do |value, index|
|
205
|
-
retval.merge!(APIHelper.form_encode(value, instance_name
|
206
|
-
index.to_s + ']'))
|
213
|
+
retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{index}]"))
|
207
214
|
end
|
208
215
|
elsif serializable_types.map { |x| obj[0].is_a? x }.any?
|
209
216
|
obj.each do |value|
|
210
217
|
abc = if formatting == 'unindexed'
|
211
|
-
APIHelper.form_encode(value, instance_name
|
218
|
+
APIHelper.form_encode(value, "#{instance_name}[]",
|
212
219
|
formatting: formatting)
|
213
220
|
else
|
214
221
|
APIHelper.form_encode(value, instance_name,
|
@@ -218,14 +225,14 @@ module Square
|
|
218
225
|
end
|
219
226
|
else
|
220
227
|
obj.each_with_index do |value, index|
|
221
|
-
retval.merge!(APIHelper.form_encode(value, instance_name
|
222
|
-
|
228
|
+
retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{index}]",
|
229
|
+
formatting: formatting))
|
223
230
|
end
|
224
231
|
end
|
225
232
|
elsif obj.instance_of? Hash
|
226
233
|
obj.each do |key, value|
|
227
|
-
retval.merge!(APIHelper.form_encode(value, instance_name
|
228
|
-
|
234
|
+
retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{key}]",
|
235
|
+
formatting: formatting))
|
229
236
|
end
|
230
237
|
elsif obj.instance_of? File
|
231
238
|
retval[instance_name] = UploadIO.new(
|
@@ -260,17 +267,5 @@ module Square
|
|
260
267
|
end
|
261
268
|
val
|
262
269
|
end
|
263
|
-
|
264
|
-
# Safely converts a string into an rfc3339 DateTime object
|
265
|
-
# @param [String] The datetime string
|
266
|
-
# @return [DateTime] A DateTime object of rfc3339 format
|
267
|
-
def self.rfc3339(date_time)
|
268
|
-
# missing timezone information
|
269
|
-
if date_time.end_with?('Z') || date_time.index('+')
|
270
|
-
DateTime.rfc3339(date_time)
|
271
|
-
else
|
272
|
-
DateTime.rfc3339(date_time + 'Z')
|
273
|
-
end
|
274
|
-
end
|
275
270
|
end
|
276
271
|
end
|
data/lib/square/client.rb
CHANGED
@@ -4,13 +4,17 @@ module Square
|
|
4
4
|
attr_reader :config
|
5
5
|
|
6
6
|
def sdk_version
|
7
|
-
'
|
7
|
+
'17.1.0.20220120'
|
8
8
|
end
|
9
9
|
|
10
10
|
def square_version
|
11
11
|
config.square_version
|
12
12
|
end
|
13
13
|
|
14
|
+
def user_agent_detail
|
15
|
+
config.user_agent_detail
|
16
|
+
end
|
17
|
+
|
14
18
|
# Access to mobile_authorization controller.
|
15
19
|
# @return [MobileAuthorizationApi] Returns the controller instance.
|
16
20
|
def mobile_authorization
|
@@ -23,30 +27,12 @@ module Square
|
|
23
27
|
@o_auth ||= OAuthApi.new config
|
24
28
|
end
|
25
29
|
|
26
|
-
# Access to v1_locations controller.
|
27
|
-
# @return [V1LocationsApi] Returns the controller instance.
|
28
|
-
def v1_locations
|
29
|
-
@v1_locations ||= V1LocationsApi.new config
|
30
|
-
end
|
31
|
-
|
32
|
-
# Access to v1_employees controller.
|
33
|
-
# @return [V1EmployeesApi] Returns the controller instance.
|
34
|
-
def v1_employees
|
35
|
-
@v1_employees ||= V1EmployeesApi.new config
|
36
|
-
end
|
37
|
-
|
38
30
|
# Access to v1_transactions controller.
|
39
31
|
# @return [V1TransactionsApi] Returns the controller instance.
|
40
32
|
def v1_transactions
|
41
33
|
@v1_transactions ||= V1TransactionsApi.new config
|
42
34
|
end
|
43
35
|
|
44
|
-
# Access to v1_items controller.
|
45
|
-
# @return [V1ItemsApi] Returns the controller instance.
|
46
|
-
def v1_items
|
47
|
-
@v1_items ||= V1ItemsApi.new config
|
48
|
-
end
|
49
|
-
|
50
36
|
# Access to apple_pay controller.
|
51
37
|
# @return [ApplePayApi] Returns the controller instance.
|
52
38
|
def apple_pay
|
@@ -59,6 +45,18 @@ module Square
|
|
59
45
|
@bank_accounts ||= BankAccountsApi.new config
|
60
46
|
end
|
61
47
|
|
48
|
+
# Access to bookings controller.
|
49
|
+
# @return [BookingsApi] Returns the controller instance.
|
50
|
+
def bookings
|
51
|
+
@bookings ||= BookingsApi.new config
|
52
|
+
end
|
53
|
+
|
54
|
+
# Access to cards controller.
|
55
|
+
# @return [CardsApi] Returns the controller instance.
|
56
|
+
def cards
|
57
|
+
@cards ||= CardsApi.new config
|
58
|
+
end
|
59
|
+
|
62
60
|
# Access to cash_drawers controller.
|
63
61
|
# @return [CashDrawersApi] Returns the controller instance.
|
64
62
|
def cash_drawers
|
@@ -107,6 +105,18 @@ module Square
|
|
107
105
|
@employees ||= EmployeesApi.new config
|
108
106
|
end
|
109
107
|
|
108
|
+
# Access to gift_cards controller.
|
109
|
+
# @return [GiftCardsApi] Returns the controller instance.
|
110
|
+
def gift_cards
|
111
|
+
@gift_cards ||= GiftCardsApi.new config
|
112
|
+
end
|
113
|
+
|
114
|
+
# Access to gift_card_activities controller.
|
115
|
+
# @return [GiftCardActivitiesApi] Returns the controller instance.
|
116
|
+
def gift_card_activities
|
117
|
+
@gift_card_activities ||= GiftCardActivitiesApi.new config
|
118
|
+
end
|
119
|
+
|
110
120
|
# Access to inventory controller.
|
111
121
|
# @return [InventoryApi] Returns the controller instance.
|
112
122
|
def inventory
|
@@ -173,6 +183,18 @@ module Square
|
|
173
183
|
@refunds ||= RefundsApi.new config
|
174
184
|
end
|
175
185
|
|
186
|
+
# Access to sites controller.
|
187
|
+
# @return [SitesApi] Returns the controller instance.
|
188
|
+
def sites
|
189
|
+
@sites ||= SitesApi.new config
|
190
|
+
end
|
191
|
+
|
192
|
+
# Access to snippets controller.
|
193
|
+
# @return [SnippetsApi] Returns the controller instance.
|
194
|
+
def snippets
|
195
|
+
@snippets ||= SnippetsApi.new config
|
196
|
+
end
|
197
|
+
|
176
198
|
# Access to subscriptions controller.
|
177
199
|
# @return [SubscriptionsApi] Returns the controller instance.
|
178
200
|
def subscriptions
|
@@ -191,17 +213,25 @@ module Square
|
|
191
213
|
@terminal ||= TerminalApi.new config
|
192
214
|
end
|
193
215
|
|
194
|
-
def initialize(timeout: 60, max_retries: 0,
|
195
|
-
|
196
|
-
|
197
|
-
|
216
|
+
def initialize(http_client_instance: nil, timeout: 60, max_retries: 0,
|
217
|
+
retry_interval: 1, backoff_factor: 2,
|
218
|
+
retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
|
219
|
+
retry_methods: %i[get put], environment: 'production',
|
220
|
+
custom_url: 'https://connect.squareup.com',
|
221
|
+
square_version: '2022-01-20', access_token: '',
|
222
|
+
user_agent_detail: '', additional_headers: {}, config: nil)
|
198
223
|
@config = if config.nil?
|
199
|
-
Configuration.new(
|
224
|
+
Configuration.new(http_client_instance: http_client_instance,
|
225
|
+
timeout: timeout, max_retries: max_retries,
|
200
226
|
retry_interval: retry_interval,
|
201
227
|
backoff_factor: backoff_factor,
|
228
|
+
retry_statuses: retry_statuses,
|
229
|
+
retry_methods: retry_methods,
|
202
230
|
environment: environment,
|
231
|
+
custom_url: custom_url,
|
203
232
|
square_version: square_version,
|
204
233
|
access_token: access_token,
|
234
|
+
user_agent_detail: user_agent_detail,
|
205
235
|
additional_headers: additional_headers)
|
206
236
|
else
|
207
237
|
config
|
data/lib/square/configuration.rb
CHANGED
@@ -3,14 +3,9 @@ module Square
|
|
3
3
|
# are configured in this class.
|
4
4
|
class Configuration
|
5
5
|
# The attribute readers for properties.
|
6
|
-
attr_reader :http_client
|
7
|
-
|
8
|
-
|
9
|
-
attr_reader :retry_interval
|
10
|
-
attr_reader :backoff_factor
|
11
|
-
attr_reader :environment
|
12
|
-
attr_reader :square_version
|
13
|
-
attr_reader :access_token
|
6
|
+
attr_reader :http_client, :http_client_instance, :timeout, :max_retries, :retry_interval,
|
7
|
+
:backoff_factor, :retry_statuses, :retry_methods, :environment, :custom_url,
|
8
|
+
:square_version, :access_token, :user_agent_detail
|
14
9
|
|
15
10
|
def additional_headers
|
16
11
|
@additional_headers.clone
|
@@ -20,10 +15,16 @@ module Square
|
|
20
15
|
attr_reader :environments
|
21
16
|
end
|
22
17
|
|
23
|
-
def initialize(timeout: 60, max_retries: 0,
|
24
|
-
|
25
|
-
|
26
|
-
|
18
|
+
def initialize(http_client_instance: nil, timeout: 60, max_retries: 0,
|
19
|
+
retry_interval: 1, backoff_factor: 2,
|
20
|
+
retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
|
21
|
+
retry_methods: %i[get put], environment: 'production',
|
22
|
+
custom_url: 'https://connect.squareup.com',
|
23
|
+
square_version: '2022-01-20', access_token: '',
|
24
|
+
user_agent_detail: '', additional_headers: {})
|
25
|
+
# The Http Client passed from the sdk user for making requests
|
26
|
+
@http_client_instance = http_client_instance
|
27
|
+
|
27
28
|
# The value to use for connection timeout
|
28
29
|
@timeout = timeout
|
29
30
|
|
@@ -37,13 +38,22 @@ module Square
|
|
37
38
|
# by in order to provide backoff
|
38
39
|
@backoff_factor = backoff_factor
|
39
40
|
|
41
|
+
# A list of HTTP statuses to retry
|
42
|
+
@retry_statuses = retry_statuses
|
43
|
+
|
44
|
+
# A list of HTTP methods to retry
|
45
|
+
@retry_methods = retry_methods
|
46
|
+
|
40
47
|
# Current API environment
|
41
48
|
@environment = String(environment)
|
42
49
|
|
50
|
+
# Sets the base URL requests are made to. Defaults to `https://connect.squareup.com`
|
51
|
+
@custom_url = custom_url
|
52
|
+
|
43
53
|
# Square Connect API versions
|
44
54
|
@square_version = square_version
|
45
55
|
|
46
|
-
# OAuth 2.0 Access Token
|
56
|
+
# The OAuth 2.0 Access Token to use for API requests.
|
47
57
|
@access_token = access_token
|
48
58
|
|
49
59
|
# Additional headers to add to each API request
|
@@ -51,33 +61,55 @@ module Square
|
|
51
61
|
|
52
62
|
# The Http Client to use for making requests.
|
53
63
|
@http_client = create_http_client
|
64
|
+
|
65
|
+
# User agent detail, to be appended with user-agent header.
|
66
|
+
@user_agent_detail = get_user_agent(user_agent_detail)
|
54
67
|
end
|
55
68
|
|
56
|
-
def clone_with(
|
57
|
-
|
58
|
-
|
69
|
+
def clone_with(http_client_instance: nil, timeout: nil, max_retries: nil,
|
70
|
+
retry_interval: nil, backoff_factor: nil,
|
71
|
+
retry_statuses: nil, retry_methods: nil, environment: nil,
|
72
|
+
custom_url: nil, square_version: nil, access_token: nil,
|
73
|
+
user_agent_detail: nil, additional_headers: nil)
|
74
|
+
http_client_instance ||= self.http_client_instance
|
59
75
|
timeout ||= self.timeout
|
60
76
|
max_retries ||= self.max_retries
|
61
77
|
retry_interval ||= self.retry_interval
|
62
78
|
backoff_factor ||= self.backoff_factor
|
79
|
+
retry_statuses ||= self.retry_statuses
|
80
|
+
retry_methods ||= self.retry_methods
|
63
81
|
environment ||= self.environment
|
82
|
+
custom_url ||= self.custom_url
|
64
83
|
square_version ||= self.square_version
|
65
84
|
access_token ||= self.access_token
|
85
|
+
user_agent_detail ||= self.user_agent_detail
|
66
86
|
additional_headers ||= self.additional_headers
|
67
87
|
|
68
|
-
Configuration.new(
|
88
|
+
Configuration.new(http_client_instance: http_client_instance,
|
89
|
+
timeout: timeout, max_retries: max_retries,
|
69
90
|
retry_interval: retry_interval,
|
70
91
|
backoff_factor: backoff_factor,
|
71
|
-
|
72
|
-
|
92
|
+
retry_statuses: retry_statuses,
|
93
|
+
retry_methods: retry_methods, environment: environment,
|
94
|
+
custom_url: custom_url, square_version: square_version,
|
73
95
|
access_token: access_token,
|
96
|
+
user_agent_detail: user_agent_detail,
|
74
97
|
additional_headers: additional_headers)
|
75
98
|
end
|
76
99
|
|
77
100
|
def create_http_client
|
78
101
|
FaradayClient.new(timeout: timeout, max_retries: max_retries,
|
79
102
|
retry_interval: retry_interval,
|
80
|
-
backoff_factor: backoff_factor
|
103
|
+
backoff_factor: backoff_factor,
|
104
|
+
retry_statuses: retry_statuses,
|
105
|
+
retry_methods: retry_methods,
|
106
|
+
http_client_instance: http_client_instance)
|
107
|
+
end
|
108
|
+
|
109
|
+
def get_user_agent(user_agent_detail)
|
110
|
+
raise ArgumentError, 'The length of user-agent detail should not exceed 128 characters.' unless user_agent_detail.length < 128
|
111
|
+
|
112
|
+
user_agent_detail
|
81
113
|
end
|
82
114
|
|
83
115
|
# All the environments the SDK can run in.
|
@@ -87,6 +119,9 @@ module Square
|
|
87
119
|
},
|
88
120
|
'sandbox' => {
|
89
121
|
'default' => 'https://connect.squareupsandbox.com'
|
122
|
+
},
|
123
|
+
'custom' => {
|
124
|
+
'default' => '{custom_url}'
|
90
125
|
}
|
91
126
|
}.freeze
|
92
127
|
|
@@ -95,7 +130,12 @@ module Square
|
|
95
130
|
# required.
|
96
131
|
# @return [String] The base URI.
|
97
132
|
def get_base_uri(server = 'default')
|
98
|
-
|
133
|
+
parameters = {
|
134
|
+
'custom_url' => { 'value' => custom_url, 'encode' => false }
|
135
|
+
}
|
136
|
+
APIHelper.append_url_with_template_parameters(
|
137
|
+
ENVIRONMENTS[environment][server], parameters
|
138
|
+
)
|
99
139
|
end
|
100
140
|
end
|
101
141
|
end
|
@@ -25,7 +25,7 @@ module Square
|
|
25
25
|
end.new(*data.values)
|
26
26
|
|
27
27
|
@cursor = data.fetch(:cursor, nil)
|
28
|
-
data.reject! { |k|
|
28
|
+
data.reject! { |k| %i[cursor errors].include?(k) }
|
29
29
|
@data = Struct.new(*data.keys).new(*data.values) if data.keys.any?
|
30
30
|
end
|
31
31
|
else
|
@@ -34,10 +34,12 @@ module Square
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
+
# returns true if status_code is between 200-300
|
37
38
|
def success?
|
38
39
|
status_code >= 200 && status_code < 300
|
39
40
|
end
|
40
41
|
|
42
|
+
# returns true if status_code is between 400-600
|
41
43
|
def error?
|
42
44
|
status_code >= 400 && status_code < 600
|
43
45
|
end
|
@@ -6,20 +6,43 @@ module Square
|
|
6
6
|
class FaradayClient < HttpClient
|
7
7
|
# The constructor.
|
8
8
|
def initialize(timeout:, max_retries:, retry_interval:,
|
9
|
-
backoff_factor:,
|
9
|
+
backoff_factor:, retry_statuses:, retry_methods:,
|
10
|
+
http_client_instance: nil, cache: false, verify: true)
|
11
|
+
if http_client_instance.nil?
|
12
|
+
create_connection(timeout: timeout, max_retries: max_retries,
|
13
|
+
retry_interval: retry_interval, backoff_factor: backoff_factor,
|
14
|
+
retry_statuses: retry_statuses, retry_methods: retry_methods,
|
15
|
+
cache: cache, verify: verify)
|
16
|
+
else
|
17
|
+
if http_client_instance.instance_variable_get('@connection').nil?
|
18
|
+
raise ArgumentError,
|
19
|
+
"`connection` cannot be nil in `#{self.class}`. Please specify a valid value."
|
20
|
+
end
|
21
|
+
@connection = http_client_instance.instance_variable_get('@connection')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Method to initialize connection.
|
26
|
+
def create_connection(timeout:, max_retries:, retry_interval:,
|
27
|
+
backoff_factor:, retry_statuses:, retry_methods:,
|
28
|
+
cache: false, verify: true)
|
10
29
|
@connection = Faraday.new do |faraday|
|
11
30
|
faraday.use Faraday::HttpCache, serializer: Marshal if cache
|
12
31
|
faraday.use FaradayMiddleware::FollowRedirects
|
32
|
+
faraday.use :gzip
|
13
33
|
faraday.request :multipart
|
14
34
|
faraday.request :url_encoded
|
15
35
|
faraday.ssl[:ca_file] = Certifi.where
|
16
36
|
faraday.ssl[:verify] = verify
|
17
37
|
faraday.request :retry, max: max_retries, interval: retry_interval,
|
18
|
-
backoff_factor: backoff_factor
|
38
|
+
backoff_factor: backoff_factor,
|
39
|
+
retry_statuses: retry_statuses,
|
40
|
+
methods: retry_methods
|
19
41
|
faraday.adapter Faraday.default_adapter
|
20
42
|
faraday.options[:params_encoder] = Faraday::FlatParamsEncoder
|
21
|
-
faraday.options[:timeout] = timeout if timeout
|
43
|
+
faraday.options[:timeout] = timeout if timeout.positive?
|
22
44
|
end
|
45
|
+
@connection
|
23
46
|
end
|
24
47
|
|
25
48
|
# Method overridden from HttpClient.
|
@@ -29,7 +52,10 @@ module Square
|
|
29
52
|
http_request.query_url
|
30
53
|
) do |request|
|
31
54
|
request.headers = http_request.headers
|
32
|
-
|
55
|
+
unless http_request.http_method == HttpMethodEnum::GET &&
|
56
|
+
http_request.parameters.empty?
|
57
|
+
request.body = http_request.parameters
|
58
|
+
end
|
33
59
|
end
|
34
60
|
convert_response(response, http_request)
|
35
61
|
end
|
@@ -41,7 +67,10 @@ module Square
|
|
41
67
|
http_request.query_url
|
42
68
|
) do |request|
|
43
69
|
request.headers = http_request.headers
|
44
|
-
|
70
|
+
unless http_request.http_method == HttpMethodEnum::GET &&
|
71
|
+
http_request.parameters.empty?
|
72
|
+
request.body = http_request.parameters
|
73
|
+
end
|
45
74
|
end
|
46
75
|
convert_response(response, http_request)
|
47
76
|
end
|