square.rb 26.1.0.20230119 → 26.2.0.20230315

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/lib/square/api/apple_pay_api.rb +14 -30
  3. data/lib/square/api/bank_accounts_api.rb +40 -90
  4. data/lib/square/api/base_api.rb +42 -42
  5. data/lib/square/api/booking_custom_attributes_api.rb +164 -329
  6. data/lib/square/api/bookings_api.rb +135 -260
  7. data/lib/square/api/cards_api.rb +56 -118
  8. data/lib/square/api/cash_drawers_api.rb +47 -103
  9. data/lib/square/api/catalog_api.rb +197 -423
  10. data/lib/square/api/checkout_api.rb +85 -175
  11. data/lib/square/api/customer_custom_attributes_api.rb +150 -303
  12. data/lib/square/api/customer_groups_api.rb +69 -145
  13. data/lib/square/api/customer_segments_api.rb +26 -61
  14. data/lib/square/api/customers_api.rb +147 -294
  15. data/lib/square/api/devices_api.rb +42 -89
  16. data/lib/square/api/disputes_api.rb +130 -288
  17. data/lib/square/api/employees_api.rb +28 -63
  18. data/lib/square/api/gift_card_activities_api.rb +33 -65
  19. data/lib/square/api/gift_cards_api.rb +103 -202
  20. data/lib/square/api/inventory_api.rb +179 -366
  21. data/lib/square/api/invoices_api.rb +118 -237
  22. data/lib/square/api/labor_api.rb +223 -459
  23. data/lib/square/api/location_custom_attributes_api.rb +166 -331
  24. data/lib/square/api/locations_api.rb +54 -112
  25. data/lib/square/api/loyalty_api.rb +259 -512
  26. data/lib/square/api/merchants_api.rb +25 -60
  27. data/lib/square/api/mobile_authorization_api.rb +14 -30
  28. data/lib/square/api/o_auth_api.rb +54 -109
  29. data/lib/square/api/order_custom_attributes_api.rb +168 -333
  30. data/lib/square/api/orders_api.rb +115 -222
  31. data/lib/square/api/payments_api.rb +106 -208
  32. data/lib/square/api/payouts_api.rb +47 -100
  33. data/lib/square/api/refunds_api.rb +46 -93
  34. data/lib/square/api/sites_api.rb +11 -28
  35. data/lib/square/api/snippets_api.rb +42 -90
  36. data/lib/square/api/subscriptions_api.rb +150 -299
  37. data/lib/square/api/team_api.rb +114 -224
  38. data/lib/square/api/terminal_api.rb +162 -328
  39. data/lib/square/api/transactions_api.rb +62 -126
  40. data/lib/square/api/v1_transactions_api.rb +155 -296
  41. data/lib/square/api/vendors_api.rb +99 -192
  42. data/lib/square/api/webhook_subscriptions_api.rb +115 -235
  43. data/lib/square/api_helper.rb +1 -437
  44. data/lib/square/client.rb +77 -47
  45. data/lib/square/configuration.rb +28 -53
  46. data/lib/square/exceptions/api_exception.rb +1 -11
  47. data/lib/square/http/api_response.rb +13 -19
  48. data/lib/square/http/auth/o_auth2.rb +14 -7
  49. data/lib/square/http/http_call_back.rb +1 -15
  50. data/lib/square/http/http_method_enum.rb +1 -4
  51. data/lib/square/http/http_request.rb +1 -45
  52. data/lib/square/http/http_response.rb +1 -20
  53. data/lib/square/utilities/date_time_helper.rb +1 -146
  54. data/lib/square/utilities/file_wrapper.rb +5 -5
  55. data/lib/square.rb +6 -6
  56. data/spec/user_journey_spec.rb +2 -2
  57. data/test/api/api_test_base.rb +13 -5
  58. data/test/api/test_catalog_api.rb +5 -4
  59. data/test/api/test_customers_api.rb +3 -2
  60. data/test/api/test_employees_api.rb +4 -3
  61. data/test/api/test_labor_api.rb +6 -5
  62. data/test/api/test_locations_api.rb +3 -2
  63. data/test/api/test_merchants_api.rb +4 -3
  64. data/test/api/test_payments_api.rb +4 -3
  65. data/test/api/test_refunds_api.rb +4 -3
  66. metadata +11 -111
  67. data/lib/square/exceptions/validation_exception.rb +0 -13
  68. data/lib/square/http/faraday_client.rb +0 -93
  69. data/lib/square/http/http_client.rb +0 -118
  70. data/test/test_helper.rb +0 -89
@@ -1,441 +1,5 @@
1
1
  module Square
2
2
  # API utility class
3
- class APIHelper
4
- # Serializes an array parameter (creates key value pairs).
5
- # @param [String] The name of the parameter.
6
- # @param [Array] The value of the parameter.
7
- # @param [String] The format of the serialization.
8
- def self.serialize_array(key, array, formatting: 'indexed')
9
- tuples = []
10
-
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
21
- tuples
22
- end
23
-
24
- # Replaces template parameters in the given url.
25
- # @param [String] The query string builder to replace the template
26
- # parameters.
27
- # @param [Hash] The parameters to replace in the url.
28
- def self.append_url_with_template_parameters(query_builder, parameters)
29
- # perform parameter validation
30
- unless query_builder.instance_of? String
31
- raise ArgumentError, 'Given value for parameter \"query_builder\" is
32
- invalid.'
33
- end
34
-
35
- # Return if there are no parameters to replace.
36
- return query_builder if parameters.nil?
37
-
38
- parameters.each do |key, val|
39
- if val.nil?
40
- replace_value = ''
41
- elsif val['value'].instance_of? Array
42
- if val['encode'] == true
43
- val['value'].map! { |element| CGI.escape(element.to_s) }
44
- else
45
- val['value'].map!(&:to_s)
46
- end
47
- replace_value = val['value'].join('/')
48
- else
49
- replace_value = if val['encode'] == true
50
- CGI.escape(val['value'].to_s)
51
- else
52
- val['value'].to_s
53
- end
54
- end
55
-
56
- # Find the template parameter and replace it with its value.
57
- query_builder = query_builder.gsub("{#{key}}", replace_value)
58
- end
59
- query_builder
60
- end
61
-
62
- # Appends the given set of parameters to the given query string.
63
- # @param [String] The query string builder to add the query parameters to.
64
- # @param [Hash] The parameters to append.
65
- def self.append_url_with_query_parameters(query_builder, parameters)
66
- # Perform parameter validation.
67
- unless query_builder.instance_of? String
68
- raise ArgumentError, 'Given value for parameter \"query_builder\"
69
- is invalid.'
70
- end
71
-
72
- # Return if there are no parameters to replace.
73
- return query_builder if parameters.nil?
74
-
75
- array_serialization = 'indexed'
76
- parameters = process_complex_types_parameters(parameters, array_serialization)
77
-
78
- parameters.each do |key, value|
79
- seperator = query_builder.include?('?') ? '&' : '?'
80
- unless value.nil?
81
- if value.instance_of? Array
82
- value.compact!
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
89
- else
90
- query_builder += "#{seperator}#{key}=#{CGI.escape(value.to_s)}"
91
- end
92
- end
93
- end
94
- query_builder
95
- end
96
-
97
- # Validates and processes the given Url.
98
- # @param [String] The given Url to process.
99
- # @return [String] Pre-processed Url as string.
100
- def self.clean_url(url)
101
- # Perform parameter validation.
102
- raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
103
-
104
- # Ensure that the urls are absolute.
105
- matches = url.match(%r{^(https?://[^/]+)})
106
- raise ArgumentError, 'Invalid Url format.' if matches.nil?
107
-
108
- # Get the http protocol match.
109
- protocol = matches[1]
110
-
111
- # Check if parameters exist.
112
- index = url.index('?')
113
-
114
- # Remove redundant forward slashes.
115
- query = url[protocol.length...(!index.nil? ? index : url.length)]
116
- query.gsub!(%r{//+}, '/')
117
-
118
- # Get the parameters.
119
- parameters = !index.nil? ? url[url.index('?')...url.length] : ''
120
-
121
- # Return processed url.
122
- protocol + query + parameters
123
- end
124
-
125
- # Parses JSON string.
126
- # @param [String] A JSON string.
127
- def self.json_deserialize(json)
128
- JSON.parse(json, symbolize_names: true)
129
- rescue StandardError
130
- raise TypeError, 'Server responded with invalid JSON.'
131
- end
132
-
133
- # Parses JSON string.
134
- # @param [object] The object to serialize.
135
- def self.json_serialize(obj)
136
- serializable_types.map { |x| obj.is_a? x }.any? ? obj.to_s : obj.to_json
137
- end
138
-
139
- # Removes elements with empty values from a hash.
140
- # @param [Hash] The hash to clean.
141
- def self.clean_hash(hash)
142
- hash.delete_if { |_key, value| value.to_s.strip.empty? }
143
- end
144
-
145
- # Form encodes a hash of parameters.
146
- # @param [Hash] The hash of parameters to encode.
147
- # @return [Hash] A hash with the same parameters form encoded.
148
- def self.form_encode_parameters(form_parameters)
149
- array_serialization = 'indexed'
150
- encoded = {}
151
- form_parameters.each do |key, value|
152
- encoded.merge!(APIHelper.form_encode(value, key, formatting:
153
- array_serialization))
154
- end
155
- encoded
156
- end
157
-
158
- # Process complex types in query_params.
159
- # @param [Hash] The hash of query parameters.
160
- # @return [Hash] A hash with the processed query parameters.
161
- def self.process_complex_types_parameters(query_parameters, array_serialization)
162
- processed_params = {}
163
- query_parameters.each do |key, value|
164
- processed_params.merge!(APIHelper.form_encode(value, key, formatting:
165
- array_serialization))
166
- end
167
- processed_params
168
- end
169
-
170
- def self.custom_merge(a, b)
171
- x = {}
172
- a.each do |key, value_a|
173
- b.each do |k, value_b|
174
- next unless key == k
175
-
176
- x[k] = []
177
- if value_a.instance_of? Array
178
- value_a.each do |v|
179
- x[k].push(v)
180
- end
181
- else
182
- x[k].push(value_a)
183
- end
184
- if value_b.instance_of? Array
185
- value_b.each do |v|
186
- x[k].push(v)
187
- end
188
- else
189
- x[k].push(value_b)
190
- end
191
- a.delete(k)
192
- b.delete(k)
193
- end
194
- end
195
- x.merge!(a)
196
- x.merge!(b)
197
- x
198
- end
199
-
200
- # Form encodes an object.
201
- # @param [Dynamic] An object to form encode.
202
- # @param [String] The name of the object.
203
- # @return [Hash] A form encoded representation of the object in the form
204
- # of a hash.
205
- def self.form_encode(obj, instance_name, formatting: 'indexed')
206
- retval = {}
207
-
208
- # Create a form encoded hash for this object.
209
- if obj.nil?
210
- nil
211
- elsif obj.instance_of? Array
212
- if formatting == 'indexed'
213
- obj.each_with_index do |value, index|
214
- retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{index}]"))
215
- end
216
- elsif APIHelper.serializable_types.map { |x| obj[0].is_a? x }.any?
217
- obj.each do |value|
218
- abc = if formatting == 'unindexed'
219
- APIHelper.form_encode(value, "#{instance_name}[]",
220
- formatting: formatting)
221
- else
222
- APIHelper.form_encode(value, instance_name,
223
- formatting: formatting)
224
- end
225
- retval = APIHelper.custom_merge(retval, abc)
226
- end
227
- else
228
- obj.each_with_index do |value, index|
229
- retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{index}]",
230
- formatting: formatting))
231
- end
232
- end
233
- elsif obj.instance_of? Hash
234
- obj.each do |key, value|
235
- retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{key}]",
236
- formatting: formatting))
237
- end
238
- elsif obj.instance_of? File
239
- retval[instance_name] = UploadIO.new(
240
- obj, 'application/octet-stream', File.basename(obj.path)
241
- )
242
- else
243
- retval[instance_name] = obj
244
- end
245
- retval
246
- end
247
-
248
- # Retrieves a field from a Hash/Array based on an Array of keys/indexes
249
- # @param [Hash, Array] The hash to extract data from
250
- # @param [Array<String, Integer>] The keys/indexes to use
251
- # @return [Object] The extracted value
252
- def self.map_response(obj, keys)
253
- val = obj
254
- begin
255
- keys.each do |key|
256
- val = if val.is_a? Array
257
- if key.to_i.to_s == key
258
- val[key.to_i]
259
- else
260
- val = nil
261
- end
262
- else
263
- val.fetch(key.to_sym)
264
- end
265
- end
266
- rescue NoMethodError, TypeError, IndexError
267
- val = nil
268
- end
269
- val
270
- end
271
-
272
- # Deserialize the value against the template (group of types).
273
- # @param [String] The value to be deserialized.
274
- # @param [String] The parameter indicates the type-combination
275
- # against which the value will be mapped (oneOf(Integer, String)).
276
- def self.deserialize(template, value)
277
- decoded = APIHelper.json_deserialize(value)
278
- map_types(decoded, template)
279
- end
280
-
281
- # Validates and processes the value against the template(group of types).
282
- # @param [String] The value to be mapped against the template.
283
- # @param [String] The parameter indicates the group of types (oneOf(Integer, String)).
284
- # @param [String] The parameter indicates the group (oneOf|anyOf).
285
- def self.map_types(value, template, group_name: nil)
286
- result_value = nil
287
- matches = 0
288
- types = []
289
- group_name = template.partition('(').first if group_name.nil? && template.match?(/anyOf|oneOf/)
290
-
291
- return if value.nil?
292
-
293
- if template.end_with?('{}') || template.end_with?('[]')
294
- types = template.split(group_name, 2).last.gsub(/\s+/, '').split
295
- else
296
- template = template.split(group_name, 2).last.delete_prefix('(').delete_suffix(')')
297
- types = template.scan(/(anyOf|oneOf)[(]([^[)]]*)[)]/).flatten.combination(2).map { |a, b| "#{a}(#{b})" }
298
- types.each { |t| template = template.gsub(", #{t}", '') }
299
- types = template.gsub(/\s+/, '').split(',').push(*types)
300
- end
301
- types.each do |element|
302
- if element.match?(/^(oneOf|anyOf)[(].*$/)
303
- begin
304
- result_value = map_types(value, element, matches)
305
- matches += 1
306
- rescue ValidationException
307
- next
308
- end
309
- elsif element.end_with?('{}')
310
- result_value, matches = map_hash_type(value, element, group_name, matches)
311
- elsif element.end_with?('[]')
312
- result_value, matches = map_array_type(value, element, group_name, matches)
313
- else
314
- begin
315
- result_value, matches = map_type(value, element, group_name, matches)
316
- rescue StandardError
317
- next
318
- end
319
- end
320
- break if group_name == 'anyOf' && matches == 1
321
- end
322
- raise ValidationException.new(value, template) unless matches == 1
323
-
324
- value = result_value unless result_value.nil?
325
- value
326
- end
327
-
328
- # Validates and processes the value against the [Hash] type.
329
- # @param [String] The value to be mapped against the type.
330
- # @param [String] The possible type of the value.
331
- # @param [String] The parameter indicates the group (oneOf|anyOf).
332
- # @param [Integer] The parameter indicates the number of matches of value against types.
333
- def self.map_hash_type(value, type, group_name, matches)
334
- if value.instance_of? Hash
335
- decoded = {}
336
- value.each do |key, val|
337
- type = type.chomp('{}').to_s
338
- val = map_types(val, type, group_name: group_name)
339
- decoded[key] = val unless type.empty?
340
- rescue ValidationException
341
- next
342
- end
343
- matches += 1 if decoded.length == value.length
344
- value = decoded unless decoded.empty?
345
- end
346
- [value, matches]
347
- end
348
-
349
- # Validates and processes the value against the [Array] type.
350
- # @param [String] The value to be mapped against the type.
351
- # @param [String] The possible type of the value.
352
- # @param [String] The parameter indicates the group (oneOf|anyOf).
353
- # @param [Integer] The parameter indicates the number of matches of value against types.
354
- def self.map_array_type(value, type, group_name, matches)
355
- if value.instance_of? Array
356
- decoded = []
357
- value.each do |val|
358
- type = type.chomp('[]').to_s
359
- val = map_types(val, type, group_name: group_name)
360
- decoded.append(val) unless type.empty?
361
- rescue ValidationException
362
- next
363
- end
364
- matches += 1 if decoded.length == value.length
365
- value = decoded unless decoded.empty?
366
- end
367
- [value, matches]
368
- end
369
-
370
- # Validates and processes the value against the type.
371
- # @param [String] The value to be mapped against the type.
372
- # @param [String] The possible type of the value.
373
- # @param [String] The parameter indicates the group (oneOf|anyOf).
374
- # @param [Integer] The parameter indicates the number of matches of value against types.
375
- def self.map_type(value, type, _group_name, matches)
376
- if Square.constants.select do |c|
377
- Square.const_get(c).to_s == "Square::#{type}"
378
- end.empty?
379
- value, matches = map_data_type(value, type, matches)
380
- else
381
- value, matches = map_complex_type(value, type, matches)
382
- end
383
- [value, matches]
384
- end
385
-
386
- # Validates and processes the value against the complex types.
387
- # @param [String] The value to be mapped against the type.
388
- # @param [String] The possible type of the value.
389
- # @param [Integer] The parameter indicates the number of matches of value against types.
390
- def self.map_complex_type(value, type, matches)
391
- obj = Square.const_get(type)
392
- value = if obj.respond_to? 'from_hash'
393
- obj.send('from_hash', value)
394
- else
395
- obj.constants.find { |k| obj.const_get(k) == value }
396
- end
397
- matches += 1 unless value.nil?
398
- [value, matches]
399
- end
400
-
401
- # Validates and processes the value against the data types.
402
- # @param [String] The value to be mapped against the type.
403
- # @param [String] The possible type of the value.
404
- # @param [Integer] The parameter indicates the number of matches of value against types.
405
- def self.map_data_type(value, element, matches)
406
- element = element.split('|').map { |x| Object.const_get x }
407
- matches += 1 if element.all? { |x| APIHelper.data_types.include?(x) } &&
408
- element.any? { |x| (value.instance_of? x) || (value.class.ancestors.include? x) }
409
- [value, matches]
410
- end
411
-
412
- # Validates the value against the template(group of types).
413
- # @param [String] The value to be mapped against the type.
414
- # @param [String] The parameter indicates the group of types (oneOf(Integer, String)).
415
- def self.validate_types(value, template)
416
- map_types(APIHelper.json_deserialize(value.to_json), template)
417
- end
418
-
419
- # Get content-type depending on the value
420
- def self.get_content_type(value)
421
- if serializable_types.map { |x| value.is_a? x }.any?
422
- 'text/plain; charset=utf-8'
423
- else
424
- 'application/json; charset=utf-8'
425
- end
426
- end
427
-
428
- # Array of serializable types
429
- def self.serializable_types
430
- [String, Numeric, TrueClass,
431
- FalseClass, Date, DateTime]
432
- end
433
-
434
- # Array of supported data types
435
- def self.data_types
436
- [String, Float, Integer,
437
- TrueClass, FalseClass, Date,
438
- DateTime, Array, Hash, Object]
439
- end
3
+ class APIHelper < CoreLibrary::ApiHelper
440
4
  end
441
5
  end