square.rb 20.1.0.20220616 → 22.0.0.20220817

Sign up to get free protection for your applications and to get access to all the features.
@@ -130,6 +130,12 @@ module Square
130
130
  raise TypeError, 'Server responded with invalid JSON.'
131
131
  end
132
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
+
133
139
  # Removes elements with empty values from a hash.
134
140
  # @param [Hash] The hash to clean.
135
141
  def self.clean_hash(hash)
@@ -199,9 +205,6 @@ module Square
199
205
  def self.form_encode(obj, instance_name, formatting: 'indexed')
200
206
  retval = {}
201
207
 
202
- serializable_types = [String, Numeric, TrueClass,
203
- FalseClass, Date, DateTime]
204
-
205
208
  # Create a form encoded hash for this object.
206
209
  if obj.nil?
207
210
  nil
@@ -210,7 +213,7 @@ module Square
210
213
  obj.each_with_index do |value, index|
211
214
  retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{index}]"))
212
215
  end
213
- elsif serializable_types.map { |x| obj[0].is_a? x }.any?
216
+ elsif APIHelper.serializable_types.map { |x| obj[0].is_a? x }.any?
214
217
  obj.each do |value|
215
218
  abc = if formatting == 'unindexed'
216
219
  APIHelper.form_encode(value, "#{instance_name}[]",
@@ -265,5 +268,174 @@ module Square
265
268
  end
266
269
  val
267
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
268
440
  end
269
441
  end
data/lib/square/client.rb CHANGED
@@ -4,7 +4,7 @@ module Square
4
4
  attr_reader :config
5
5
 
6
6
  def sdk_version
7
- '20.1.0.20220616'
7
+ '22.0.0.20220817'
8
8
  end
9
9
 
10
10
  def square_version
@@ -231,13 +231,18 @@ module Square
231
231
  @vendors ||= VendorsApi.new config
232
232
  end
233
233
 
234
- def initialize(connection: nil, adapter: Faraday.default_adapter,
235
- timeout: 60, max_retries: 0, retry_interval: 1,
236
- backoff_factor: 2,
234
+ # Access to webhook_subscriptions controller.
235
+ # @return [WebhookSubscriptionsApi] Returns the controller instance.
236
+ def webhook_subscriptions
237
+ @webhook_subscriptions ||= WebhookSubscriptionsApi.new config
238
+ end
239
+
240
+ def initialize(connection: nil, adapter: :net_http_persistent, timeout: 60,
241
+ max_retries: 0, retry_interval: 1, backoff_factor: 2,
237
242
  retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
238
243
  retry_methods: %i[get put], environment: 'production',
239
244
  custom_url: 'https://connect.squareup.com',
240
- square_version: '2022-06-16', access_token: '',
245
+ square_version: '2022-08-17', access_token: '',
241
246
  user_agent_detail: '', additional_headers: {}, config: nil)
242
247
  @config = if config.nil?
243
248
  Configuration.new(connection: connection, adapter: adapter,
@@ -15,13 +15,12 @@ module Square
15
15
  attr_reader :environments
16
16
  end
17
17
 
18
- def initialize(connection: nil, adapter: Faraday.default_adapter,
19
- timeout: 60, max_retries: 0, retry_interval: 1,
20
- backoff_factor: 2,
18
+ def initialize(connection: nil, adapter: :net_http_persistent, timeout: 60,
19
+ max_retries: 0, retry_interval: 1, backoff_factor: 2,
21
20
  retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
22
21
  retry_methods: %i[get put], environment: 'production',
23
22
  custom_url: 'https://connect.squareup.com',
24
- square_version: '2022-06-16', access_token: '',
23
+ square_version: '2022-08-17', access_token: '',
25
24
  user_agent_detail: '', additional_headers: {})
26
25
  # The Faraday connection object passed by the SDK user for making requests
27
26
  @connection = connection
@@ -0,0 +1,13 @@
1
+ module Square
2
+ # Class for exceptions when there is a schema validation error.
3
+ class ValidationException < StandardError
4
+ attr_reader :reason
5
+
6
+ # The constructor.
7
+ # @param [String] The reason for raising an exception.
8
+ def initialize(value, template)
9
+ @reason = "The value #{value} provided doesn't validate against the schema #{template}"
10
+ super(reason)
11
+ end
12
+ end
13
+ end
@@ -3,6 +3,7 @@ require 'faraday/retry'
3
3
  require 'faraday/multipart'
4
4
  require 'faraday/follow_redirects'
5
5
  require 'faraday/gzip'
6
+ require 'faraday/net_http_persistent'
6
7
 
7
8
  module Square
8
9
  # An implementation of HttpClient.
data/lib/square.rb CHANGED
@@ -25,6 +25,7 @@ require_relative 'square/http/auth/o_auth2'
25
25
 
26
26
  # Exceptions
27
27
  require_relative 'square/exceptions/api_exception'
28
+ require_relative 'square/exceptions/validation_exception'
28
29
 
29
30
  require_relative 'square/configuration'
30
31
 
@@ -66,3 +67,4 @@ require_relative 'square/api/subscriptions_api'
66
67
  require_relative 'square/api/team_api'
67
68
  require_relative 'square/api/terminal_api'
68
69
  require_relative 'square/api/vendors_api'
70
+ require_relative 'square/api/webhook_subscriptions_api'
data/test/test_helper.rb CHANGED
@@ -49,7 +49,7 @@ class TestHelper
49
49
  end
50
50
  end
51
51
  elsif expected_body.instance_of? Array
52
- return False unless received_body.instance_of? Array
52
+ return false unless received_body.instance_of? Array
53
53
  if check_count == true && (expected_body.length != received_body.length)
54
54
  return false
55
55
  else
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: square.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 20.1.0.20220616
4
+ version: 22.0.0.20220817
5
5
  platform: ruby
6
6
  authors:
7
7
  - Square Developer Platform
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-16 00:00:00.000000000 Z
11
+ date: 2022-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: logging
@@ -31,9 +31,9 @@ dependencies:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '2.0'
34
- - - ">="
34
+ - - "<"
35
35
  - !ruby/object:Gem::Version
36
- version: 2.0.1
36
+ version: 2.5.0
37
37
  type: :runtime
38
38
  prerelease: false
39
39
  version_requirements: !ruby/object:Gem::Requirement
@@ -41,9 +41,9 @@ dependencies:
41
41
  - - "~>"
42
42
  - !ruby/object:Gem::Version
43
43
  version: '2.0'
44
- - - ">="
44
+ - - "<"
45
45
  - !ruby/object:Gem::Version
46
- version: 2.0.1
46
+ version: 2.5.0
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: faraday-follow_redirects
49
49
  requirement: !ruby/object:Gem::Requirement
@@ -100,6 +100,20 @@ dependencies:
100
100
  - - "~>"
101
101
  - !ruby/object:Gem::Version
102
102
  version: '1.0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: faraday-net_http_persistent
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '2.0'
110
+ type: :runtime
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '2.0'
103
117
  - !ruby/object:Gem::Dependency
104
118
  name: certifi
105
119
  requirement: !ruby/object:Gem::Requirement
@@ -215,10 +229,12 @@ files:
215
229
  - lib/square/api/transactions_api.rb
216
230
  - lib/square/api/v1_transactions_api.rb
217
231
  - lib/square/api/vendors_api.rb
232
+ - lib/square/api/webhook_subscriptions_api.rb
218
233
  - lib/square/api_helper.rb
219
234
  - lib/square/client.rb
220
235
  - lib/square/configuration.rb
221
236
  - lib/square/exceptions/api_exception.rb
237
+ - lib/square/exceptions/validation_exception.rb
222
238
  - lib/square/http/api_response.rb
223
239
  - lib/square/http/auth/o_auth2.rb
224
240
  - lib/square/http/faraday_client.rb