square.rb 10.0.0.202104217 → 13.1.0.20210818

Sign up to get free protection for your applications and to get access to all the features.
@@ -152,8 +152,9 @@ module Square
152
152
 
153
153
  # Charges a card represented by a card nonce or a customer's card on file.
154
154
  # Your request to this endpoint must include _either_:
155
- # - A value for the `card_nonce` parameter (to charge a card nonce generated
156
- # with the `SqPaymentForm`)
155
+ # - A value for the `card_nonce` parameter (to charge a card payment token
156
+ # generated
157
+ # with the Web Payments SDK)
157
158
  # - Values for the `customer_card_id` and `customer_id` parameters (to
158
159
  # charge
159
160
  # a customer's card on file)
@@ -20,6 +20,7 @@ module Square
20
20
  order: nil,
21
21
  limit: nil,
22
22
  batch_token: nil)
23
+ warn 'Endpoint list_orders in V1TransactionsApi is deprecated'
23
24
  # Prepare query url.
24
25
  _query_builder = config.get_base_uri
25
26
  _query_builder << '/v1/{location_id}/orders'
@@ -66,6 +67,7 @@ module Square
66
67
  # @return [V1Order Hash] response from the API call
67
68
  def retrieve_order(location_id:,
68
69
  order_id:)
70
+ warn 'Endpoint retrieve_order in V1TransactionsApi is deprecated'
69
71
  # Prepare query url.
70
72
  _query_builder = config.get_base_uri
71
73
  _query_builder << '/v1/{location_id}/orders/{order_id}'
@@ -111,6 +113,7 @@ module Square
111
113
  def update_order(location_id:,
112
114
  order_id:,
113
115
  body:)
116
+ warn 'Endpoint update_order in V1TransactionsApi is deprecated'
114
117
  # Prepare query url.
115
118
  _query_builder = config.get_base_uri
116
119
  _query_builder << '/v1/{location_id}/orders/{order_id}'
@@ -184,6 +187,7 @@ module Square
184
187
  limit: nil,
185
188
  batch_token: nil,
186
189
  include_partial: false)
190
+ warn 'Endpoint list_payments in V1TransactionsApi is deprecated'
187
191
  # Prepare query url.
188
192
  _query_builder = config.get_base_uri
189
193
  _query_builder << '/v1/{location_id}/payments'
@@ -233,6 +237,7 @@ module Square
233
237
  # @return [V1Payment Hash] response from the API call
234
238
  def retrieve_payment(location_id:,
235
239
  payment_id:)
240
+ warn 'Endpoint retrieve_payment in V1TransactionsApi is deprecated'
236
241
  # Prepare query url.
237
242
  _query_builder = config.get_base_uri
238
243
  _query_builder << '/v1/{location_id}/payments/{payment_id}'
@@ -294,6 +299,7 @@ module Square
294
299
  end_time: nil,
295
300
  limit: nil,
296
301
  batch_token: nil)
302
+ warn 'Endpoint list_refunds in V1TransactionsApi is deprecated'
297
303
  # Prepare query url.
298
304
  _query_builder = config.get_base_uri
299
305
  _query_builder << '/v1/{location_id}/refunds'
@@ -350,6 +356,7 @@ module Square
350
356
  # @return [V1Refund Hash] response from the API call
351
357
  def create_refund(location_id:,
352
358
  body:)
359
+ warn 'Endpoint create_refund in V1TransactionsApi is deprecated'
353
360
  # Prepare query url.
354
361
  _query_builder = config.get_base_uri
355
362
  _query_builder << '/v1/{location_id}/refunds'
@@ -417,6 +424,7 @@ module Square
417
424
  limit: nil,
418
425
  status: nil,
419
426
  batch_token: nil)
427
+ warn 'Endpoint list_settlements in V1TransactionsApi is deprecated'
420
428
  # Prepare query url.
421
429
  _query_builder = config.get_base_uri
422
430
  _query_builder << '/v1/{location_id}/settlements'
@@ -479,6 +487,7 @@ module Square
479
487
  # @return [V1Settlement Hash] response from the API call
480
488
  def retrieve_settlement(location_id:,
481
489
  settlement_id:)
490
+ warn 'Endpoint retrieve_settlement in V1TransactionsApi is deprecated'
482
491
  # Prepare query url.
483
492
  _query_builder = config.get_base_uri
484
493
  _query_builder << '/v1/{location_id}/settlements/{settlement_id}'
@@ -8,13 +8,14 @@ module Square
8
8
  def self.serialize_array(key, array, formatting: 'indexed')
9
9
  tuples = []
10
10
 
11
- if formatting == 'unindexed'
11
+ case formatting
12
+ when 'unindexed'
12
13
  tuples += array.map { |element| ["#{key}[]", element] }
13
- elsif formatting == 'indexed'
14
+ when 'indexed'
14
15
  tuples += array.map.with_index do |element, index|
15
16
  ["#{key}[#{index}]", element]
16
17
  end
17
- elsif formatting == 'plain'
18
+ when 'plain'
18
19
  tuples += array.map { |element| [key, element] }
19
20
  else
20
21
  raise ArgumentError, 'Invalid format provided.'
@@ -55,7 +56,7 @@ module Square
55
56
  end
56
57
 
57
58
  # Find the template parameter and replace it with its value.
58
- query_builder = query_builder.gsub('{' + key.to_s + '}', replace_value)
59
+ query_builder = query_builder.gsub("{#{key}}", replace_value)
59
60
  end
60
61
  query_builder
61
62
  end
@@ -80,15 +81,16 @@ module Square
80
81
  unless value.nil?
81
82
  if value.instance_of? Array
82
83
  value.compact!
83
- query_builder += if array_serialization == 'csv'
84
+ query_builder += case array_serialization
85
+ when 'csv'
84
86
  "#{seperator}#{key}=#{value.map do |element|
85
87
  CGI.escape(element.to_s)
86
88
  end.join(',')}"
87
- elsif array_serialization == 'psv'
89
+ when 'psv'
88
90
  "#{seperator}#{key}=#{value.map do |element|
89
91
  CGI.escape(element.to_s)
90
92
  end.join('|')}"
91
- elsif array_serialization == 'tsv'
93
+ when 'tsv'
92
94
  "#{seperator}#{key}=#{value.map do |element|
93
95
  CGI.escape(element.to_s)
94
96
  end.join("\t")}"
@@ -114,7 +116,7 @@ module Square
114
116
  raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
115
117
 
116
118
  # Ensure that the urls are absolute.
117
- matches = url.match(%r{^(https?:\/\/[^\/]+)})
119
+ matches = url.match(%r{^(https?://[^/]+)})
118
120
  raise ArgumentError, 'Invalid Url format.' if matches.nil?
119
121
 
120
122
  # Get the http protocol match.
@@ -125,7 +127,7 @@ module Square
125
127
 
126
128
  # Remove redundant forward slashes.
127
129
  query = url[protocol.length...(!index.nil? ? index : url.length)]
128
- query.gsub!(%r{\/\/+}, '/')
130
+ query.gsub!(%r{//+}, '/')
129
131
 
130
132
  # Get the parameters.
131
133
  parameters = !index.nil? ? url[url.index('?')...url.length] : ''
@@ -137,7 +139,7 @@ module Square
137
139
  # Parses JSON string.
138
140
  # @param [String] A JSON string.
139
141
  def self.json_deserialize(json)
140
- return JSON.parse(json, symbolize_names: true)
142
+ JSON.parse(json, symbolize_names: true)
141
143
  rescue StandardError
142
144
  raise TypeError, 'Server responded with invalid JSON.'
143
145
  end
@@ -207,13 +209,12 @@ module Square
207
209
  elsif obj.instance_of? Array
208
210
  if formatting == 'indexed'
209
211
  obj.each_with_index do |value, index|
210
- retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
211
- index.to_s + ']'))
212
+ retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{index}]"))
212
213
  end
213
214
  elsif serializable_types.map { |x| obj[0].is_a? x }.any?
214
215
  obj.each do |value|
215
216
  abc = if formatting == 'unindexed'
216
- APIHelper.form_encode(value, instance_name + '[]',
217
+ APIHelper.form_encode(value, "#{instance_name}[]",
217
218
  formatting: formatting)
218
219
  else
219
220
  APIHelper.form_encode(value, instance_name,
@@ -223,14 +224,14 @@ module Square
223
224
  end
224
225
  else
225
226
  obj.each_with_index do |value, index|
226
- retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
227
- index.to_s + ']', formatting: formatting))
227
+ retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{index}]",
228
+ formatting: formatting))
228
229
  end
229
230
  end
230
231
  elsif obj.instance_of? Hash
231
232
  obj.each do |key, value|
232
- retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
233
- key.to_s + ']', formatting: formatting))
233
+ retval.merge!(APIHelper.form_encode(value, "#{instance_name}[#{key}]",
234
+ formatting: formatting))
234
235
  end
235
236
  elsif obj.instance_of? File
236
237
  retval[instance_name] = UploadIO.new(
@@ -265,17 +266,5 @@ module Square
265
266
  end
266
267
  val
267
268
  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
269
  end
281
270
  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
- '10.0.0.202104217'
7
+ '13.1.0.20210818'
8
8
  end
9
9
 
10
10
  def square_version
@@ -53,6 +53,12 @@ module Square
53
53
  @bookings ||= BookingsApi.new config
54
54
  end
55
55
 
56
+ # Access to cards controller.
57
+ # @return [CardsApi] Returns the controller instance.
58
+ def cards
59
+ @cards ||= CardsApi.new config
60
+ end
61
+
56
62
  # Access to cash_drawers controller.
57
63
  # @return [CashDrawersApi] Returns the controller instance.
58
64
  def cash_drawers
@@ -101,6 +107,18 @@ module Square
101
107
  @employees ||= EmployeesApi.new config
102
108
  end
103
109
 
110
+ # Access to gift_cards controller.
111
+ # @return [GiftCardsApi] Returns the controller instance.
112
+ def gift_cards
113
+ @gift_cards ||= GiftCardsApi.new config
114
+ end
115
+
116
+ # Access to gift_card_activities controller.
117
+ # @return [GiftCardActivitiesApi] Returns the controller instance.
118
+ def gift_card_activities
119
+ @gift_card_activities ||= GiftCardActivitiesApi.new config
120
+ end
121
+
104
122
  # Access to inventory controller.
105
123
  # @return [InventoryApi] Returns the controller instance.
106
124
  def inventory
@@ -167,6 +185,18 @@ module Square
167
185
  @refunds ||= RefundsApi.new config
168
186
  end
169
187
 
188
+ # Access to sites controller.
189
+ # @return [SitesApi] Returns the controller instance.
190
+ def sites
191
+ @sites ||= SitesApi.new config
192
+ end
193
+
194
+ # Access to snippets controller.
195
+ # @return [SnippetsApi] Returns the controller instance.
196
+ def snippets
197
+ @snippets ||= SnippetsApi.new config
198
+ end
199
+
170
200
  # Access to subscriptions controller.
171
201
  # @return [SubscriptionsApi] Returns the controller instance.
172
202
  def subscriptions
@@ -186,14 +216,18 @@ module Square
186
216
  end
187
217
 
188
218
  def initialize(timeout: 60, max_retries: 0, retry_interval: 1,
189
- backoff_factor: 1, environment: 'production',
219
+ backoff_factor: 2,
220
+ retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
221
+ retry_methods: %i[get put], environment: 'production',
190
222
  custom_url: 'https://connect.squareup.com',
191
- square_version: '2021-04-21', access_token: 'TODO: Replace',
223
+ square_version: '2021-08-18', access_token: 'TODO: Replace',
192
224
  additional_headers: {}, config: nil)
193
225
  @config = if config.nil?
194
226
  Configuration.new(timeout: timeout, max_retries: max_retries,
195
227
  retry_interval: retry_interval,
196
228
  backoff_factor: backoff_factor,
229
+ retry_statuses: retry_statuses,
230
+ retry_methods: retry_methods,
197
231
  environment: environment,
198
232
  custom_url: custom_url,
199
233
  square_version: square_version,
@@ -8,6 +8,8 @@ module Square
8
8
  attr_reader :max_retries
9
9
  attr_reader :retry_interval
10
10
  attr_reader :backoff_factor
11
+ attr_reader :retry_statuses
12
+ attr_reader :retry_methods
11
13
  attr_reader :environment
12
14
  attr_reader :custom_url
13
15
  attr_reader :square_version
@@ -22,9 +24,11 @@ module Square
22
24
  end
23
25
 
24
26
  def initialize(timeout: 60, max_retries: 0, retry_interval: 1,
25
- backoff_factor: 1, environment: 'production',
27
+ backoff_factor: 2,
28
+ retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
29
+ retry_methods: %i[get put], environment: 'production',
26
30
  custom_url: 'https://connect.squareup.com',
27
- square_version: '2021-04-21', access_token: 'TODO: Replace',
31
+ square_version: '2021-08-18', access_token: 'TODO: Replace',
28
32
  additional_headers: {})
29
33
  # The value to use for connection timeout
30
34
  @timeout = timeout
@@ -39,6 +43,12 @@ module Square
39
43
  # by in order to provide backoff
40
44
  @backoff_factor = backoff_factor
41
45
 
46
+ # A list of HTTP statuses to retry
47
+ @retry_statuses = retry_statuses
48
+
49
+ # A list of HTTP methods to retry
50
+ @retry_methods = retry_methods
51
+
42
52
  # Current API environment
43
53
  @environment = String(environment)
44
54
 
@@ -59,13 +69,15 @@ module Square
59
69
  end
60
70
 
61
71
  def clone_with(timeout: nil, max_retries: nil, retry_interval: nil,
62
- backoff_factor: nil, environment: nil, custom_url: nil,
63
- square_version: nil, access_token: nil,
64
- additional_headers: nil)
72
+ backoff_factor: nil, retry_statuses: nil, retry_methods: nil,
73
+ environment: nil, custom_url: nil, square_version: nil,
74
+ access_token: nil, additional_headers: nil)
65
75
  timeout ||= self.timeout
66
76
  max_retries ||= self.max_retries
67
77
  retry_interval ||= self.retry_interval
68
78
  backoff_factor ||= self.backoff_factor
79
+ retry_statuses ||= self.retry_statuses
80
+ retry_methods ||= self.retry_methods
69
81
  environment ||= self.environment
70
82
  custom_url ||= self.custom_url
71
83
  square_version ||= self.square_version
@@ -75,8 +87,9 @@ module Square
75
87
  Configuration.new(timeout: timeout, max_retries: max_retries,
76
88
  retry_interval: retry_interval,
77
89
  backoff_factor: backoff_factor,
78
- environment: environment, custom_url: custom_url,
79
- square_version: square_version,
90
+ retry_statuses: retry_statuses,
91
+ retry_methods: retry_methods, environment: environment,
92
+ custom_url: custom_url, square_version: square_version,
80
93
  access_token: access_token,
81
94
  additional_headers: additional_headers)
82
95
  end
@@ -84,7 +97,9 @@ module Square
84
97
  def create_http_client
85
98
  FaradayClient.new(timeout: timeout, max_retries: max_retries,
86
99
  retry_interval: retry_interval,
87
- backoff_factor: backoff_factor)
100
+ backoff_factor: backoff_factor,
101
+ retry_statuses: retry_statuses,
102
+ retry_methods: retry_methods)
88
103
  end
89
104
 
90
105
  # All the environments the SDK can run in.
@@ -6,7 +6,8 @@ module Square
6
6
  class FaradayClient < HttpClient
7
7
  # The constructor.
8
8
  def initialize(timeout:, max_retries:, retry_interval:,
9
- backoff_factor:, cache: false, verify: true)
9
+ backoff_factor:, retry_statuses:, retry_methods:,
10
+ cache: false, verify: true)
10
11
  @connection = Faraday.new do |faraday|
11
12
  faraday.use Faraday::HttpCache, serializer: Marshal if cache
12
13
  faraday.use FaradayMiddleware::FollowRedirects
@@ -16,10 +17,12 @@ module Square
16
17
  faraday.ssl[:ca_file] = Certifi.where
17
18
  faraday.ssl[:verify] = verify
18
19
  faraday.request :retry, max: max_retries, interval: retry_interval,
19
- backoff_factor: backoff_factor
20
+ backoff_factor: backoff_factor,
21
+ retry_statuses: retry_statuses,
22
+ methods: retry_methods
20
23
  faraday.adapter Faraday.default_adapter
21
24
  faraday.options[:params_encoder] = Faraday::FlatParamsEncoder
22
- faraday.options[:timeout] = timeout if timeout > 0
25
+ faraday.options[:timeout] = timeout if timeout.positive?
23
26
  end
24
27
  end
25
28
 
@@ -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