fangkuai.rb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +10 -0
  3. data/README.md +1 -0
  4. data/lib/square.rb +61 -0
  5. data/lib/square/api/apple_pay_api.rb +50 -0
  6. data/lib/square/api/bank_accounts_api.rb +136 -0
  7. data/lib/square/api/base_api.rb +43 -0
  8. data/lib/square/api/cash_drawers_api.rb +150 -0
  9. data/lib/square/api/catalog_api.rb +572 -0
  10. data/lib/square/api/checkout_api.rb +49 -0
  11. data/lib/square/api/customer_groups_api.rb +182 -0
  12. data/lib/square/api/customer_segments_api.rb +78 -0
  13. data/lib/square/api/customers_api.rb +418 -0
  14. data/lib/square/api/devices_api.rb +120 -0
  15. data/lib/square/api/disputes_api.rb +398 -0
  16. data/lib/square/api/employees_api.rb +87 -0
  17. data/lib/square/api/inventory_api.rb +296 -0
  18. data/lib/square/api/invoices_api.rb +358 -0
  19. data/lib/square/api/labor_api.rb +630 -0
  20. data/lib/square/api/locations_api.rb +151 -0
  21. data/lib/square/api/loyalty_api.rb +543 -0
  22. data/lib/square/api/merchants_api.rb +83 -0
  23. data/lib/square/api/mobile_authorization_api.rb +52 -0
  24. data/lib/square/api/o_auth_api.rb +163 -0
  25. data/lib/square/api/orders_api.rb +280 -0
  26. data/lib/square/api/payments_api.rb +279 -0
  27. data/lib/square/api/refunds_api.rb +145 -0
  28. data/lib/square/api/subscriptions_api.rb +251 -0
  29. data/lib/square/api/team_api.rb +326 -0
  30. data/lib/square/api/terminal_api.rb +141 -0
  31. data/lib/square/api/transactions_api.rb +369 -0
  32. data/lib/square/api/v1_employees_api.rb +723 -0
  33. data/lib/square/api/v1_items_api.rb +1686 -0
  34. data/lib/square/api/v1_locations_api.rb +65 -0
  35. data/lib/square/api/v1_transactions_api.rb +572 -0
  36. data/lib/square/api_helper.rb +276 -0
  37. data/lib/square/client.rb +211 -0
  38. data/lib/square/configuration.rb +101 -0
  39. data/lib/square/exceptions/api_exception.rb +15 -0
  40. data/lib/square/http/api_response.rb +45 -0
  41. data/lib/square/http/auth/o_auth2.rb +12 -0
  42. data/lib/square/http/faraday_client.rb +55 -0
  43. data/lib/square/http/http_call_back.rb +19 -0
  44. data/lib/square/http/http_client.rb +99 -0
  45. data/lib/square/http/http_method_enum.rb +8 -0
  46. data/lib/square/http/http_request.rb +45 -0
  47. data/lib/square/http/http_response.rb +24 -0
  48. data/lib/square/utilities/file_wrapper.rb +12 -0
  49. data/spec/user_journey_spec.rb +148 -0
  50. data/test/api/api_test_base.rb +24 -0
  51. data/test/api/test_catalog_api.rb +59 -0
  52. data/test/api/test_customers_api.rb +45 -0
  53. data/test/api/test_employees_api.rb +36 -0
  54. data/test/api/test_labor_api.rb +74 -0
  55. data/test/api/test_locations_api.rb +35 -0
  56. data/test/api/test_merchants_api.rb +40 -0
  57. data/test/api/test_payments_api.rb +42 -0
  58. data/test/api/test_refunds_api.rb +41 -0
  59. data/test/http_response_catcher.rb +19 -0
  60. data/test/test_helper.rb +94 -0
  61. metadata +199 -0
@@ -0,0 +1,276 @@
1
+ module Square
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
+ if formatting == 'unindexed'
12
+ tuples += array.map { |element| ["#{key}[]", element] }
13
+ elsif formatting == 'indexed'
14
+ tuples += array.map.with_index do |element, index|
15
+ ["#{key}[#{index}]", element]
16
+ end
17
+ elsif formatting == 'plain'
18
+ tuples += array.map { |element| [key, element] }
19
+ else
20
+ raise ArgumentError, 'Invalid format provided.'
21
+ end
22
+ tuples
23
+ end
24
+
25
+ # Replaces template parameters in the given url.
26
+ # @param [String] The query string builder to replace the template
27
+ # parameters.
28
+ # @param [Hash] The parameters to replace in the url.
29
+ def self.append_url_with_template_parameters(query_builder, parameters)
30
+ # perform parameter validation
31
+ unless query_builder.instance_of? String
32
+ raise ArgumentError, 'Given value for parameter \"query_builder\" is
33
+ invalid.'
34
+ end
35
+
36
+ # Return if there are no parameters to replace.
37
+ return query_builder if parameters.nil?
38
+
39
+ # Iterate and append parameters.
40
+ parameters.each do |key, value|
41
+ replace_value = ''
42
+
43
+ if value.nil?
44
+ replace_value = ''
45
+ elsif value.instance_of? Array
46
+ value.map! { |element| CGI.escape(element.to_s) }
47
+ replace_value = value.join('/')
48
+ else
49
+ replace_value = CGI.escape(value.to_s)
50
+ end
51
+
52
+ # Find the template parameter and replace it with its value.
53
+ query_builder = query_builder.gsub('{' + key.to_s + '}', replace_value)
54
+ end
55
+ query_builder
56
+ end
57
+
58
+ # Appends the given set of parameters to the given query string.
59
+ # @param [String] The query string builder to add the query parameters to.
60
+ # @param [Hash] The parameters to append.
61
+ # @param [String] The format of array parameter serialization.
62
+ def self.append_url_with_query_parameters(query_builder, parameters,
63
+ array_serialization: 'indexed')
64
+ # Perform parameter validation.
65
+ unless query_builder.instance_of? String
66
+ raise ArgumentError, 'Given value for parameter \"query_builder\"
67
+ is invalid.'
68
+ end
69
+
70
+ # Return if there are no parameters to replace.
71
+ return query_builder if parameters.nil?
72
+
73
+ parameters.each do |key, value|
74
+ seperator = query_builder.include?('?') ? '&' : '?'
75
+ unless value.nil?
76
+ if value.instance_of? Array
77
+ value.compact!
78
+ query_builder += if array_serialization == 'csv'
79
+ "#{seperator}#{key}=#{value.map do |element|
80
+ CGI.escape(element.to_s)
81
+ end.join(',')}"
82
+ elsif array_serialization == 'psv'
83
+ "#{seperator}#{key}=#{value.map do |element|
84
+ CGI.escape(element.to_s)
85
+ end.join('|')}"
86
+ elsif array_serialization == 'tsv'
87
+ "#{seperator}#{key}=#{value.map do |element|
88
+ CGI.escape(element.to_s)
89
+ end.join("\t")}"
90
+ else
91
+ "#{seperator}#{APIHelper.serialize_array(
92
+ key, value, formatting: array_serialization
93
+ ).map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }
94
+ .join('&')}"
95
+ end
96
+ else
97
+ query_builder += "#{seperator}#{key}=#{CGI.escape(value.to_s)}"
98
+ end
99
+ end
100
+ end
101
+ query_builder
102
+ end
103
+
104
+ # Validates and processes the given Url.
105
+ # @param [String] The given Url to process.
106
+ # @return [String] Pre-processed Url as string.
107
+ def self.clean_url(url)
108
+ # Perform parameter validation.
109
+ raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
110
+
111
+ # Ensure that the urls are absolute.
112
+ matches = url.match(%r{^(https?:\/\/[^\/]+)})
113
+ raise ArgumentError, 'Invalid Url format.' if matches.nil?
114
+
115
+ # Get the http protocol match.
116
+ protocol = matches[1]
117
+
118
+ # Check if parameters exist.
119
+ index = url.index('?')
120
+
121
+ # Remove redundant forward slashes.
122
+ query = url[protocol.length...(!index.nil? ? index : url.length)]
123
+ query.gsub!(%r{\/\/+}, '/')
124
+
125
+ # Get the parameters.
126
+ parameters = !index.nil? ? url[url.index('?')...url.length] : ''
127
+
128
+ # Return processed url.
129
+ protocol + query + parameters
130
+ end
131
+
132
+ # Parses JSON string.
133
+ # @param [String] A JSON string.
134
+ def self.json_deserialize(json)
135
+ return JSON.parse(json, symbolize_names: true)
136
+ rescue StandardError
137
+ raise TypeError, 'Server responded with invalid JSON.'
138
+ end
139
+
140
+ # Removes elements with empty values from a hash.
141
+ # @param [Hash] The hash to clean.
142
+ def self.clean_hash(hash)
143
+ hash.delete_if { |_key, value| value.to_s.strip.empty? }
144
+ end
145
+
146
+ # Form encodes a hash of parameters.
147
+ # @param [Hash] The hash of parameters to encode.
148
+ # @return [Hash] A hash with the same parameters form encoded.
149
+ def self.form_encode_parameters(form_parameters,
150
+ array_serialization: 'indexed')
151
+ encoded = {}
152
+ form_parameters.each do |key, value|
153
+ encoded.merge!(APIHelper.form_encode(value, key, formatting:
154
+ array_serialization))
155
+ end
156
+ encoded
157
+ end
158
+
159
+ def self.custom_merge(a, b)
160
+ x = {}
161
+ a.each do |key, value_a|
162
+ b.each do |k, value_b|
163
+ next unless key == k
164
+ x[k] = []
165
+ if value_a.instance_of? Array
166
+ value_a.each do |v|
167
+ x[k].push(v)
168
+ end
169
+ else
170
+ x[k].push(value_a)
171
+ end
172
+ if value_b.instance_of? Array
173
+ value_b.each do |v|
174
+ x[k].push(v)
175
+ end
176
+ else
177
+ x[k].push(value_b)
178
+ end
179
+ a.delete(k)
180
+ b.delete(k)
181
+ end
182
+ end
183
+ x.merge!(a)
184
+ x.merge!(b)
185
+ x
186
+ end
187
+
188
+ # Form encodes an object.
189
+ # @param [Dynamic] An object to form encode.
190
+ # @param [String] The name of the object.
191
+ # @return [Hash] A form encoded representation of the object in the form
192
+ # of a hash.
193
+ def self.form_encode(obj, instance_name, formatting: 'indexed')
194
+ retval = {}
195
+
196
+ serializable_types = [String, Numeric, TrueClass,
197
+ FalseClass, Date, DateTime]
198
+
199
+ # Create a form encoded hash for this object.
200
+ if obj.nil?
201
+ nil
202
+ elsif obj.instance_of? Array
203
+ if formatting == 'indexed'
204
+ obj.each_with_index do |value, index|
205
+ retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
206
+ index.to_s + ']'))
207
+ end
208
+ elsif serializable_types.map { |x| obj[0].is_a? x }.any?
209
+ obj.each do |value|
210
+ abc = if formatting == 'unindexed'
211
+ APIHelper.form_encode(value, instance_name + '[]',
212
+ formatting: formatting)
213
+ else
214
+ APIHelper.form_encode(value, instance_name,
215
+ formatting: formatting)
216
+ end
217
+ retval = APIHelper.custom_merge(retval, abc)
218
+ end
219
+ else
220
+ obj.each_with_index do |value, index|
221
+ retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
222
+ index.to_s + ']', formatting: formatting))
223
+ end
224
+ end
225
+ elsif obj.instance_of? Hash
226
+ obj.each do |key, value|
227
+ retval.merge!(APIHelper.form_encode(value, instance_name + '[' +
228
+ key.to_s + ']', formatting: formatting))
229
+ end
230
+ elsif obj.instance_of? File
231
+ retval[instance_name] = UploadIO.new(
232
+ obj, 'application/octet-stream', File.basename(obj.path)
233
+ )
234
+ else
235
+ retval[instance_name] = obj
236
+ end
237
+ retval
238
+ end
239
+
240
+ # Retrieves a field from a Hash/Array based on an Array of keys/indexes
241
+ # @param [Hash, Array] The hash to extract data from
242
+ # @param [Array<String, Integer>] The keys/indexes to use
243
+ # @return [Object] The extracted value
244
+ def self.map_response(obj, keys)
245
+ val = obj
246
+ begin
247
+ keys.each do |key|
248
+ val = if val.is_a? Array
249
+ if key.to_i.to_s == key
250
+ val[key.to_i]
251
+ else
252
+ val = nil
253
+ end
254
+ else
255
+ val.fetch(key.to_sym)
256
+ end
257
+ end
258
+ rescue NoMethodError, TypeError, IndexError
259
+ val = nil
260
+ end
261
+ val
262
+ end
263
+
264
+ # Safely converts a string into an rfc3339 DateTime object
265
+ # @param [String] The datetime string
266
+ # @return [DateTime] A DateTime object of rfc3339 format
267
+ def self.rfc3339(date_time)
268
+ # missing timezone information
269
+ if date_time.end_with?('Z') || date_time.index('+')
270
+ DateTime.rfc3339(date_time)
271
+ else
272
+ DateTime.rfc3339(date_time + 'Z')
273
+ end
274
+ end
275
+ end
276
+ end
@@ -0,0 +1,211 @@
1
+ module Square
2
+ # square client class.
3
+ class Client
4
+ attr_reader :config
5
+
6
+ def sdk_version
7
+ '6.3.0.20200826'
8
+ end
9
+
10
+ def square_version
11
+ config.square_version
12
+ end
13
+
14
+ # Access to mobile_authorization controller.
15
+ # @return [MobileAuthorizationApi] Returns the controller instance.
16
+ def mobile_authorization
17
+ @mobile_authorization ||= MobileAuthorizationApi.new config
18
+ end
19
+
20
+ # Access to o_auth controller.
21
+ # @return [OAuthApi] Returns the controller instance.
22
+ def o_auth
23
+ @o_auth ||= OAuthApi.new config
24
+ end
25
+
26
+ # Access to v1_locations controller.
27
+ # @return [V1LocationsApi] Returns the controller instance.
28
+ def v1_locations
29
+ @v1_locations ||= V1LocationsApi.new config
30
+ end
31
+
32
+ # Access to v1_employees controller.
33
+ # @return [V1EmployeesApi] Returns the controller instance.
34
+ def v1_employees
35
+ @v1_employees ||= V1EmployeesApi.new config
36
+ end
37
+
38
+ # Access to v1_transactions controller.
39
+ # @return [V1TransactionsApi] Returns the controller instance.
40
+ def v1_transactions
41
+ @v1_transactions ||= V1TransactionsApi.new config
42
+ end
43
+
44
+ # Access to v1_items controller.
45
+ # @return [V1ItemsApi] Returns the controller instance.
46
+ def v1_items
47
+ @v1_items ||= V1ItemsApi.new config
48
+ end
49
+
50
+ # Access to apple_pay controller.
51
+ # @return [ApplePayApi] Returns the controller instance.
52
+ def apple_pay
53
+ @apple_pay ||= ApplePayApi.new config
54
+ end
55
+
56
+ # Access to bank_accounts controller.
57
+ # @return [BankAccountsApi] Returns the controller instance.
58
+ def bank_accounts
59
+ @bank_accounts ||= BankAccountsApi.new config
60
+ end
61
+
62
+ # Access to cash_drawers controller.
63
+ # @return [CashDrawersApi] Returns the controller instance.
64
+ def cash_drawers
65
+ @cash_drawers ||= CashDrawersApi.new config
66
+ end
67
+
68
+ # Access to catalog controller.
69
+ # @return [CatalogApi] Returns the controller instance.
70
+ def catalog
71
+ @catalog ||= CatalogApi.new config
72
+ end
73
+
74
+ # Access to customers controller.
75
+ # @return [CustomersApi] Returns the controller instance.
76
+ def customers
77
+ @customers ||= CustomersApi.new config
78
+ end
79
+
80
+ # Access to customer_groups controller.
81
+ # @return [CustomerGroupsApi] Returns the controller instance.
82
+ def customer_groups
83
+ @customer_groups ||= CustomerGroupsApi.new config
84
+ end
85
+
86
+ # Access to customer_segments controller.
87
+ # @return [CustomerSegmentsApi] Returns the controller instance.
88
+ def customer_segments
89
+ @customer_segments ||= CustomerSegmentsApi.new config
90
+ end
91
+
92
+ # Access to devices controller.
93
+ # @return [DevicesApi] Returns the controller instance.
94
+ def devices
95
+ @devices ||= DevicesApi.new config
96
+ end
97
+
98
+ # Access to disputes controller.
99
+ # @return [DisputesApi] Returns the controller instance.
100
+ def disputes
101
+ @disputes ||= DisputesApi.new config
102
+ end
103
+
104
+ # Access to employees controller.
105
+ # @return [EmployeesApi] Returns the controller instance.
106
+ def employees
107
+ @employees ||= EmployeesApi.new config
108
+ end
109
+
110
+ # Access to inventory controller.
111
+ # @return [InventoryApi] Returns the controller instance.
112
+ def inventory
113
+ @inventory ||= InventoryApi.new config
114
+ end
115
+
116
+ # Access to invoices controller.
117
+ # @return [InvoicesApi] Returns the controller instance.
118
+ def invoices
119
+ @invoices ||= InvoicesApi.new config
120
+ end
121
+
122
+ # Access to labor controller.
123
+ # @return [LaborApi] Returns the controller instance.
124
+ def labor
125
+ @labor ||= LaborApi.new config
126
+ end
127
+
128
+ # Access to locations controller.
129
+ # @return [LocationsApi] Returns the controller instance.
130
+ def locations
131
+ @locations ||= LocationsApi.new config
132
+ end
133
+
134
+ # Access to checkout controller.
135
+ # @return [CheckoutApi] Returns the controller instance.
136
+ def checkout
137
+ @checkout ||= CheckoutApi.new config
138
+ end
139
+
140
+ # Access to transactions controller.
141
+ # @return [TransactionsApi] Returns the controller instance.
142
+ def transactions
143
+ @transactions ||= TransactionsApi.new config
144
+ end
145
+
146
+ # Access to loyalty controller.
147
+ # @return [LoyaltyApi] Returns the controller instance.
148
+ def loyalty
149
+ @loyalty ||= LoyaltyApi.new config
150
+ end
151
+
152
+ # Access to merchants controller.
153
+ # @return [MerchantsApi] Returns the controller instance.
154
+ def merchants
155
+ @merchants ||= MerchantsApi.new config
156
+ end
157
+
158
+ # Access to orders controller.
159
+ # @return [OrdersApi] Returns the controller instance.
160
+ def orders
161
+ @orders ||= OrdersApi.new config
162
+ end
163
+
164
+ # Access to payments controller.
165
+ # @return [PaymentsApi] Returns the controller instance.
166
+ def payments
167
+ @payments ||= PaymentsApi.new config
168
+ end
169
+
170
+ # Access to refunds controller.
171
+ # @return [RefundsApi] Returns the controller instance.
172
+ def refunds
173
+ @refunds ||= RefundsApi.new config
174
+ end
175
+
176
+ # Access to subscriptions controller.
177
+ # @return [SubscriptionsApi] Returns the controller instance.
178
+ def subscriptions
179
+ @subscriptions ||= SubscriptionsApi.new config
180
+ end
181
+
182
+ # Access to team controller.
183
+ # @return [TeamApi] Returns the controller instance.
184
+ def team
185
+ @team ||= TeamApi.new config
186
+ end
187
+
188
+ # Access to terminal controller.
189
+ # @return [TerminalApi] Returns the controller instance.
190
+ def terminal
191
+ @terminal ||= TerminalApi.new config
192
+ end
193
+
194
+ def initialize(timeout: 60, max_retries: 0, retry_interval: 1,
195
+ backoff_factor: 1, environment: 'production',
196
+ square_version: '2020-08-26', access_token: 'TODO: Replace',
197
+ additional_headers: {}, config: nil)
198
+ @config = if config.nil?
199
+ Configuration.new(timeout: timeout, max_retries: max_retries,
200
+ retry_interval: retry_interval,
201
+ backoff_factor: backoff_factor,
202
+ environment: environment,
203
+ square_version: square_version,
204
+ access_token: access_token,
205
+ additional_headers: additional_headers)
206
+ else
207
+ config
208
+ end
209
+ end
210
+ end
211
+ end