square_connect 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGES.md +5 -0
  3. data/CONTRIBUTING.md +5 -0
  4. data/LICENSE.txt +202 -0
  5. data/README.md +47 -0
  6. data/lib/square_connect.rb +88 -0
  7. data/lib/square_connect/api/customer_api.rb +354 -0
  8. data/lib/square_connect/api/customer_card_api.rb +163 -0
  9. data/lib/square_connect/api/location_api.rb +82 -0
  10. data/lib/square_connect/api/refund_api.rb +179 -0
  11. data/lib/square_connect/api/transaction_api.rb +387 -0
  12. data/lib/square_connect/api_client.rb +337 -0
  13. data/lib/square_connect/api_error.rb +31 -0
  14. data/lib/square_connect/configuration.rb +163 -0
  15. data/lib/square_connect/models/address.rb +288 -0
  16. data/lib/square_connect/models/capture_transaction_response.rb +160 -0
  17. data/lib/square_connect/models/card.rb +233 -0
  18. data/lib/square_connect/models/card_brand.rb +147 -0
  19. data/lib/square_connect/models/charge_request.rb +257 -0
  20. data/lib/square_connect/models/charge_response.rb +171 -0
  21. data/lib/square_connect/models/country.rb +147 -0
  22. data/lib/square_connect/models/create_customer_card_request.rb +180 -0
  23. data/lib/square_connect/models/create_customer_card_response.rb +171 -0
  24. data/lib/square_connect/models/create_customer_request.rb +246 -0
  25. data/lib/square_connect/models/create_customer_response.rb +171 -0
  26. data/lib/square_connect/models/create_refund_request.rb +191 -0
  27. data/lib/square_connect/models/create_refund_response.rb +171 -0
  28. data/lib/square_connect/models/currency.rb +147 -0
  29. data/lib/square_connect/models/customer.rb +292 -0
  30. data/lib/square_connect/models/delete_customer_card_response.rb +160 -0
  31. data/lib/square_connect/models/delete_customer_response.rb +160 -0
  32. data/lib/square_connect/models/error.rb +209 -0
  33. data/lib/square_connect/models/error_category.rb +147 -0
  34. data/lib/square_connect/models/error_code.rb +147 -0
  35. data/lib/square_connect/models/list_customers_request.rb +158 -0
  36. data/lib/square_connect/models/list_customers_response.rb +184 -0
  37. data/lib/square_connect/models/list_locations_response.rb +173 -0
  38. data/lib/square_connect/models/list_refunds_request.rb +200 -0
  39. data/lib/square_connect/models/list_refunds_response.rb +184 -0
  40. data/lib/square_connect/models/list_transactions_request.rb +200 -0
  41. data/lib/square_connect/models/list_transactions_response.rb +184 -0
  42. data/lib/square_connect/models/location.rb +204 -0
  43. data/lib/square_connect/models/location_capability.rb +147 -0
  44. data/lib/square_connect/models/money.rb +178 -0
  45. data/lib/square_connect/models/refund.rb +255 -0
  46. data/lib/square_connect/models/refund_status.rb +147 -0
  47. data/lib/square_connect/models/retrieve_customer_response.rb +171 -0
  48. data/lib/square_connect/models/retrieve_transaction_response.rb +171 -0
  49. data/lib/square_connect/models/sort_order.rb +147 -0
  50. data/lib/square_connect/models/tender.rb +277 -0
  51. data/lib/square_connect/models/tender_card_details.rb +198 -0
  52. data/lib/square_connect/models/tender_card_details_entry_method.rb +147 -0
  53. data/lib/square_connect/models/tender_card_details_status.rb +147 -0
  54. data/lib/square_connect/models/tender_cash_details.rb +169 -0
  55. data/lib/square_connect/models/tender_type.rb +147 -0
  56. data/lib/square_connect/models/transaction.rb +237 -0
  57. data/lib/square_connect/models/transaction_product.rb +147 -0
  58. data/lib/square_connect/models/update_customer_request.rb +246 -0
  59. data/lib/square_connect/models/update_customer_response.rb +171 -0
  60. data/lib/square_connect/models/void_transaction_response.rb +160 -0
  61. data/lib/square_connect/version.rb +13 -0
  62. data/square_connect.gemspec +32 -0
  63. metadata +286 -0
@@ -0,0 +1,337 @@
1
+ =begin
2
+ Square Connect API
3
+
4
+ OpenAPI spec version: 2.0
5
+
6
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
7
+
8
+
9
+ =end
10
+
11
+ require 'date'
12
+ require 'json'
13
+ require 'logger'
14
+ require 'tempfile'
15
+ require 'typhoeus'
16
+ require 'uri'
17
+
18
+ module SquareConnect
19
+ class ApiClient
20
+ # The Configuration object holding settings to be used in the API client.
21
+ attr_accessor :config
22
+
23
+ # Defines the headers to be used in HTTP requests of all API calls by default.
24
+ #
25
+ # @return [Hash]
26
+ attr_accessor :default_headers
27
+
28
+ def initialize(config = Configuration.default)
29
+ @config = config
30
+ @user_agent = "square-connect-ruby-#{VERSION}"
31
+ @default_headers = {
32
+ 'Content-Type' => "application/json",
33
+ 'Accept' => "application/json",
34
+ 'User-Agent' => @user_agent
35
+ }
36
+ end
37
+
38
+ def self.default
39
+ @@default ||= ApiClient.new
40
+ end
41
+
42
+ # Call an API with given options.
43
+ #
44
+ # @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
45
+ # the data deserialized from response body (could be nil), response status code and response headers.
46
+ def call_api(http_method, path, opts = {})
47
+ request = build_request(http_method, path, opts)
48
+ response = request.run
49
+
50
+ if @config.debugging
51
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
52
+ end
53
+
54
+ unless response.success?
55
+ fail ApiError.new(:code => response.code,
56
+ :response_headers => response.headers,
57
+ :response_body => response.body)
58
+ end
59
+
60
+ if opts[:return_type]
61
+ data = deserialize(response, opts[:return_type])
62
+ else
63
+ data = nil
64
+ end
65
+ return data, response.code, response.headers
66
+ end
67
+
68
+ def build_request(http_method, path, opts = {})
69
+ url = build_request_url(path)
70
+ http_method = http_method.to_sym.downcase
71
+
72
+ header_params = @default_headers.merge(opts[:header_params] || {})
73
+
74
+ if header_params.has_key?(:Authorization)
75
+ if !header_params[:Authorization].start_with?("Bearer ")
76
+ header_params[:Authorization] = "Bearer " + header_params[:Authorization]
77
+ end
78
+ end
79
+
80
+ query_params = opts[:query_params] || {}
81
+ form_params = opts[:form_params] || {}
82
+
83
+
84
+
85
+ req_opts = {
86
+ :method => http_method,
87
+ :headers => header_params,
88
+ :params => query_params,
89
+ :timeout => @config.timeout,
90
+ :ssl_verifypeer => @config.verify_ssl,
91
+ :sslcert => @config.cert_file,
92
+ :sslkey => @config.key_file,
93
+ :verbose => @config.debugging
94
+ }
95
+
96
+ req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
97
+
98
+ if [:post, :patch, :put, :delete].include?(http_method)
99
+ req_body = build_request_body(header_params, form_params, opts[:body])
100
+ req_opts.update :body => req_body
101
+ if @config.debugging
102
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
103
+ end
104
+ end
105
+
106
+ Typhoeus::Request.new(url, req_opts)
107
+ end
108
+
109
+ # Check if the given MIME is a JSON MIME.
110
+ # JSON MIME examples:
111
+ # application/json
112
+ # application/json; charset=UTF8
113
+ # APPLICATION/JSON
114
+ def json_mime?(mime)
115
+ !!(mime =~ /\Aapplication\/json(;.*)?\z/i)
116
+ end
117
+
118
+ # Deserialize the response to the given return type.
119
+ #
120
+ # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
121
+ def deserialize(response, return_type)
122
+ body = response.body
123
+ return nil if body.nil? || body.empty?
124
+
125
+ # return response body directly for String return type
126
+ return body if return_type == 'String'
127
+
128
+ # handle file downloading - save response body into a tmp file and return the File instance
129
+ return download_file(response) if return_type == 'File'
130
+
131
+ # ensuring a default content type
132
+ content_type = response.headers['Content-Type'] || 'application/json'
133
+
134
+ fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
135
+
136
+ begin
137
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
138
+ rescue JSON::ParserError => e
139
+ if %w(String Date DateTime).include?(return_type)
140
+ data = body
141
+ else
142
+ raise e
143
+ end
144
+ end
145
+
146
+ convert_to_type data, return_type
147
+ end
148
+
149
+ # Convert data to the given return type.
150
+ def convert_to_type(data, return_type)
151
+ return nil if data.nil?
152
+ case return_type
153
+ when 'String'
154
+ data.to_s
155
+ when 'Integer'
156
+ data.to_i
157
+ when 'Float'
158
+ data.to_f
159
+ when 'BOOLEAN'
160
+ data == true
161
+ when 'DateTime'
162
+ # parse date time (expecting ISO 8601 format)
163
+ DateTime.parse data
164
+ when 'Date'
165
+ # parse date time (expecting ISO 8601 format)
166
+ Date.parse data
167
+ when 'Object'
168
+ # generic object, return directly
169
+ data
170
+ when /\AArray<(.+)>\z/
171
+ # e.g. Array<Pet>
172
+ sub_type = $1
173
+ data.map {|item| convert_to_type(item, sub_type) }
174
+ when /\AHash\<String, (.+)\>\z/
175
+ # e.g. Hash<String, Integer>
176
+ sub_type = $1
177
+ {}.tap do |hash|
178
+ data.each {|k, v| hash[k] = convert_to_type(v, sub_type) }
179
+ end
180
+ else
181
+ # models, e.g. Pet
182
+ SquareConnect.const_get(return_type).new.tap do |model|
183
+ model.build_from_hash data
184
+ end
185
+ end
186
+ end
187
+
188
+ # Save response body into a file in (the defined) temporary folder, using the filename
189
+ # from the "Content-Disposition" header if provided, otherwise a random filename.
190
+ #
191
+ # @see Configuration#temp_folder_path
192
+ # @return [Tempfile] the file downloaded
193
+ def download_file(response)
194
+ content_disposition = response.headers['Content-Disposition']
195
+ if content_disposition
196
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
197
+ prefix = sanitize_filename(filename)
198
+ else
199
+ prefix = 'download-'
200
+ end
201
+ prefix = prefix + '-' unless prefix.end_with?('-')
202
+
203
+ tempfile = nil
204
+ encoding = response.body.encoding
205
+ Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) do |file|
206
+ file.write(response.body)
207
+ tempfile = file
208
+ end
209
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
210
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
211
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
212
+ "explicitly with `tempfile.delete`"
213
+ tempfile
214
+ end
215
+
216
+ # Sanitize filename by removing path.
217
+ # e.g. ../../sun.gif becomes sun.gif
218
+ #
219
+ # @param [String] filename the filename to be sanitized
220
+ # @return [String] the sanitized filename
221
+ def sanitize_filename(filename)
222
+ filename.gsub /.*[\/\\]/, ''
223
+ end
224
+
225
+ def build_request_url(path)
226
+ # Add leading and trailing slashes to path
227
+ path = "/#{path}".gsub(/\/+/, '/')
228
+ URI.encode(@config.base_url + path)
229
+ end
230
+
231
+ def build_request_body(header_params, form_params, body)
232
+ # http form
233
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
234
+ header_params['Content-Type'] == 'multipart/form-data'
235
+ data = {}
236
+ form_params.each do |key, value|
237
+ case value
238
+ when File, Array, nil
239
+ # let typhoeus handle File, Array and nil parameters
240
+ data[key] = value
241
+ else
242
+ data[key] = value.to_s
243
+ end
244
+ end
245
+ elsif body
246
+ data = body.is_a?(String) ? body : body.to_json
247
+ else
248
+ data = nil
249
+ end
250
+ data
251
+ end
252
+
253
+ # Update hearder and query params based on authentication settings.
254
+ def update_params_for_auth!(header_params, query_params, auth_names)
255
+ Array(auth_names).each do |auth_name|
256
+ auth_setting = @config.auth_settings[auth_name]
257
+ next unless auth_setting
258
+ case auth_setting[:in]
259
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
260
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
261
+ else fail ArgumentError, 'Authentication token must be in `query` of `header`'
262
+ end
263
+ end
264
+ end
265
+
266
+ def user_agent=(user_agent)
267
+ @user_agent = user_agent
268
+ @default_headers['User-Agent'] = @user_agent
269
+ end
270
+
271
+ # Return Accept header based on an array of accepts provided.
272
+ # @param [Array] accepts array for Accept
273
+ # @return [String] the Accept header (e.g. application/json)
274
+ def select_header_accept(accepts)
275
+ return nil if accepts.nil? || accepts.empty?
276
+ # use JSON when present, otherwise use all of the provided
277
+ json_accept = accepts.find { |s| json_mime?(s) }
278
+ return json_accept || accepts.join(',')
279
+ end
280
+
281
+ # Return Content-Type header based on an array of content types provided.
282
+ # @param [Array] content_types array for Content-Type
283
+ # @return [String] the Content-Type header (e.g. application/json)
284
+ def select_header_content_type(content_types)
285
+ # use application/json by default
286
+ return 'application/json' if content_types.nil? || content_types.empty?
287
+ # use JSON when present, otherwise use the first one
288
+ json_content_type = content_types.find { |s| json_mime?(s) }
289
+ return json_content_type || content_types.first
290
+ end
291
+
292
+ # Convert object (array, hash, object, etc) to JSON string.
293
+ # @param [Object] model object to be converted into JSON string
294
+ # @return [String] JSON string representation of the object
295
+ def object_to_http_body(model)
296
+ return model if model.nil? || model.is_a?(String)
297
+ _body = nil
298
+ if model.is_a?(Array)
299
+ _body = model.map{|m| object_to_hash(m) }
300
+ else
301
+ _body = object_to_hash(model)
302
+ end
303
+ _body.to_json
304
+ end
305
+
306
+ # Convert object(non-array) to hash.
307
+ # @param [Object] obj object to be converted into JSON string
308
+ # @return [String] JSON string representation of the object
309
+ def object_to_hash(obj)
310
+ if obj.respond_to?(:to_hash)
311
+ obj.to_hash
312
+ else
313
+ obj
314
+ end
315
+ end
316
+
317
+ # Build parameter value according to the given collection format.
318
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
319
+ def build_collection_param(param, collection_format)
320
+ case collection_format
321
+ when :csv
322
+ param.join(',')
323
+ when :ssv
324
+ param.join(' ')
325
+ when :tsv
326
+ param.join("\t")
327
+ when :pipes
328
+ param.join('|')
329
+ when :multi
330
+ # return the array directly as typhoeus will handle it as expected
331
+ param
332
+ else
333
+ fail "unknown collection format: #{collection_format.inspect}"
334
+ end
335
+ end
336
+ end
337
+ end
@@ -0,0 +1,31 @@
1
+ =begin
2
+ Square Connect API
3
+
4
+ OpenAPI spec version: 2.0
5
+
6
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
7
+
8
+
9
+ =end
10
+
11
+ module SquareConnect
12
+ class ApiError < StandardError
13
+ attr_reader :code, :response_headers, :response_body
14
+
15
+ # Usage examples:
16
+ # ApiError.new
17
+ # ApiError.new("message")
18
+ # ApiError.new(:code => 500, :response_headers => {}, :response_body => "")
19
+ # ApiError.new(:code => 404, :message => "Not Found")
20
+ def initialize(arg = nil)
21
+ if arg.is_a? Hash
22
+ super arg
23
+ arg.each do |k, v|
24
+ instance_variable_set "@#{k}", v
25
+ end
26
+ else
27
+ super arg
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,163 @@
1
+ require 'uri'
2
+
3
+ module SquareConnect
4
+ class Configuration
5
+ # Defines url scheme
6
+ attr_accessor :scheme
7
+
8
+ # Defines url host
9
+ attr_accessor :host
10
+
11
+ # Defines url base path
12
+ attr_accessor :base_path
13
+
14
+ # Defines API keys used with API Key authentications.
15
+ #
16
+ # @return [Hash] key: parameter name, value: parameter value (API key)
17
+ #
18
+ # @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string)
19
+ # config.api_key['api_key'] = 'xxx'
20
+ attr_accessor :api_key
21
+
22
+ # Defines API key prefixes used with API Key authentications.
23
+ #
24
+ # @return [Hash] key: parameter name, value: API key prefix
25
+ #
26
+ # @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers)
27
+ # config.api_key_prefix['api_key'] = 'Token'
28
+ attr_accessor :api_key_prefix
29
+
30
+ # Defines the username used with HTTP basic authentication.
31
+ #
32
+ # @return [String]
33
+ attr_accessor :username
34
+
35
+ # Defines the password used with HTTP basic authentication.
36
+ #
37
+ # @return [String]
38
+ attr_accessor :password
39
+
40
+ # Defines the access token (Bearer) used with OAuth2.
41
+ attr_accessor :access_token
42
+
43
+ # Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
44
+ # details will be logged with `logger.debug` (see the `logger` attribute).
45
+ # Default to false.
46
+ #
47
+ # @return [true, false]
48
+ attr_accessor :debugging
49
+
50
+ # Defines the logger used for debugging.
51
+ # Default to `Rails.logger` (when in Rails) or logging to STDOUT.
52
+ #
53
+ # @return [#debug]
54
+ attr_accessor :logger
55
+
56
+ # Defines the temporary folder to store downloaded files
57
+ # (for API endpoints that have file response).
58
+ # Default to use `Tempfile`.
59
+ #
60
+ # @return [String]
61
+ attr_accessor :temp_folder_path
62
+
63
+ # The time limit for HTTP request in seconds.
64
+ # Default to 0 (never times out).
65
+ attr_accessor :timeout
66
+
67
+ ### TLS/SSL
68
+ # Set this to false to skip verifying SSL certificate when calling API from https server.
69
+ # Default to true.
70
+ #
71
+ # @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
72
+ #
73
+ # @return [true, false]
74
+ attr_accessor :verify_ssl
75
+
76
+ # Set this to customize the certificate file to verify the peer.
77
+ #
78
+ # @return [String] the path to the certificate file
79
+ #
80
+ # @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
81
+ # https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
82
+ attr_accessor :ssl_ca_cert
83
+
84
+ # Client certificate file (for client certificate)
85
+ attr_accessor :cert_file
86
+
87
+ # Client private key file (for client certificate)
88
+ attr_accessor :key_file
89
+
90
+ attr_accessor :inject_format
91
+
92
+ attr_accessor :force_ending_format
93
+
94
+ def initialize
95
+ @scheme = 'https'
96
+ @host = 'connect.squareup.com'
97
+ @base_path = ''
98
+ @api_key = {}
99
+ @api_key_prefix = {}
100
+ @timeout = 0
101
+ @verify_ssl = true
102
+ @cert_file = nil
103
+ @key_file = nil
104
+ @debugging = false
105
+ @inject_format = false
106
+ @force_ending_format = false
107
+ @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
108
+
109
+ yield(self) if block_given?
110
+ end
111
+
112
+ # The default Configuration object.
113
+ def self.default
114
+ @@default ||= Configuration.new
115
+ end
116
+
117
+ def configure
118
+ yield(self) if block_given?
119
+ end
120
+
121
+ def scheme=(scheme)
122
+ # remove :// from scheme
123
+ @scheme = scheme.sub(/:\/\//, '')
124
+ end
125
+
126
+ def host=(host)
127
+ # remove http(s):// and anything after a slash
128
+ @host = host.sub(/https?:\/\//, '').split('/').first
129
+ end
130
+
131
+ def base_path=(base_path)
132
+ # Add leading and trailing slashes to base_path
133
+ @base_path = "/#{base_path}".gsub(/\/+/, '/')
134
+ @base_path = "" if @base_path == "/"
135
+ end
136
+
137
+ def base_url
138
+ url = "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '')
139
+ URI.encode(url)
140
+ end
141
+
142
+ # Gets API key (with prefix if set).
143
+ # @param [String] param_name the parameter name of API key auth
144
+ def api_key_with_prefix(param_name)
145
+ if @api_key_prefix[param_name]
146
+ "#{@api_key_prefix[param_name]} #{@api_key[param_name]}"
147
+ else
148
+ @api_key[param_name]
149
+ end
150
+ end
151
+
152
+ # Gets Basic Auth token string
153
+ def basic_auth_token
154
+ 'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")
155
+ end
156
+
157
+ # Returns Auth Settings hash for api client.
158
+ def auth_settings
159
+ {
160
+ }
161
+ end
162
+ end
163
+ end