noths 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/README.md +10 -23
- data/bin/generate_gem +3 -1
- data/bin/refresh_schemas +22 -16
- data/docs/AcceptanceBulkModel.md +2 -2
- data/docs/DispatchBulkModel.md +1 -1
- data/docs/Financials.md +1 -1
- data/docs/Order.md +12 -11
- data/docs/OrdersApi.md +104 -151
- data/docs/RefundsApi.md +8 -12
- data/git_push.sh +55 -0
- data/lib/noths.rb +1 -11
- data/lib/noths/api/orders_api.rb +234 -114
- data/lib/noths/api/refunds_api.rb +25 -15
- data/lib/noths/api_client.rb +3 -12
- data/lib/noths/api_error.rb +16 -11
- data/lib/noths/configuration.rb +3 -20
- data/lib/noths/models/acceptance_bulk_model.rb +3 -14
- data/lib/noths/models/currency.rb +1 -12
- data/lib/noths/models/decline_bulk_model.rb +1 -12
- data/lib/noths/models/delivery_address.rb +1 -12
- data/lib/noths/models/delivery_service.rb +1 -12
- data/lib/noths/models/delivery_zone.rb +1 -12
- data/lib/noths/models/dispatch_bulk_model.rb +2 -13
- data/lib/noths/models/dispatch_notes_bulk_model.rb +1 -12
- data/lib/noths/models/enquiry.rb +1 -12
- data/lib/noths/models/enquiry_message.rb +1 -12
- data/lib/noths/models/error.rb +1 -12
- data/lib/noths/models/financials.rb +5 -14
- data/lib/noths/models/financials_details.rb +1 -12
- data/lib/noths/models/image_url.rb +1 -12
- data/lib/noths/models/item.rb +1 -12
- data/lib/noths/models/item_financials.rb +1 -12
- data/lib/noths/models/link.rb +1 -12
- data/lib/noths/models/manual_payment.rb +1 -12
- data/lib/noths/models/money.rb +1 -12
- data/lib/noths/models/order.rb +28 -28
- data/lib/noths/models/order_detail.rb +1 -12
- data/lib/noths/models/order_details_bulk_model.rb +1 -12
- data/lib/noths/models/order_index.rb +1 -12
- data/lib/noths/models/order_refund.rb +1 -12
- data/lib/noths/models/product.rb +1 -12
- data/lib/noths/models/product_option.rb +1 -12
- data/lib/noths/models/refund_response.rb +1 -12
- data/lib/noths/models/search_result_meta.rb +1 -12
- data/lib/noths/models/user.rb +1 -12
- data/lib/noths/version.rb +2 -12
- data/noths.gemspec +1 -11
- data/schemas/api-docs.json +3 -7
- data/schemas/orders.json +163 -88
- data/schemas/refunds.json +10 -5
- data/spec/api/orders_api_spec.rb +53 -99
- data/spec/api/refunds_api_spec.rb +6 -28
- data/spec/api_client_spec.rb +2 -14
- data/spec/configuration_spec.rb +9 -25
- data/spec/models/acceptance_bulk_model_spec.rb +7 -38
- data/spec/models/currency_spec.rb +7 -38
- data/spec/models/decline_bulk_model_spec.rb +5 -32
- data/spec/models/delivery_address_spec.rb +9 -44
- data/spec/models/delivery_service_spec.rb +5 -32
- data/spec/models/delivery_zone_spec.rb +5 -32
- data/spec/models/dispatch_bulk_model_spec.rb +8 -41
- data/spec/models/dispatch_notes_bulk_model_spec.rb +4 -29
- data/spec/models/enquiry_message_spec.rb +4 -29
- data/spec/models/enquiry_spec.rb +6 -35
- data/spec/models/error_spec.rb +5 -32
- data/spec/models/financials_details_spec.rb +6 -35
- data/spec/models/financials_spec.rb +21 -80
- data/spec/models/image_url_spec.rb +9 -44
- data/spec/models/item_financials_spec.rb +9 -44
- data/spec/models/item_spec.rb +11 -50
- data/spec/models/link_spec.rb +6 -35
- data/spec/models/manual_payment_spec.rb +7 -38
- data/spec/models/money_spec.rb +5 -32
- data/spec/models/order_detail_spec.rb +5 -32
- data/spec/models/order_details_bulk_model_spec.rb +4 -29
- data/spec/models/order_index_spec.rb +6 -35
- data/spec/models/order_refund_spec.rb +4 -29
- data/spec/models/order_spec.rb +53 -164
- data/spec/models/product_option_spec.rb +5 -32
- data/spec/models/product_spec.rb +8 -41
- data/spec/models/refund_response_spec.rb +6 -35
- data/spec/models/search_result_meta_spec.rb +5 -32
- data/spec/models/user_spec.rb +6 -35
- data/spec/spec_helper.rb +1 -11
- data/swagger_config.json +1 -1
- data/templates/Gemfile.mustache +7 -0
- data/templates/README.mustache +127 -0
- data/templates/Rakefile.mustache +8 -0
- data/templates/api.mustache +194 -0
- data/templates/api_client.mustache +383 -0
- data/templates/api_client_spec.mustache +214 -0
- data/templates/api_doc.mustache +79 -0
- data/templates/api_error.mustache +43 -0
- data/templates/api_info.mustache +12 -0
- data/templates/api_test.mustache +29 -0
- data/templates/base_object.mustache +102 -0
- data/templates/base_object_spec.mustache +107 -0
- data/templates/configuration.mustache +221 -0
- data/templates/configuration_spec.mustache +26 -0
- data/templates/gem.mustache +38 -0
- data/templates/gemspec.mustache +41 -0
- data/templates/git_push.sh.mustache +55 -0
- data/templates/gitignore.mustache +35 -0
- data/templates/model.mustache +7 -0
- data/templates/model_doc.mustache +9 -0
- data/templates/model_test.mustache +19 -0
- data/templates/partial_model_enum_class.mustache +13 -0
- data/templates/partial_model_generic.mustache +275 -0
- data/templates/rspec.mustache +2 -0
- data/templates/spec_helper.mustache +101 -0
- data/templates/version.mustache +5 -0
- metadata +30 -3
@@ -0,0 +1,383 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'date'
|
4
|
+
require 'json'
|
5
|
+
require 'logger'
|
6
|
+
require 'tempfile'
|
7
|
+
require 'typhoeus'
|
8
|
+
require 'uri'
|
9
|
+
|
10
|
+
module {{moduleName}}
|
11
|
+
class ApiClient
|
12
|
+
# The Configuration object holding settings to be used in the API client.
|
13
|
+
attr_accessor :config
|
14
|
+
|
15
|
+
# Defines the headers to be used in HTTP requests of all API calls by default.
|
16
|
+
#
|
17
|
+
# @return [Hash]
|
18
|
+
attr_accessor :default_headers
|
19
|
+
|
20
|
+
# Initializes the ApiClient
|
21
|
+
# @option config [Configuration] Configuration for initializing the object, default to Configuration.default
|
22
|
+
def initialize(config = Configuration.default)
|
23
|
+
@config = config
|
24
|
+
@user_agent = "{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/#{VERSION}/ruby{{/httpUserAgent}}"
|
25
|
+
@default_headers = {
|
26
|
+
'Content-Type' => "application/json",
|
27
|
+
'User-Agent' => @user_agent
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.default
|
32
|
+
@@default ||= ApiClient.new
|
33
|
+
end
|
34
|
+
|
35
|
+
# Call an API with given options.
|
36
|
+
#
|
37
|
+
# @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
|
38
|
+
# the data deserialized from response body (could be nil), response status code and response headers.
|
39
|
+
def call_api(http_method, path, opts = {})
|
40
|
+
request = build_request(http_method, path, opts)
|
41
|
+
response = request.run
|
42
|
+
|
43
|
+
if @config.debugging
|
44
|
+
@config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
|
45
|
+
end
|
46
|
+
|
47
|
+
unless response.success?
|
48
|
+
if response.timed_out?
|
49
|
+
fail ApiError.new('Connection timed out')
|
50
|
+
elsif response.code == 0
|
51
|
+
# Errors from libcurl will be made visible here
|
52
|
+
fail ApiError.new(:code => 0,
|
53
|
+
:message => response.return_message)
|
54
|
+
else
|
55
|
+
fail ApiError.new(:code => response.code,
|
56
|
+
:response_headers => response.headers,
|
57
|
+
:response_body => response.body),
|
58
|
+
response.status_message
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
if opts[:return_type]
|
63
|
+
data = deserialize(response, opts[:return_type])
|
64
|
+
else
|
65
|
+
data = nil
|
66
|
+
end
|
67
|
+
return data, response.code, response.headers
|
68
|
+
end
|
69
|
+
|
70
|
+
# Builds the HTTP request
|
71
|
+
#
|
72
|
+
# @param [String] http_method HTTP method/verb (e.g. POST)
|
73
|
+
# @param [String] path URL path (e.g. /account/new)
|
74
|
+
# @option opts [Hash] :header_params Header parameters
|
75
|
+
# @option opts [Hash] :query_params Query parameters
|
76
|
+
# @option opts [Hash] :form_params Query parameters
|
77
|
+
# @option opts [Object] :body HTTP body (JSON/XML)
|
78
|
+
# @return [Typhoeus::Request] A Typhoeus Request
|
79
|
+
def build_request(http_method, path, opts = {})
|
80
|
+
url = build_request_url(path)
|
81
|
+
http_method = http_method.to_sym.downcase
|
82
|
+
|
83
|
+
header_params = @default_headers.merge(opts[:header_params] || {})
|
84
|
+
query_params = opts[:query_params] || {}
|
85
|
+
form_params = opts[:form_params] || {}
|
86
|
+
|
87
|
+
{{#hasAuthMethods}}
|
88
|
+
update_params_for_auth! header_params, query_params, opts[:auth_names]
|
89
|
+
{{/hasAuthMethods}}
|
90
|
+
|
91
|
+
# set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
|
92
|
+
_verify_ssl_host = @config.verify_ssl_host ? 2 : 0
|
93
|
+
|
94
|
+
req_opts = {
|
95
|
+
:method => http_method,
|
96
|
+
:headers => header_params,
|
97
|
+
:params => query_params,
|
98
|
+
:params_encoding => @config.params_encoding,
|
99
|
+
:timeout => @config.timeout,
|
100
|
+
:ssl_verifypeer => @config.verify_ssl,
|
101
|
+
:ssl_verifyhost => _verify_ssl_host,
|
102
|
+
:sslcert => @config.cert_file,
|
103
|
+
:sslkey => @config.key_file,
|
104
|
+
:verbose => @config.debugging
|
105
|
+
}
|
106
|
+
|
107
|
+
# set custom cert, if provided
|
108
|
+
req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
|
109
|
+
|
110
|
+
if [:post, :patch, :put, :delete].include?(http_method)
|
111
|
+
req_body = build_request_body(header_params, form_params, opts[:body])
|
112
|
+
req_opts.update :body => req_body
|
113
|
+
if @config.debugging
|
114
|
+
@config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
request = Typhoeus::Request.new(url, req_opts)
|
119
|
+
download_file(request) if opts[:return_type] == 'File'
|
120
|
+
request
|
121
|
+
end
|
122
|
+
|
123
|
+
# Check if the given MIME is a JSON MIME.
|
124
|
+
# JSON MIME examples:
|
125
|
+
# application/json
|
126
|
+
# application/json; charset=UTF8
|
127
|
+
# APPLICATION/JSON
|
128
|
+
# */*
|
129
|
+
# @param [String] mime MIME
|
130
|
+
# @return [Boolean] True if the MIME is application/json
|
131
|
+
def json_mime?(mime)
|
132
|
+
(mime == "*/*") || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
|
133
|
+
end
|
134
|
+
|
135
|
+
# Deserialize the response to the given return type.
|
136
|
+
#
|
137
|
+
# @param [Response] response HTTP response
|
138
|
+
# @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
|
139
|
+
def deserialize(response, return_type)
|
140
|
+
body = response.body
|
141
|
+
|
142
|
+
# handle file downloading - return the File instance processed in request callbacks
|
143
|
+
# note that response body is empty when the file is written in chunks in request on_body callback
|
144
|
+
return @tempfile if return_type == 'File'
|
145
|
+
|
146
|
+
return nil if body.nil? || body.empty?
|
147
|
+
|
148
|
+
# return response body directly for String return type
|
149
|
+
return body if return_type == 'String'
|
150
|
+
|
151
|
+
# ensuring a default content type
|
152
|
+
content_type = response.headers['Content-Type'] || 'application/json'
|
153
|
+
|
154
|
+
fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
|
155
|
+
|
156
|
+
begin
|
157
|
+
data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
|
158
|
+
rescue JSON::ParserError => e
|
159
|
+
if %w(String Date DateTime).include?(return_type)
|
160
|
+
data = body
|
161
|
+
else
|
162
|
+
raise e
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
convert_to_type data, return_type
|
167
|
+
end
|
168
|
+
|
169
|
+
# Convert data to the given return type.
|
170
|
+
# @param [Object] data Data to be converted
|
171
|
+
# @param [String] return_type Return type
|
172
|
+
# @return [Mixed] Data in a particular type
|
173
|
+
def convert_to_type(data, return_type)
|
174
|
+
return nil if data.nil?
|
175
|
+
case return_type
|
176
|
+
when 'String'
|
177
|
+
data.to_s
|
178
|
+
when 'Integer'
|
179
|
+
data.to_i
|
180
|
+
when 'Float'
|
181
|
+
data.to_f
|
182
|
+
when 'BOOLEAN'
|
183
|
+
data == true
|
184
|
+
when 'DateTime'
|
185
|
+
# parse date time (expecting ISO 8601 format)
|
186
|
+
DateTime.parse data
|
187
|
+
when 'Date'
|
188
|
+
# parse date time (expecting ISO 8601 format)
|
189
|
+
Date.parse data
|
190
|
+
when 'Object'
|
191
|
+
# generic object (usually a Hash), return directly
|
192
|
+
data
|
193
|
+
when /\AArray<(.+)>\z/
|
194
|
+
# e.g. Array<Pet>
|
195
|
+
sub_type = $1
|
196
|
+
data.map {|item| convert_to_type(item, sub_type) }
|
197
|
+
when /\AHash\<String, (.+)\>\z/
|
198
|
+
# e.g. Hash<String, Integer>
|
199
|
+
sub_type = $1
|
200
|
+
{}.tap do |hash|
|
201
|
+
data.each {|k, v| hash[k] = convert_to_type(v, sub_type) }
|
202
|
+
end
|
203
|
+
else
|
204
|
+
# models, e.g. Pet
|
205
|
+
{{moduleName}}.const_get(return_type).new.tap do |model|
|
206
|
+
model.build_from_hash data
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
# Save response body into a file in (the defined) temporary folder, using the filename
|
212
|
+
# from the "Content-Disposition" header if provided, otherwise a random filename.
|
213
|
+
# The response body is written to the file in chunks in order to handle files which
|
214
|
+
# size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
|
215
|
+
# process can use.
|
216
|
+
#
|
217
|
+
# @see Configuration#temp_folder_path
|
218
|
+
def download_file(request)
|
219
|
+
tempfile = nil
|
220
|
+
encoding = nil
|
221
|
+
request.on_headers do |response|
|
222
|
+
content_disposition = response.headers['Content-Disposition']
|
223
|
+
if content_disposition and content_disposition =~ /filename=/i
|
224
|
+
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
|
225
|
+
prefix = sanitize_filename(filename)
|
226
|
+
else
|
227
|
+
prefix = 'download-'
|
228
|
+
end
|
229
|
+
prefix = prefix + '-' unless prefix.end_with?('-')
|
230
|
+
encoding = response.body.encoding
|
231
|
+
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
|
232
|
+
@tempfile = tempfile
|
233
|
+
end
|
234
|
+
request.on_body do |chunk|
|
235
|
+
chunk.force_encoding(encoding)
|
236
|
+
tempfile.write(chunk)
|
237
|
+
end
|
238
|
+
request.on_complete do |response|
|
239
|
+
tempfile.close
|
240
|
+
@config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
|
241
|
+
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
|
242
|
+
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
243
|
+
"explicitly with `tempfile.delete`"
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
# Sanitize filename by removing path.
|
248
|
+
# e.g. ../../sun.gif becomes sun.gif
|
249
|
+
#
|
250
|
+
# @param [String] filename the filename to be sanitized
|
251
|
+
# @return [String] the sanitized filename
|
252
|
+
def sanitize_filename(filename)
|
253
|
+
filename.gsub(/.*[\/\\]/, '')
|
254
|
+
end
|
255
|
+
|
256
|
+
def build_request_url(path)
|
257
|
+
# Add leading and trailing slashes to path
|
258
|
+
path = "/#{path}".gsub(/\/+/, '/')
|
259
|
+
URI.encode(@config.base_url + path)
|
260
|
+
end
|
261
|
+
|
262
|
+
# Builds the HTTP request body
|
263
|
+
#
|
264
|
+
# @param [Hash] header_params Header parameters
|
265
|
+
# @param [Hash] form_params Query parameters
|
266
|
+
# @param [Object] body HTTP body (JSON/XML)
|
267
|
+
# @return [String] HTTP body data in the form of string
|
268
|
+
def build_request_body(header_params, form_params, body)
|
269
|
+
# http form
|
270
|
+
if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
|
271
|
+
header_params['Content-Type'] == 'multipart/form-data'
|
272
|
+
data = {}
|
273
|
+
form_params.each do |key, value|
|
274
|
+
case value
|
275
|
+
when ::File, ::Array, nil
|
276
|
+
# let typhoeus handle File, Array and nil parameters
|
277
|
+
data[key] = value
|
278
|
+
when :DateTime, :Date
|
279
|
+
data[key] = value.to_s(:iso_8601)
|
280
|
+
else
|
281
|
+
data[key] = value.to_s
|
282
|
+
end
|
283
|
+
end
|
284
|
+
elsif body
|
285
|
+
data = body.is_a?(String) ? body : body.to_json
|
286
|
+
else
|
287
|
+
data = nil
|
288
|
+
end
|
289
|
+
data
|
290
|
+
end
|
291
|
+
|
292
|
+
# Update hearder and query params based on authentication settings.
|
293
|
+
#
|
294
|
+
# @param [Hash] header_params Header parameters
|
295
|
+
# @param [Hash] query_params Query parameters
|
296
|
+
# @param [String] auth_names Authentication scheme name
|
297
|
+
def update_params_for_auth!(header_params, query_params, auth_names)
|
298
|
+
Array(auth_names).each do |auth_name|
|
299
|
+
auth_setting = @config.auth_settings[auth_name]
|
300
|
+
next unless auth_setting
|
301
|
+
case auth_setting[:in]
|
302
|
+
when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
|
303
|
+
when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
|
304
|
+
else fail ArgumentError, 'Authentication token must be in `query` of `header`'
|
305
|
+
end
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
# Sets user agent in HTTP header
|
310
|
+
#
|
311
|
+
# @param [String] user_agent User agent (e.g. swagger-codegen/ruby/1.0.0)
|
312
|
+
def user_agent=(user_agent)
|
313
|
+
@user_agent = user_agent
|
314
|
+
@default_headers['User-Agent'] = @user_agent
|
315
|
+
end
|
316
|
+
|
317
|
+
# Return Accept header based on an array of accepts provided.
|
318
|
+
# @param [Array] accepts array for Accept
|
319
|
+
# @return [String] the Accept header (e.g. application/json)
|
320
|
+
def select_header_accept(accepts)
|
321
|
+
return nil if accepts.nil? || accepts.empty?
|
322
|
+
# use JSON when present, otherwise use all of the provided
|
323
|
+
json_accept = accepts.find { |s| json_mime?(s) }
|
324
|
+
return json_accept || accepts.join(',')
|
325
|
+
end
|
326
|
+
|
327
|
+
# Return Content-Type header based on an array of content types provided.
|
328
|
+
# @param [Array] content_types array for Content-Type
|
329
|
+
# @return [String] the Content-Type header (e.g. application/json)
|
330
|
+
def select_header_content_type(content_types)
|
331
|
+
# use application/json by default
|
332
|
+
return 'application/json' if content_types.nil? || content_types.empty?
|
333
|
+
# use JSON when present, otherwise use the first one
|
334
|
+
json_content_type = content_types.find { |s| json_mime?(s) }
|
335
|
+
return json_content_type || content_types.first
|
336
|
+
end
|
337
|
+
|
338
|
+
# Convert object (array, hash, object, etc) to JSON string.
|
339
|
+
# @param [Object] model object to be converted into JSON string
|
340
|
+
# @return [String] JSON string representation of the object
|
341
|
+
def object_to_http_body(model)
|
342
|
+
return model if model.nil? || model.is_a?(String)
|
343
|
+
local_body = nil
|
344
|
+
if model.is_a?(Array)
|
345
|
+
local_body = model.map{|m| object_to_hash(m) }
|
346
|
+
else
|
347
|
+
local_body = object_to_hash(model)
|
348
|
+
end
|
349
|
+
local_body.to_json
|
350
|
+
end
|
351
|
+
|
352
|
+
# Convert object(non-array) to hash.
|
353
|
+
# @param [Object] obj object to be converted into JSON string
|
354
|
+
# @return [String] JSON string representation of the object
|
355
|
+
def object_to_hash(obj)
|
356
|
+
if obj.respond_to?(:to_hash)
|
357
|
+
obj.to_hash
|
358
|
+
else
|
359
|
+
obj
|
360
|
+
end
|
361
|
+
end
|
362
|
+
|
363
|
+
# Build parameter value according to the given collection format.
|
364
|
+
# @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
|
365
|
+
def build_collection_param(param, collection_format)
|
366
|
+
case collection_format
|
367
|
+
when :csv
|
368
|
+
param.join(',')
|
369
|
+
when :ssv
|
370
|
+
param.join(' ')
|
371
|
+
when :tsv
|
372
|
+
param.join("\t")
|
373
|
+
when :pipes
|
374
|
+
param.join('|')
|
375
|
+
when :multi
|
376
|
+
# return the array directly as typhoeus will handle it as expected
|
377
|
+
param
|
378
|
+
else
|
379
|
+
fail "unknown collection format: #{collection_format.inspect}"
|
380
|
+
end
|
381
|
+
end
|
382
|
+
end
|
383
|
+
end
|
@@ -0,0 +1,214 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
describe {{moduleName}}::ApiClient do
|
4
|
+
context 'initialization' do
|
5
|
+
context 'URL stuff' do
|
6
|
+
context 'host' do
|
7
|
+
it 'removes http from host' do
|
8
|
+
{{moduleName}}.configure { |c| c.host = 'http://example.com' }
|
9
|
+
expect({{moduleName}}::Configuration.default.host).to eq('example.com')
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'removes https from host' do
|
13
|
+
{{moduleName}}.configure { |c| c.host = 'https://wookiee.com' }
|
14
|
+
expect({{moduleName}}::ApiClient.default.config.host).to eq('wookiee.com')
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'removes trailing path from host' do
|
18
|
+
{{moduleName}}.configure { |c| c.host = 'hobo.com/v4' }
|
19
|
+
expect({{moduleName}}::Configuration.default.host).to eq('hobo.com')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'base_path' do
|
24
|
+
it "prepends a slash to base_path" do
|
25
|
+
{{moduleName}}.configure { |c| c.base_path = 'v4/dog' }
|
26
|
+
expect({{moduleName}}::Configuration.default.base_path).to eq('/v4/dog')
|
27
|
+
end
|
28
|
+
|
29
|
+
it "doesn't prepend a slash if one is already there" do
|
30
|
+
{{moduleName}}.configure { |c| c.base_path = '/v4/dog' }
|
31
|
+
expect({{moduleName}}::Configuration.default.base_path).to eq('/v4/dog')
|
32
|
+
end
|
33
|
+
|
34
|
+
it "ends up as a blank string if nil" do
|
35
|
+
{{moduleName}}.configure { |c| c.base_path = nil }
|
36
|
+
expect({{moduleName}}::Configuration.default.base_path).to eq('')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "params_encoding in #build_request" do
|
43
|
+
let(:config) { {{moduleName}}::Configuration.new }
|
44
|
+
let(:api_client) { {{moduleName}}::ApiClient.new(config) }
|
45
|
+
|
46
|
+
it "defaults to nil" do
|
47
|
+
expect({{moduleName}}::Configuration.default.params_encoding).to eq(nil)
|
48
|
+
expect(config.params_encoding).to eq(nil)
|
49
|
+
|
50
|
+
request = api_client.build_request(:get, '/test')
|
51
|
+
expect(request.options[:params_encoding]).to eq(nil)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "can be customized" do
|
55
|
+
config.params_encoding = :multi
|
56
|
+
request = api_client.build_request(:get, '/test')
|
57
|
+
expect(request.options[:params_encoding]).to eq(:multi)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "timeout in #build_request" do
|
62
|
+
let(:config) { {{moduleName}}::Configuration.new }
|
63
|
+
let(:api_client) { {{moduleName}}::ApiClient.new(config) }
|
64
|
+
|
65
|
+
it "defaults to 0" do
|
66
|
+
expect({{moduleName}}::Configuration.default.timeout).to eq(0)
|
67
|
+
expect(config.timeout).to eq(0)
|
68
|
+
|
69
|
+
request = api_client.build_request(:get, '/test')
|
70
|
+
expect(request.options[:timeout]).to eq(0)
|
71
|
+
end
|
72
|
+
|
73
|
+
it "can be customized" do
|
74
|
+
config.timeout = 100
|
75
|
+
request = api_client.build_request(:get, '/test')
|
76
|
+
expect(request.options[:timeout]).to eq(100)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "#deserialize" do
|
81
|
+
it "handles Array<Integer>" do
|
82
|
+
api_client = {{moduleName}}::ApiClient.new
|
83
|
+
headers = {'Content-Type' => 'application/json'}
|
84
|
+
response = double('response', headers: headers, body: '[12, 34]')
|
85
|
+
data = api_client.deserialize(response, 'Array<Integer>')
|
86
|
+
expect(data).to be_instance_of(Array)
|
87
|
+
expect(data).to eq([12, 34])
|
88
|
+
end
|
89
|
+
|
90
|
+
it "handles Array<Array<Integer>>" do
|
91
|
+
api_client = {{moduleName}}::ApiClient.new
|
92
|
+
headers = {'Content-Type' => 'application/json'}
|
93
|
+
response = double('response', headers: headers, body: '[[12, 34], [56]]')
|
94
|
+
data = api_client.deserialize(response, 'Array<Array<Integer>>')
|
95
|
+
expect(data).to be_instance_of(Array)
|
96
|
+
expect(data).to eq([[12, 34], [56]])
|
97
|
+
end
|
98
|
+
|
99
|
+
it "handles Hash<String, String>" do
|
100
|
+
api_client = {{moduleName}}::ApiClient.new
|
101
|
+
headers = {'Content-Type' => 'application/json'}
|
102
|
+
response = double('response', headers: headers, body: '{"message": "Hello"}')
|
103
|
+
data = api_client.deserialize(response, 'Hash<String, String>')
|
104
|
+
expect(data).to be_instance_of(Hash)
|
105
|
+
expect(data).to eq({:message => 'Hello'})
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe "#object_to_hash" do
|
110
|
+
it "ignores nils and includes empty arrays" do
|
111
|
+
# uncomment below to test object_to_hash for model
|
112
|
+
#api_client = {{moduleName}}::ApiClient.new
|
113
|
+
#_model = {{moduleName}}::ModelName.new
|
114
|
+
# update the model attribute below
|
115
|
+
#_model.id = 1
|
116
|
+
# update the expected value (hash) below
|
117
|
+
#expected = {id: 1, name: '', tags: []}
|
118
|
+
#expect(api_client.object_to_hash(_model)).to eq(expected)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
describe "#build_collection_param" do
|
123
|
+
let(:param) { ['aa', 'bb', 'cc'] }
|
124
|
+
let(:api_client) { {{moduleName}}::ApiClient.new }
|
125
|
+
|
126
|
+
it "works for csv" do
|
127
|
+
expect(api_client.build_collection_param(param, :csv)).to eq('aa,bb,cc')
|
128
|
+
end
|
129
|
+
|
130
|
+
it "works for ssv" do
|
131
|
+
expect(api_client.build_collection_param(param, :ssv)).to eq('aa bb cc')
|
132
|
+
end
|
133
|
+
|
134
|
+
it "works for tsv" do
|
135
|
+
expect(api_client.build_collection_param(param, :tsv)).to eq("aa\tbb\tcc")
|
136
|
+
end
|
137
|
+
|
138
|
+
it "works for pipes" do
|
139
|
+
expect(api_client.build_collection_param(param, :pipes)).to eq('aa|bb|cc')
|
140
|
+
end
|
141
|
+
|
142
|
+
it "works for multi" do
|
143
|
+
expect(api_client.build_collection_param(param, :multi)).to eq(['aa', 'bb', 'cc'])
|
144
|
+
end
|
145
|
+
|
146
|
+
it "fails for invalid collection format" do
|
147
|
+
expect(proc { api_client.build_collection_param(param, :INVALID) }).to raise_error(RuntimeError, 'unknown collection format: :INVALID')
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
describe "#json_mime?" do
|
152
|
+
let(:api_client) { {{moduleName}}::ApiClient.new }
|
153
|
+
|
154
|
+
it "works" do
|
155
|
+
expect(api_client.json_mime?(nil)).to eq false
|
156
|
+
expect(api_client.json_mime?('')).to eq false
|
157
|
+
|
158
|
+
expect(api_client.json_mime?('application/json')).to eq true
|
159
|
+
expect(api_client.json_mime?('application/json; charset=UTF8')).to eq true
|
160
|
+
expect(api_client.json_mime?('APPLICATION/JSON')).to eq true
|
161
|
+
|
162
|
+
expect(api_client.json_mime?('application/xml')).to eq false
|
163
|
+
expect(api_client.json_mime?('text/plain')).to eq false
|
164
|
+
expect(api_client.json_mime?('application/jsonp')).to eq false
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
describe "#select_header_accept" do
|
169
|
+
let(:api_client) { {{moduleName}}::ApiClient.new }
|
170
|
+
|
171
|
+
it "works" do
|
172
|
+
expect(api_client.select_header_accept(nil)).to be_nil
|
173
|
+
expect(api_client.select_header_accept([])).to be_nil
|
174
|
+
|
175
|
+
expect(api_client.select_header_accept(['application/json'])).to eq('application/json')
|
176
|
+
expect(api_client.select_header_accept(['application/xml', 'application/json; charset=UTF8'])).to eq('application/json; charset=UTF8')
|
177
|
+
expect(api_client.select_header_accept(['APPLICATION/JSON', 'text/html'])).to eq('APPLICATION/JSON')
|
178
|
+
|
179
|
+
expect(api_client.select_header_accept(['application/xml'])).to eq('application/xml')
|
180
|
+
expect(api_client.select_header_accept(['text/html', 'application/xml'])).to eq('text/html,application/xml')
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
describe "#select_header_content_type" do
|
185
|
+
let(:api_client) { {{moduleName}}::ApiClient.new }
|
186
|
+
|
187
|
+
it "works" do
|
188
|
+
expect(api_client.select_header_content_type(nil)).to eq('application/json')
|
189
|
+
expect(api_client.select_header_content_type([])).to eq('application/json')
|
190
|
+
|
191
|
+
expect(api_client.select_header_content_type(['application/json'])).to eq('application/json')
|
192
|
+
expect(api_client.select_header_content_type(['application/xml', 'application/json; charset=UTF8'])).to eq('application/json; charset=UTF8')
|
193
|
+
expect(api_client.select_header_content_type(['APPLICATION/JSON', 'text/html'])).to eq('APPLICATION/JSON')
|
194
|
+
expect(api_client.select_header_content_type(['application/xml'])).to eq('application/xml')
|
195
|
+
expect(api_client.select_header_content_type(['text/plain', 'application/xml'])).to eq('text/plain')
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
describe "#sanitize_filename" do
|
200
|
+
let(:api_client) { {{moduleName}}::ApiClient.new }
|
201
|
+
|
202
|
+
it "works" do
|
203
|
+
expect(api_client.sanitize_filename('sun')).to eq('sun')
|
204
|
+
expect(api_client.sanitize_filename('sun.gif')).to eq('sun.gif')
|
205
|
+
expect(api_client.sanitize_filename('../sun.gif')).to eq('sun.gif')
|
206
|
+
expect(api_client.sanitize_filename('/var/tmp/sun.gif')).to eq('sun.gif')
|
207
|
+
expect(api_client.sanitize_filename('./sun.gif')).to eq('sun.gif')
|
208
|
+
expect(api_client.sanitize_filename('..\sun.gif')).to eq('sun.gif')
|
209
|
+
expect(api_client.sanitize_filename('\var\tmp\sun.gif')).to eq('sun.gif')
|
210
|
+
expect(api_client.sanitize_filename('c:\var\tmp\sun.gif')).to eq('sun.gif')
|
211
|
+
expect(api_client.sanitize_filename('.\sun.gif')).to eq('sun.gif')
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|