square.rb 8.1.0.20210121 → 18.1.0.20220316
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +59 -213
- data/lib/square/api/apple_pay_api.rb +9 -8
- data/lib/square/api/bank_accounts_api.rb +5 -5
- data/lib/square/api/base_api.rb +20 -9
- data/lib/square/api/bookings_api.rb +95 -12
- data/lib/square/api/cards_api.rb +170 -0
- data/lib/square/api/cash_drawers_api.rb +2 -2
- data/lib/square/api/catalog_api.rb +140 -66
- data/lib/square/api/checkout_api.rb +3 -3
- data/lib/square/api/customer_groups_api.rb +17 -8
- data/lib/square/api/customer_segments_api.rb +15 -6
- data/lib/square/api/customers_api.rb +61 -31
- data/lib/square/api/devices_api.rb +3 -2
- data/lib/square/api/disputes_api.rb +101 -92
- 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 +263 -24
- data/lib/square/api/invoices_api.rb +19 -19
- data/lib/square/api/labor_api.rb +70 -68
- data/lib/square/api/locations_api.rb +22 -14
- data/lib/square/api/loyalty_api.rb +86 -32
- data/lib/square/api/merchants_api.rb +11 -9
- data/lib/square/api/mobile_authorization_api.rb +4 -4
- data/lib/square/api/o_auth_api.rb +31 -25
- data/lib/square/api/orders_api.rb +78 -39
- data/lib/square/api/payments_api.rb +71 -23
- data/lib/square/api/refunds_api.rb +18 -7
- 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 +190 -12
- data/lib/square/api/team_api.rb +46 -46
- data/lib/square/api/terminal_api.rb +19 -18
- data/lib/square/api/transactions_api.rb +15 -191
- data/lib/square/api/v1_transactions_api.rb +13 -85
- data/lib/square/api/vendors_api.rb +257 -0
- data/lib/square/api_helper.rb +45 -57
- data/lib/square/client.rb +54 -18
- data/lib/square/configuration.rb +59 -20
- data/lib/square/http/api_response.rb +1 -1
- data/lib/square/http/faraday_client.rb +24 -4
- data/lib/square/utilities/date_time_helper.rb +151 -0
- data/lib/square/utilities/file_wrapper.rb +1 -2
- data/lib/square.rb +49 -44
- data/test/api/test_locations_api.rb +2 -5
- data/test/test_helper.rb +1 -1
- metadata +11 -6
- data/lib/square/api/v1_employees_api.rb +0 -749
- data/lib/square/api/v1_items_api.rb +0 -1766
data/lib/square/api_helper.rb
CHANGED
@@ -8,17 +8,16 @@ module Square
|
|
8
8
|
def self.serialize_array(key, array, formatting: 'indexed')
|
9
9
|
tuples = []
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
11
|
+
tuples += case formatting
|
12
|
+
when 'csv'
|
13
|
+
[[key, array.map { |element| CGI.escape(element.to_s) }.join(',')]]
|
14
|
+
when 'psv'
|
15
|
+
[[key, array.map { |element| CGI.escape(element.to_s) }.join('|')]]
|
16
|
+
when 'tsv'
|
17
|
+
[[key, array.map { |element| CGI.escape(element.to_s) }.join("\t")]]
|
18
|
+
else
|
19
|
+
array.map { |element| [key, element] }
|
20
|
+
end
|
22
21
|
tuples
|
23
22
|
end
|
24
23
|
|
@@ -55,7 +54,7 @@ module Square
|
|
55
54
|
end
|
56
55
|
|
57
56
|
# Find the template parameter and replace it with its value.
|
58
|
-
query_builder = query_builder.gsub(
|
57
|
+
query_builder = query_builder.gsub("{#{key}}", replace_value)
|
59
58
|
end
|
60
59
|
query_builder
|
61
60
|
end
|
@@ -63,9 +62,7 @@ module Square
|
|
63
62
|
# Appends the given set of parameters to the given query string.
|
64
63
|
# @param [String] The query string builder to add the query parameters to.
|
65
64
|
# @param [Hash] The parameters to append.
|
66
|
-
|
67
|
-
def self.append_url_with_query_parameters(query_builder, parameters,
|
68
|
-
array_serialization: 'indexed')
|
65
|
+
def self.append_url_with_query_parameters(query_builder, parameters)
|
69
66
|
# Perform parameter validation.
|
70
67
|
unless query_builder.instance_of? String
|
71
68
|
raise ArgumentError, 'Given value for parameter \"query_builder\"
|
@@ -75,29 +72,20 @@ module Square
|
|
75
72
|
# Return if there are no parameters to replace.
|
76
73
|
return query_builder if parameters.nil?
|
77
74
|
|
75
|
+
array_serialization = 'indexed'
|
76
|
+
parameters = process_complex_types_parameters(parameters, array_serialization)
|
77
|
+
|
78
78
|
parameters.each do |key, value|
|
79
79
|
seperator = query_builder.include?('?') ? '&' : '?'
|
80
80
|
unless value.nil?
|
81
81
|
if value.instance_of? Array
|
82
82
|
value.compact!
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
CGI.escape(element.to_s)
|
90
|
-
end.join('|')}"
|
91
|
-
elsif array_serialization == 'tsv'
|
92
|
-
"#{seperator}#{key}=#{value.map do |element|
|
93
|
-
CGI.escape(element.to_s)
|
94
|
-
end.join("\t")}"
|
95
|
-
else
|
96
|
-
"#{seperator}#{APIHelper.serialize_array(
|
97
|
-
key, value, formatting: array_serialization
|
98
|
-
).map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }
|
99
|
-
.join('&')}"
|
100
|
-
end
|
83
|
+
APIHelper.serialize_array(
|
84
|
+
key, value, formatting: array_serialization
|
85
|
+
).each do |element|
|
86
|
+
seperator = query_builder.include?('?') ? '&' : '?'
|
87
|
+
query_builder += "#{seperator}#{element[0]}=#{element[1]}"
|
88
|
+
end
|
101
89
|
else
|
102
90
|
query_builder += "#{seperator}#{key}=#{CGI.escape(value.to_s)}"
|
103
91
|
end
|
@@ -114,7 +102,7 @@ module Square
|
|
114
102
|
raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
|
115
103
|
|
116
104
|
# Ensure that the urls are absolute.
|
117
|
-
matches = url.match(%r{^(https
|
105
|
+
matches = url.match(%r{^(https?://[^/]+)})
|
118
106
|
raise ArgumentError, 'Invalid Url format.' if matches.nil?
|
119
107
|
|
120
108
|
# Get the http protocol match.
|
@@ -125,7 +113,7 @@ module Square
|
|
125
113
|
|
126
114
|
# Remove redundant forward slashes.
|
127
115
|
query = url[protocol.length...(!index.nil? ? index : url.length)]
|
128
|
-
query.gsub!(%r{
|
116
|
+
query.gsub!(%r{//+}, '/')
|
129
117
|
|
130
118
|
# Get the parameters.
|
131
119
|
parameters = !index.nil? ? url[url.index('?')...url.length] : ''
|
@@ -137,7 +125,7 @@ module Square
|
|
137
125
|
# Parses JSON string.
|
138
126
|
# @param [String] A JSON string.
|
139
127
|
def self.json_deserialize(json)
|
140
|
-
|
128
|
+
JSON.parse(json, symbolize_names: true)
|
141
129
|
rescue StandardError
|
142
130
|
raise TypeError, 'Server responded with invalid JSON.'
|
143
131
|
end
|
@@ -151,8 +139,8 @@ module Square
|
|
151
139
|
# Form encodes a hash of parameters.
|
152
140
|
# @param [Hash] The hash of parameters to encode.
|
153
141
|
# @return [Hash] A hash with the same parameters form encoded.
|
154
|
-
def self.form_encode_parameters(form_parameters
|
155
|
-
|
142
|
+
def self.form_encode_parameters(form_parameters)
|
143
|
+
array_serialization = 'indexed'
|
156
144
|
encoded = {}
|
157
145
|
form_parameters.each do |key, value|
|
158
146
|
encoded.merge!(APIHelper.form_encode(value, key, formatting:
|
@@ -161,11 +149,24 @@ module Square
|
|
161
149
|
encoded
|
162
150
|
end
|
163
151
|
|
152
|
+
# Process complex types in query_params.
|
153
|
+
# @param [Hash] The hash of query parameters.
|
154
|
+
# @return [Hash] A hash with the processed query parameters.
|
155
|
+
def self.process_complex_types_parameters(query_parameters, array_serialization)
|
156
|
+
processed_params = {}
|
157
|
+
query_parameters.each do |key, value|
|
158
|
+
processed_params.merge!(APIHelper.form_encode(value, key, formatting:
|
159
|
+
array_serialization))
|
160
|
+
end
|
161
|
+
processed_params
|
162
|
+
end
|
163
|
+
|
164
164
|
def self.custom_merge(a, b)
|
165
165
|
x = {}
|
166
166
|
a.each do |key, value_a|
|
167
167
|
b.each do |k, value_b|
|
168
168
|
next unless key == k
|
169
|
+
|
169
170
|
x[k] = []
|
170
171
|
if value_a.instance_of? Array
|
171
172
|
value_a.each do |v|
|
@@ -207,13 +208,12 @@ module Square
|
|
207
208
|
elsif obj.instance_of? Array
|
208
209
|
if formatting == 'indexed'
|
209
210
|
obj.each_with_index do |value, index|
|
210
|
-
retval.merge!(APIHelper.form_encode(value, instance_name
|
211
|
-
index.to_s + ']'))
|
211
|
+
retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{index}]"))
|
212
212
|
end
|
213
213
|
elsif serializable_types.map { |x| obj[0].is_a? x }.any?
|
214
214
|
obj.each do |value|
|
215
215
|
abc = if formatting == 'unindexed'
|
216
|
-
APIHelper.form_encode(value, instance_name
|
216
|
+
APIHelper.form_encode(value, "#{instance_name}[]",
|
217
217
|
formatting: formatting)
|
218
218
|
else
|
219
219
|
APIHelper.form_encode(value, instance_name,
|
@@ -223,14 +223,14 @@ module Square
|
|
223
223
|
end
|
224
224
|
else
|
225
225
|
obj.each_with_index do |value, index|
|
226
|
-
retval.merge!(APIHelper.form_encode(value, instance_name
|
227
|
-
|
226
|
+
retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{index}]",
|
227
|
+
formatting: formatting))
|
228
228
|
end
|
229
229
|
end
|
230
230
|
elsif obj.instance_of? Hash
|
231
231
|
obj.each do |key, value|
|
232
|
-
retval.merge!(APIHelper.form_encode(value, instance_name
|
233
|
-
|
232
|
+
retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{key}]",
|
233
|
+
formatting: formatting))
|
234
234
|
end
|
235
235
|
elsif obj.instance_of? File
|
236
236
|
retval[instance_name] = UploadIO.new(
|
@@ -265,17 +265,5 @@ module Square
|
|
265
265
|
end
|
266
266
|
val
|
267
267
|
end
|
268
|
-
|
269
|
-
# Safely converts a string into an rfc3339 DateTime object
|
270
|
-
# @param [String] The datetime string
|
271
|
-
# @return [DateTime] A DateTime object of rfc3339 format
|
272
|
-
def self.rfc3339(date_time)
|
273
|
-
# missing timezone information
|
274
|
-
if date_time.end_with?('Z') || date_time.index('+')
|
275
|
-
DateTime.rfc3339(date_time)
|
276
|
-
else
|
277
|
-
DateTime.rfc3339(date_time + 'Z')
|
278
|
-
end
|
279
|
-
end
|
280
268
|
end
|
281
269
|
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
|
+
'18.1.0.20220316'
|
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,24 +27,12 @@ module Square
|
|
23
27
|
@o_auth ||= OAuthApi.new config
|
24
28
|
end
|
25
29
|
|
26
|
-
# Access to v1_employees controller.
|
27
|
-
# @return [V1EmployeesApi] Returns the controller instance.
|
28
|
-
def v1_employees
|
29
|
-
@v1_employees ||= V1EmployeesApi.new config
|
30
|
-
end
|
31
|
-
|
32
30
|
# Access to v1_transactions controller.
|
33
31
|
# @return [V1TransactionsApi] Returns the controller instance.
|
34
32
|
def v1_transactions
|
35
33
|
@v1_transactions ||= V1TransactionsApi.new config
|
36
34
|
end
|
37
35
|
|
38
|
-
# Access to v1_items controller.
|
39
|
-
# @return [V1ItemsApi] Returns the controller instance.
|
40
|
-
def v1_items
|
41
|
-
@v1_items ||= V1ItemsApi.new config
|
42
|
-
end
|
43
|
-
|
44
36
|
# Access to apple_pay controller.
|
45
37
|
# @return [ApplePayApi] Returns the controller instance.
|
46
38
|
def apple_pay
|
@@ -59,6 +51,12 @@ module Square
|
|
59
51
|
@bookings ||= BookingsApi.new config
|
60
52
|
end
|
61
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,31 @@ module Square
|
|
191
213
|
@terminal ||= TerminalApi.new config
|
192
214
|
end
|
193
215
|
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
216
|
+
# Access to vendors controller.
|
217
|
+
# @return [VendorsApi] Returns the controller instance.
|
218
|
+
def vendors
|
219
|
+
@vendors ||= VendorsApi.new config
|
220
|
+
end
|
221
|
+
|
222
|
+
def initialize(connection: nil, timeout: 60, max_retries: 0,
|
223
|
+
retry_interval: 1, backoff_factor: 2,
|
224
|
+
retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
|
225
|
+
retry_methods: %i[get put], environment: 'production',
|
226
|
+
custom_url: 'https://connect.squareup.com',
|
227
|
+
square_version: '2022-03-16', access_token: '',
|
228
|
+
user_agent_detail: '', additional_headers: {}, config: nil)
|
198
229
|
@config = if config.nil?
|
199
|
-
Configuration.new(
|
230
|
+
Configuration.new(connection: connection, timeout: timeout,
|
231
|
+
max_retries: max_retries,
|
200
232
|
retry_interval: retry_interval,
|
201
233
|
backoff_factor: backoff_factor,
|
234
|
+
retry_statuses: retry_statuses,
|
235
|
+
retry_methods: retry_methods,
|
202
236
|
environment: environment,
|
237
|
+
custom_url: custom_url,
|
203
238
|
square_version: square_version,
|
204
239
|
access_token: access_token,
|
240
|
+
user_agent_detail: user_agent_detail,
|
205
241
|
additional_headers: additional_headers)
|
206
242
|
else
|
207
243
|
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, :connection, :timeout, :max_retries, :retry_interval, :backoff_factor,
|
7
|
+
:retry_statuses, :retry_methods, :environment, :custom_url, :square_version,
|
8
|
+
: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(connection: 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-03-16', access_token: '',
|
24
|
+
user_agent_detail: '', additional_headers: {})
|
25
|
+
# The Faraday connection object passed by the SDK user for making requests
|
26
|
+
@connection = connection
|
27
|
+
|
27
28
|
# The value to use for connection timeout
|
28
29
|
@timeout = timeout
|
29
30
|
|
@@ -37,9 +38,18 @@ 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
|
|
@@ -51,33 +61,54 @@ 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(connection: 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
|
+
connection ||= self.connection
|
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(connection: connection, timeout: timeout,
|
89
|
+
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, connection: connection)
|
106
|
+
end
|
107
|
+
|
108
|
+
def get_user_agent(user_agent_detail)
|
109
|
+
raise ArgumentError, 'The length of user-agent detail should not exceed 128 characters.' unless user_agent_detail.length < 128
|
110
|
+
|
111
|
+
user_agent_detail
|
81
112
|
end
|
82
113
|
|
83
114
|
# All the environments the SDK can run in.
|
@@ -87,6 +118,9 @@ module Square
|
|
87
118
|
},
|
88
119
|
'sandbox' => {
|
89
120
|
'default' => 'https://connect.squareupsandbox.com'
|
121
|
+
},
|
122
|
+
'custom' => {
|
123
|
+
'default' => '{custom_url}'
|
90
124
|
}
|
91
125
|
}.freeze
|
92
126
|
|
@@ -95,7 +129,12 @@ module Square
|
|
95
129
|
# required.
|
96
130
|
# @return [String] The base URI.
|
97
131
|
def get_base_uri(server = 'default')
|
98
|
-
|
132
|
+
parameters = {
|
133
|
+
'custom_url' => { 'value' => custom_url, 'encode' => false }
|
134
|
+
}
|
135
|
+
APIHelper.append_url_with_template_parameters(
|
136
|
+
ENVIRONMENTS[environment][server], parameters
|
137
|
+
)
|
99
138
|
end
|
100
139
|
end
|
101
140
|
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
|
@@ -4,10 +4,28 @@ require 'faraday_middleware'
|
|
4
4
|
module Square
|
5
5
|
# An implementation of HttpClient.
|
6
6
|
class FaradayClient < HttpClient
|
7
|
+
# The attribute readers for properties.
|
8
|
+
attr_reader :connection
|
9
|
+
|
7
10
|
# The constructor.
|
8
11
|
def initialize(timeout:, max_retries:, retry_interval:,
|
9
|
-
backoff_factor:,
|
10
|
-
|
12
|
+
backoff_factor:, retry_statuses:, retry_methods:,
|
13
|
+
connection: nil, cache: false, verify: true)
|
14
|
+
@connection = if connection.nil?
|
15
|
+
create_connection(timeout: timeout, max_retries: max_retries,
|
16
|
+
retry_interval: retry_interval, backoff_factor: backoff_factor,
|
17
|
+
retry_statuses: retry_statuses, retry_methods: retry_methods,
|
18
|
+
cache: cache, verify: verify)
|
19
|
+
else
|
20
|
+
connection
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Method to initialize connection.
|
25
|
+
def create_connection(timeout:, max_retries:, retry_interval:,
|
26
|
+
backoff_factor:, retry_statuses:, retry_methods:,
|
27
|
+
cache: false, verify: true)
|
28
|
+
Faraday.new do |faraday|
|
11
29
|
faraday.use Faraday::HttpCache, serializer: Marshal if cache
|
12
30
|
faraday.use FaradayMiddleware::FollowRedirects
|
13
31
|
faraday.use :gzip
|
@@ -16,10 +34,12 @@ module Square
|
|
16
34
|
faraday.ssl[:ca_file] = Certifi.where
|
17
35
|
faraday.ssl[:verify] = verify
|
18
36
|
faraday.request :retry, max: max_retries, interval: retry_interval,
|
19
|
-
backoff_factor: backoff_factor
|
37
|
+
backoff_factor: backoff_factor,
|
38
|
+
retry_statuses: retry_statuses,
|
39
|
+
methods: retry_methods
|
20
40
|
faraday.adapter Faraday.default_adapter
|
21
41
|
faraday.options[:params_encoder] = Faraday::FlatParamsEncoder
|
22
|
-
faraday.options[:timeout] = timeout if timeout
|
42
|
+
faraday.options[:timeout] = timeout if timeout.positive?
|
23
43
|
end
|
24
44
|
end
|
25
45
|
|
@@ -0,0 +1,151 @@
|
|
1
|
+
require 'date'
|
2
|
+
module Square
|
3
|
+
# A utility that supports dateTime conversion to different formats
|
4
|
+
class DateTimeHelper
|
5
|
+
# Safely converts a DateTime object into a rfc1123 format string
|
6
|
+
# @param [DateTime] The DateTime object
|
7
|
+
# @return [String] The rfc1123 formatted datetime string
|
8
|
+
def self.to_rfc1123(date_time)
|
9
|
+
date_time&.httpdate
|
10
|
+
end
|
11
|
+
|
12
|
+
# Safely converts a map of DateTime objects into a map of rfc1123 format string
|
13
|
+
# @param [hash] a map of DateTime objects
|
14
|
+
# @return [hash] a map of rfc1123 formatted datetime string
|
15
|
+
def self.to_rfc1123_map(date_time, hash, key)
|
16
|
+
return if date_time.nil?
|
17
|
+
|
18
|
+
hash[key] = {}
|
19
|
+
date_time.each do |k, v|
|
20
|
+
hash[key][k] =
|
21
|
+
if v.is_a?(BaseModel)
|
22
|
+
v.to_hash
|
23
|
+
else
|
24
|
+
v.is_a?(DateTime) ? DateTimeHelper.to_rfc1123(v) : v
|
25
|
+
end
|
26
|
+
end
|
27
|
+
hash[key]
|
28
|
+
end
|
29
|
+
|
30
|
+
# Safely converts an array of DateTime objects into an array of rfc1123 format string
|
31
|
+
# @param [Array] an array of DateTime objects
|
32
|
+
# @return [Array] an array of rfc1123 formatted datetime string
|
33
|
+
def self.to_rfc1123_array(date_time, hash, key)
|
34
|
+
return if date_time.nil?
|
35
|
+
|
36
|
+
hash[key] = date_time.map do |v|
|
37
|
+
if v.is_a?(BaseModel)
|
38
|
+
v.to_hash
|
39
|
+
else
|
40
|
+
v.is_a?(DateTime) ? DateTimeHelper.to_rfc1123(v) : v
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Safely converts a DateTime object into a unix format string
|
46
|
+
# @param [DateTime] The DateTime object
|
47
|
+
# @return [String] The unix formatted datetime string
|
48
|
+
def self.to_unix(date_time)
|
49
|
+
date_time.to_time.utc.to_i unless date_time.nil?
|
50
|
+
end
|
51
|
+
|
52
|
+
# Safely converts a map of DateTime objects into a map of unix format string
|
53
|
+
# @param [hash] a map of DateTime objects
|
54
|
+
# @return [hash] a map of unix formatted datetime string
|
55
|
+
def self.to_unix_map(date_time, hash, key)
|
56
|
+
return if date_time.nil?
|
57
|
+
|
58
|
+
hash[key] = {}
|
59
|
+
date_time.each do |k, v|
|
60
|
+
hash[key][k] =
|
61
|
+
if v.is_a?(BaseModel)
|
62
|
+
v.to_hash
|
63
|
+
else
|
64
|
+
v.is_a?(DateTime) ? DateTimeHelper.to_unix(v) : v
|
65
|
+
end
|
66
|
+
end
|
67
|
+
hash[key]
|
68
|
+
end
|
69
|
+
|
70
|
+
# Safely converts an array of DateTime objects into a map of unix format string
|
71
|
+
# @param [hash] an array of DateTime objects
|
72
|
+
# @return [hash] an array of unix formatted datetime string
|
73
|
+
def self.to_unix_array(date_time, hash, key)
|
74
|
+
return if date_time.nil?
|
75
|
+
|
76
|
+
hash[key] = date_time.map do |v|
|
77
|
+
if v.is_a?(BaseModel)
|
78
|
+
v.to_hash
|
79
|
+
else
|
80
|
+
v.is_a?(DateTime) ? DateTimeHelper.to_unix(v) : v
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# Safely converts a DateTime object into a rfc3339 format string
|
86
|
+
# @param [DateTime] The DateTime object
|
87
|
+
# @return [String] The rfc3339 formatted datetime string
|
88
|
+
def self.to_rfc3339(date_time)
|
89
|
+
date_time&.rfc3339
|
90
|
+
end
|
91
|
+
|
92
|
+
# Safely converts a map of DateTime objects into a map of rfc1123 format string
|
93
|
+
# @param [hash] a map of DateTime objects
|
94
|
+
# @return [hash] a map of rfc1123 formatted datetime string
|
95
|
+
def self.to_rfc3339_map(date_time, hash, key)
|
96
|
+
return if date_time.nil?
|
97
|
+
|
98
|
+
hash[key] = {}
|
99
|
+
date_time.each do |k, v|
|
100
|
+
hash[key][k] =
|
101
|
+
if v.is_a?(BaseModel)
|
102
|
+
v.to_hash
|
103
|
+
else
|
104
|
+
v.is_a?(DateTime) ? DateTimeHelper.to_rfc3339(v) : v
|
105
|
+
end
|
106
|
+
end
|
107
|
+
hash[key]
|
108
|
+
end
|
109
|
+
|
110
|
+
# Safely converts an array of DateTime objects into an array of rfc1123 format string
|
111
|
+
# @param [Array] an array of DateTime objects
|
112
|
+
# @return [Array] an array of rfc1123 formatted datetime string
|
113
|
+
def self.to_rfc3339_array(date_time, hash, key)
|
114
|
+
return if date_time.nil?
|
115
|
+
|
116
|
+
hash[key] = date_time.map do |v|
|
117
|
+
if v.is_a?(BaseModel)
|
118
|
+
v.to_hash
|
119
|
+
else
|
120
|
+
v.is_a?(DateTime) ? DateTimeHelper.to_rfc3339(v) : v
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
# Safely converts a rfc1123 format string into a DateTime object
|
126
|
+
# @param [String] The rfc1123 formatted datetime string
|
127
|
+
# @return [DateTime] A DateTime object
|
128
|
+
def self.from_rfc1123(date_time)
|
129
|
+
DateTime.httpdate(date_time)
|
130
|
+
end
|
131
|
+
|
132
|
+
# Safely converts a unix format string into a DateTime object
|
133
|
+
# @param [String] The unix formatted datetime string
|
134
|
+
# @return [DateTime] A DateTime object
|
135
|
+
def self.from_unix(date_time)
|
136
|
+
Time.at(date_time.to_i).utc.to_datetime
|
137
|
+
end
|
138
|
+
|
139
|
+
# Safely converts a rfc3339 format string into a DateTime object
|
140
|
+
# @param [String] The rfc3339 formatted datetime string
|
141
|
+
# @return [DateTime] A DateTime object
|
142
|
+
def self.from_rfc3339(date_time)
|
143
|
+
# missing timezone information
|
144
|
+
if date_time.end_with?('Z') || date_time.index('+')
|
145
|
+
DateTime.rfc3339(date_time)
|
146
|
+
else
|
147
|
+
DateTime.rfc3339("#{date_time}Z")
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|