square.rb 3.20190624.0 → 3.20190814.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +10 -28
  3. data/README.md +281 -17235
  4. data/lib/square/api/apple_pay_api.rb +55 -54
  5. data/lib/square/api/base_api.rb +45 -45
  6. data/lib/square/api/catalog_api.rb +461 -460
  7. data/lib/square/api/checkout_api.rb +54 -54
  8. data/lib/square/api/customers_api.rb +335 -334
  9. data/lib/square/api/employees_api.rb +91 -91
  10. data/lib/square/api/inventory_api.rb +300 -298
  11. data/lib/square/api/labor_api.rb +558 -558
  12. data/lib/square/api/locations_api.rb +45 -45
  13. data/lib/square/api/mobile_authorization_api.rb +56 -56
  14. data/lib/square/api/o_auth_api.rb +168 -164
  15. data/lib/square/api/orders_api.rb +269 -151
  16. data/lib/square/api/payments_api.rb +282 -0
  17. data/lib/square/api/refunds_api.rb +149 -0
  18. data/lib/square/api/reporting_api.rb +143 -139
  19. data/lib/square/api/transactions_api.rb +379 -360
  20. data/lib/square/api/v1_employees_api.rb +720 -720
  21. data/lib/square/api/v1_items_api.rb +1651 -1651
  22. data/lib/square/api/v1_locations_api.rb +67 -67
  23. data/lib/square/api/v1_locations_api.rbe +67 -0
  24. data/lib/square/api/v1_transactions_api.rb +574 -574
  25. data/lib/square/api_helper.rb +281 -277
  26. data/lib/square/client.rb +139 -127
  27. data/lib/square/configuration.rb +88 -85
  28. data/lib/square/exceptions/api_exception.rb +20 -20
  29. data/lib/square/http/api_response.rb +50 -50
  30. data/lib/square/http/auth/o_auth2.rb +17 -17
  31. data/lib/square/http/faraday_client.rb +64 -64
  32. data/lib/square/http/http_call_back.rb +24 -24
  33. data/lib/square/http/http_client.rb +104 -104
  34. data/lib/square/http/http_method_enum.rb +13 -13
  35. data/lib/square/http/http_request.rb +50 -50
  36. data/lib/square/http/http_response.rb +29 -29
  37. data/lib/square.rb +52 -50
  38. data/spec/user_journey_spec.rb +4 -1
  39. data/test/api/api_test_base.rb +2 -1
  40. data/test/api/test_catalog_api.rb +59 -0
  41. data/test/api/test_employees_api.rb +36 -0
  42. data/test/api/test_locations_api.rb +35 -0
  43. data/test/api/test_payments_api.rb +42 -0
  44. data/test/api/test_refunds_api.rb +41 -0
  45. metadata +19 -11
@@ -1,277 +1,281 @@
1
- # square
2
- #
3
- # This file was automatically generated by APIMATIC v2.0
4
- # ( https://apimatic.io ).
5
-
6
- module Square
7
- # API utility class
8
- class APIHelper
9
- # Serializes an array parameter (creates key value pairs).
10
- # @param [String] The name of the parameter.
11
- # @param [Array] The value of the parameter.
12
- # @param [String] The format of the serialization.
13
- def self.serialize_array(key, array, formatting: 'indexed')
14
- tuples = []
15
-
16
- if formatting == 'unindexed'
17
- tuples += array.map { |element| ["#{key}[]", element] }
18
- elsif formatting == 'indexed'
19
- tuples += array.map.with_index do |element, index|
20
- ["#{key}[#{index}]", element]
21
- end
22
- elsif formatting == 'plain'
23
- tuples += array.map { |element| [key, element] }
24
- else
25
- raise ArgumentError, 'Invalid format provided.'
26
- end
27
- tuples
28
- end
29
-
30
- # Replaces template parameters in the given url.
31
- # @param [String] The query string builder to replace the template
32
- # parameters.
33
- # @param [Hash] The parameters to replace in the url.
34
- def self.append_url_with_template_parameters(query_builder, parameters)
35
- # perform parameter validation
36
- unless query_builder.instance_of? String
37
- raise ArgumentError, 'Given value for parameter \"query_builder\" is
38
- invalid.'
39
- end
40
-
41
- # Return if there are no parameters to replace.
42
- return query_builder if parameters.nil?
43
-
44
- # Iterate and append parameters.
45
- parameters.each do |key, value|
46
- replace_value = ''
47
-
48
- if value.nil?
49
- replace_value = ''
50
- elsif value.instance_of? Array
51
- value.map! { |element| CGI.escape(element.to_s) }
52
- replace_value = value.join('/')
53
- else
54
- replace_value = CGI.escape(value.to_s)
55
- end
56
-
57
- # Find the template parameter and replace it with its value.
58
- query_builder = query_builder.gsub('{' + key.to_s + '}', replace_value)
59
- end
60
- query_builder
61
- end
62
-
63
- # Appends the given set of parameters to the given query string.
64
- # @param [String] The query string builder to add the query parameters to.
65
- # @param [Hash] The parameters to append.
66
- # @param [String] The format of array parameter serialization.
67
- def self.append_url_with_query_parameters(query_builder, parameters,
68
- array_serialization: 'indexed')
69
- # Perform parameter validation.
70
- unless query_builder.instance_of? String
71
- raise ArgumentError, 'Given value for parameter \"query_builder\"
72
- is invalid.'
73
- end
74
-
75
- # Return if there are no parameters to replace.
76
- return query_builder if parameters.nil?
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
- query_builder += if array_serialization == 'csv'
84
- "#{seperator}#{key}=#{value.map do |element|
85
- CGI.escape(element.to_s)
86
- end.join(',')}"
87
- elsif array_serialization == 'psv'
88
- "#{seperator}#{key}=#{value.map do |element|
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
101
- else
102
- query_builder += "#{seperator}#{key}=#{CGI.escape(value.to_s)}"
103
- end
104
- end
105
- end
106
- query_builder
107
- end
108
-
109
- # Validates and processes the given Url.
110
- # @param [String] The given Url to process.
111
- # @return [String] Pre-processed Url as string.
112
- def self.clean_url(url)
113
- # Perform parameter validation.
114
- raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
115
-
116
- # Ensure that the urls are absolute.
117
- matches = url.match(%r{^(https?:\/\/[^\/]+)})
118
- raise ArgumentError, 'Invalid Url format.' if matches.nil?
119
-
120
- # Get the http protocol match.
121
- protocol = matches[1]
122
-
123
- # Check if parameters exist.
124
- index = url.index('?')
125
-
126
- # Remove redundant forward slashes.
127
- query = url[protocol.length...(!index.nil? ? index : url.length)]
128
- query.gsub!(%r{\/\/+}, '/')
129
-
130
- # Get the parameters.
131
- parameters = !index.nil? ? url[url.index('?')...url.length] : ''
132
-
133
- # Return processed url.
134
- protocol + query + parameters
135
- end
136
-
137
- # Parses JSON string.
138
- # @param [String] A JSON string.
139
- def self.json_deserialize(json)
140
- return JSON.parse(json, symbolize_names: true)
141
- rescue StandardError
142
- raise TypeError, 'Server responded with invalid JSON.'
143
- end
144
-
145
- # Removes elements with empty values from a hash.
146
- # @param [Hash] The hash to clean.
147
- def self.clean_hash(hash)
148
- hash.delete_if { |_key, value| value.to_s.strip.empty? }
149
- end
150
-
151
- # Form encodes a hash of parameters.
152
- # @param [Hash] The hash of parameters to encode.
153
- # @return [Hash] A hash with the same parameters form encoded.
154
- def self.form_encode_parameters(form_parameters,
155
- array_serialization: 'indexed')
156
- encoded = {}
157
- form_parameters.each do |key, value|
158
- encoded.merge!(APIHelper.form_encode(value, key, formatting:
159
- array_serialization))
160
- end
161
- encoded
162
- end
163
-
164
- def self.custom_merge(a, b)
165
- x = {}
166
- a.each do |key, value_a|
167
- b.each do |k, value_b|
168
- next unless key == k
169
- x[k] = []
170
- if value_a.instance_of? Array
171
- value_a.each do |v|
172
- x[k].push(v)
173
- end
174
- else
175
- x[k].push(value_a)
176
- end
177
- if value_b.instance_of? Array
178
- value_b.each do |v|
179
- x[k].push(v)
180
- end
181
- else
182
- x[k].push(value_b)
183
- end
184
- a.delete(k)
185
- b.delete(k)
186
- end
187
- end
188
- x.merge!(a)
189
- x.merge!(b)
190
- x
191
- end
192
-
193
- # Form encodes an object.
194
- # @param [Dynamic] An object to form encode.
195
- # @param [String] The name of the object.
196
- # @return [Hash] A form encoded representation of the object in the form
197
- # of a hash.
198
- def self.form_encode(obj, instance_name, formatting: 'indexed')
199
- retval = {}
200
-
201
- serializable_types = [String, Numeric, TrueClass,
202
- FalseClass, Date, DateTime]
203
-
204
- # Create a form encoded hash for this object.
205
- if obj.nil?
206
- nil
207
- elsif obj.instance_of? Array
208
- if formatting == 'indexed'
209
- obj.each_with_index do |value, index|
210
- retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
211
- index.to_s + ']'))
212
- end
213
- elsif serializable_types.map { |x| obj[0].is_a? x }.any?
214
- obj.each do |value|
215
- abc = if formatting == 'unindexed'
216
- APIHelper.form_encode(value, instance_name + '[]',
217
- formatting: formatting)
218
- else
219
- APIHelper.form_encode(value, instance_name,
220
- formatting: formatting)
221
- end
222
- retval = APIHelper.custom_merge(retval, abc)
223
- end
224
- else
225
- obj.each_with_index do |value, index|
226
- retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
227
- index.to_s + ']', formatting: formatting))
228
- end
229
- end
230
- elsif obj.instance_of? Hash
231
- obj.each do |key, value|
232
- retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
233
- key.to_s + ']', formatting: formatting))
234
- end
235
- else
236
- retval[instance_name] = obj
237
- end
238
- retval
239
- end
240
-
241
- # Retrieves a field from a Hash/Array based on an Array of keys/indexes
242
- # @param [Hash, Array] The hash to extract data from
243
- # @param [Array<String, Integer>] The keys/indexes to use
244
- # @return [Object] The extracted value
245
- def self.map_response(obj, keys)
246
- val = obj
247
- begin
248
- keys.each do |key|
249
- val = if val.is_a? Array
250
- if key.to_i.to_s == key
251
- val[key.to_i]
252
- else
253
- val = nil
254
- end
255
- else
256
- val.fetch(key.to_sym)
257
- end
258
- end
259
- rescue NoMethodError, TypeError, IndexError
260
- val = nil
261
- end
262
- val
263
- end
264
-
265
- # Safely converts a string into an rfc3339 DateTime object
266
- # @param [String] The datetime string
267
- # @return [DateTime] A DateTime object of rfc3339 format
268
- def self.rfc3339(date_time)
269
- # missing timezone information
270
- if date_time.end_with?('Z') || date_time.index('+')
271
- DateTime.rfc3339(date_time)
272
- else
273
- DateTime.rfc3339(date_time + 'Z')
274
- end
275
- end
276
- end
277
- end
1
+ # square
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module Square
7
+ # API utility class
8
+ class APIHelper
9
+ # Serializes an array parameter (creates key value pairs).
10
+ # @param [String] The name of the parameter.
11
+ # @param [Array] The value of the parameter.
12
+ # @param [String] The format of the serialization.
13
+ def self.serialize_array(key, array, formatting: 'indexed')
14
+ tuples = []
15
+
16
+ if formatting == 'unindexed'
17
+ tuples += array.map { |element| ["#{key}[]", element] }
18
+ elsif formatting == 'indexed'
19
+ tuples += array.map.with_index do |element, index|
20
+ ["#{key}[#{index}]", element]
21
+ end
22
+ elsif formatting == 'plain'
23
+ tuples += array.map { |element| [key, element] }
24
+ else
25
+ raise ArgumentError, 'Invalid format provided.'
26
+ end
27
+ tuples
28
+ end
29
+
30
+ # Replaces template parameters in the given url.
31
+ # @param [String] The query string builder to replace the template
32
+ # parameters.
33
+ # @param [Hash] The parameters to replace in the url.
34
+ def self.append_url_with_template_parameters(query_builder, parameters)
35
+ # perform parameter validation
36
+ unless query_builder.instance_of? String
37
+ raise ArgumentError, 'Given value for parameter \"query_builder\" is
38
+ invalid.'
39
+ end
40
+
41
+ # Return if there are no parameters to replace.
42
+ return query_builder if parameters.nil?
43
+
44
+ # Iterate and append parameters.
45
+ parameters.each do |key, value|
46
+ replace_value = ''
47
+
48
+ if value.nil?
49
+ replace_value = ''
50
+ elsif value.instance_of? Array
51
+ value.map! { |element| CGI.escape(element.to_s) }
52
+ replace_value = value.join('/')
53
+ else
54
+ replace_value = CGI.escape(value.to_s)
55
+ end
56
+
57
+ # Find the template parameter and replace it with its value.
58
+ query_builder = query_builder.gsub('{' + key.to_s + '}', replace_value)
59
+ end
60
+ query_builder
61
+ end
62
+
63
+ # Appends the given set of parameters to the given query string.
64
+ # @param [String] The query string builder to add the query parameters to.
65
+ # @param [Hash] The parameters to append.
66
+ # @param [String] The format of array parameter serialization.
67
+ def self.append_url_with_query_parameters(query_builder, parameters,
68
+ array_serialization: 'indexed')
69
+ # Perform parameter validation.
70
+ unless query_builder.instance_of? String
71
+ raise ArgumentError, 'Given value for parameter \"query_builder\"
72
+ is invalid.'
73
+ end
74
+
75
+ # Return if there are no parameters to replace.
76
+ return query_builder if parameters.nil?
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
+ query_builder += if array_serialization == 'csv'
84
+ "#{seperator}#{key}=#{value.map do |element|
85
+ CGI.escape(element.to_s)
86
+ end.join(',')}"
87
+ elsif array_serialization == 'psv'
88
+ "#{seperator}#{key}=#{value.map do |element|
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
101
+ else
102
+ query_builder += "#{seperator}#{key}=#{CGI.escape(value.to_s)}"
103
+ end
104
+ end
105
+ end
106
+ query_builder
107
+ end
108
+
109
+ # Validates and processes the given Url.
110
+ # @param [String] The given Url to process.
111
+ # @return [String] Pre-processed Url as string.
112
+ def self.clean_url(url)
113
+ # Perform parameter validation.
114
+ raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
115
+
116
+ # Ensure that the urls are absolute.
117
+ matches = url.match(%r{^(https?:\/\/[^\/]+)})
118
+ raise ArgumentError, 'Invalid Url format.' if matches.nil?
119
+
120
+ # Get the http protocol match.
121
+ protocol = matches[1]
122
+
123
+ # Check if parameters exist.
124
+ index = url.index('?')
125
+
126
+ # Remove redundant forward slashes.
127
+ query = url[protocol.length...(!index.nil? ? index : url.length)]
128
+ query.gsub!(%r{\/\/+}, '/')
129
+
130
+ # Get the parameters.
131
+ parameters = !index.nil? ? url[url.index('?')...url.length] : ''
132
+
133
+ # Return processed url.
134
+ protocol + query + parameters
135
+ end
136
+
137
+ # Parses JSON string.
138
+ # @param [String] A JSON string.
139
+ def self.json_deserialize(json)
140
+ return JSON.parse(json, symbolize_names: true)
141
+ rescue StandardError
142
+ raise TypeError, 'Server responded with invalid JSON.'
143
+ end
144
+
145
+ # Removes elements with empty values from a hash.
146
+ # @param [Hash] The hash to clean.
147
+ def self.clean_hash(hash)
148
+ hash.delete_if { |_key, value| value.to_s.strip.empty? }
149
+ end
150
+
151
+ # Form encodes a hash of parameters.
152
+ # @param [Hash] The hash of parameters to encode.
153
+ # @return [Hash] A hash with the same parameters form encoded.
154
+ def self.form_encode_parameters(form_parameters,
155
+ array_serialization: 'indexed')
156
+ encoded = {}
157
+ form_parameters.each do |key, value|
158
+ encoded.merge!(APIHelper.form_encode(value, key, formatting:
159
+ array_serialization))
160
+ end
161
+ encoded
162
+ end
163
+
164
+ def self.custom_merge(a, b)
165
+ x = {}
166
+ a.each do |key, value_a|
167
+ b.each do |k, value_b|
168
+ next unless key == k
169
+ x[k] = []
170
+ if value_a.instance_of? Array
171
+ value_a.each do |v|
172
+ x[k].push(v)
173
+ end
174
+ else
175
+ x[k].push(value_a)
176
+ end
177
+ if value_b.instance_of? Array
178
+ value_b.each do |v|
179
+ x[k].push(v)
180
+ end
181
+ else
182
+ x[k].push(value_b)
183
+ end
184
+ a.delete(k)
185
+ b.delete(k)
186
+ end
187
+ end
188
+ x.merge!(a)
189
+ x.merge!(b)
190
+ x
191
+ end
192
+
193
+ # Form encodes an object.
194
+ # @param [Dynamic] An object to form encode.
195
+ # @param [String] The name of the object.
196
+ # @return [Hash] A form encoded representation of the object in the form
197
+ # of a hash.
198
+ def self.form_encode(obj, instance_name, formatting: 'indexed')
199
+ retval = {}
200
+
201
+ serializable_types = [String, Numeric, TrueClass,
202
+ FalseClass, Date, DateTime]
203
+
204
+ # Create a form encoded hash for this object.
205
+ if obj.nil?
206
+ nil
207
+ elsif obj.instance_of? Array
208
+ if formatting == 'indexed'
209
+ obj.each_with_index do |value, index|
210
+ retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
211
+ index.to_s + ']'))
212
+ end
213
+ elsif serializable_types.map { |x| obj[0].is_a? x }.any?
214
+ obj.each do |value|
215
+ abc = if formatting == 'unindexed'
216
+ APIHelper.form_encode(value, instance_name + '[]',
217
+ formatting: formatting)
218
+ else
219
+ APIHelper.form_encode(value, instance_name,
220
+ formatting: formatting)
221
+ end
222
+ retval = APIHelper.custom_merge(retval, abc)
223
+ end
224
+ else
225
+ obj.each_with_index do |value, index|
226
+ retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
227
+ index.to_s + ']', formatting: formatting))
228
+ end
229
+ end
230
+ elsif obj.instance_of? Hash
231
+ obj.each do |key, value|
232
+ retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
233
+ key.to_s + ']', formatting: formatting))
234
+ end
235
+ elsif obj.instance_of? File
236
+ retval[instance_name] = UploadIO.new(
237
+ obj, 'application/octet-stream', File.basename(obj.path)
238
+ )
239
+ else
240
+ retval[instance_name] = obj
241
+ end
242
+ retval
243
+ end
244
+
245
+ # Retrieves a field from a Hash/Array based on an Array of keys/indexes
246
+ # @param [Hash, Array] The hash to extract data from
247
+ # @param [Array<String, Integer>] The keys/indexes to use
248
+ # @return [Object] The extracted value
249
+ def self.map_response(obj, keys)
250
+ val = obj
251
+ begin
252
+ keys.each do |key|
253
+ val = if val.is_a? Array
254
+ if key.to_i.to_s == key
255
+ val[key.to_i]
256
+ else
257
+ val = nil
258
+ end
259
+ else
260
+ val.fetch(key.to_sym)
261
+ end
262
+ end
263
+ rescue NoMethodError, TypeError, IndexError
264
+ val = nil
265
+ end
266
+ val
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
+ end
281
+ end