vericred_client 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +225 -0
  3. data/docs/Applicant.md +14 -0
  4. data/docs/Carrier.md +10 -0
  5. data/docs/CarrierSubsidiary.md +9 -0
  6. data/docs/County.md +16 -0
  7. data/docs/Drug.md +10 -0
  8. data/docs/DrugCoverage.md +13 -0
  9. data/docs/DrugCoverageApi.md +60 -0
  10. data/docs/InlineResponse200.md +9 -0
  11. data/docs/InlineResponse2001.md +8 -0
  12. data/docs/InlineResponse2002.md +10 -0
  13. data/docs/Plan.md +53 -0
  14. data/docs/PlanCounty.md +10 -0
  15. data/docs/PlanSearchResult.md +54 -0
  16. data/docs/PlansApi.md +118 -0
  17. data/docs/Pricing.md +12 -0
  18. data/docs/Provider.md +31 -0
  19. data/docs/ProvidersApi.md +135 -0
  20. data/docs/Query.md +15 -0
  21. data/docs/RatingArea.md +9 -0
  22. data/docs/State.md +15 -0
  23. data/docs/ZipCode.md +9 -0
  24. data/docs/ZipCountiesApi.md +63 -0
  25. data/docs/ZipCounty.md +10 -0
  26. data/lib/vericred_client/api/drug_coverage_api.rb +201 -0
  27. data/lib/vericred_client/api/plans_api.rb +317 -0
  28. data/lib/vericred_client/api/providers_api.rb +303 -0
  29. data/lib/vericred_client/api/zip_counties_api.rb +208 -0
  30. data/lib/vericred_client/api_client.rb +474 -0
  31. data/lib/vericred_client/api_error.rb +149 -0
  32. data/lib/vericred_client/configuration.rb +163 -0
  33. data/lib/vericred_client/models/applicant.rb +358 -0
  34. data/lib/vericred_client/models/carrier.rb +318 -0
  35. data/lib/vericred_client/models/carrier_subsidiary.rb +308 -0
  36. data/lib/vericred_client/models/county.rb +360 -0
  37. data/lib/vericred_client/models/drug.rb +318 -0
  38. data/lib/vericred_client/models/drug_coverage.rb +348 -0
  39. data/lib/vericred_client/models/inline_response_200.rb +308 -0
  40. data/lib/vericred_client/models/inline_response_200_1.rb +297 -0
  41. data/lib/vericred_client/models/inline_response_200_2.rb +321 -0
  42. data/lib/vericred_client/models/plan.rb +748 -0
  43. data/lib/vericred_client/models/plan_county.rb +318 -0
  44. data/lib/vericred_client/models/plan_search_result.rb +758 -0
  45. data/lib/vericred_client/models/pricing.rb +338 -0
  46. data/lib/vericred_client/models/provider.rb +528 -0
  47. data/lib/vericred_client/models/query.rb +364 -0
  48. data/lib/vericred_client/models/rating_area.rb +308 -0
  49. data/lib/vericred_client/models/state.rb +368 -0
  50. data/lib/vericred_client/models/zip_code.rb +308 -0
  51. data/lib/vericred_client/models/zip_county.rb +318 -0
  52. data/lib/vericred_client/version.rb +128 -0
  53. data/lib/vericred_client.rb +175 -0
  54. data/spec/api/drug_coverage_api_spec.rb +168 -0
  55. data/spec/api/plans_api_spec.rb +226 -0
  56. data/spec/api/providers_api_spec.rb +202 -0
  57. data/spec/api/zip_counties_api_spec.rb +171 -0
  58. data/vericred_client.gemspec +31 -0
  59. metadata +285 -0
@@ -0,0 +1,208 @@
1
+ =begin
2
+ Vericred API
3
+
4
+ Vericred's API allows you to search for Health Plans that a specific doctor
5
+ accepts.
6
+
7
+ ## Getting Started
8
+
9
+ Visit our [Developer Portal](https://vericred.3scale.net/access_code?access_code=vericred&cms_token=3545ca52af07bde85b7c0c3aa9d1985e) to
10
+ create an account.
11
+
12
+ Once you have created an account, you can create one Application for
13
+ Production and another for our Sandbox (select the appropriate Plan when
14
+ you create the Application).
15
+
16
+ ## Authentication
17
+
18
+ To authenticate, pass the API Key you created in the Developer Portal as
19
+ a `Vericred-Api-Key` header.
20
+
21
+ `curl -H 'Vericred-Api-Key: YOUR_KEY' "https://api.vericred.com/providers?search_term=Foo&zip_code=11215"`
22
+
23
+ ## Versioning
24
+
25
+ Vericred's API default to the latest version. However, if you need a specific
26
+ version, you can request it with an `Accept-Version` header.
27
+
28
+ The current version is `v3`. Previous versions are `v1` and `v2`.
29
+
30
+ `curl -H 'Vericred-Api-Key: YOUR_KEY' -H 'Accept-Version: v2' "https://api.vericred.com/providers?search_term=Foo&zip_code=11215"`
31
+
32
+ ## Pagination
33
+
34
+ Most endpoints are not paginated. It will be noted in the documentation if/when
35
+ an endpoint is paginated.
36
+
37
+ When pagination is present, a `meta` stanza will be present in the response
38
+ with the total number of records
39
+
40
+ ```
41
+ {
42
+ things: [{ id: 1 }, { id: 2 }],
43
+ meta: { total: 500 }
44
+ }
45
+ ```
46
+
47
+ ## Sideloading
48
+
49
+ When we return multiple levels of an object graph (e.g. `Provider`s and their `State`s
50
+ we sideload the associated data. In this example, we would provide an Array of
51
+ `State`s and a `state_id` for each provider. This is done primarily to reduce the
52
+ payload size since many of the `Provider`s will share a `State`
53
+
54
+ ```
55
+ {
56
+ providers: [{ id: 1, state_id: 1}, { id: 2, state_id: 1 }],
57
+ states: [{ id: 1, code: 'NY' }]
58
+ }
59
+ ```
60
+
61
+ If you need the second level of the object graph, you can just match the
62
+ corresponding id.
63
+
64
+ ## Selecting specific data
65
+
66
+ All endpoints allow you to specify which fields you would like to return.
67
+ This allows you to limit the response to contain only the data you need.
68
+
69
+ For example, let's take a request that returns the following JSON by default
70
+
71
+ ```
72
+ {
73
+ provider: {
74
+ id: 1,
75
+ name: 'John',
76
+ phone: '1234567890',
77
+ field_we_dont_care_about: 'value_we_dont_care_about'
78
+ },
79
+ states: [{
80
+ id: 1,
81
+ name: 'New York',
82
+ code: 'NY',
83
+ field_we_dont_care_about: 'value_we_dont_care_about'
84
+ }]
85
+ }
86
+ ```
87
+
88
+ To limit our results to only return the fields we care about, we specify the
89
+ `select` query string parameter for the corresponding fields in the JSON
90
+ document.
91
+
92
+ In this case, we want to select `name` and `phone` from the `provider` key,
93
+ so we would add the parameters `select=provider.name,provider.phone`.
94
+ We also want the `name` and `code` from the `states` key, so we would
95
+ add the parameters `select=states.name,staes.code`. The id field of
96
+ each document is always returned whether or not it is requested.
97
+
98
+ Our final request would be `GET /providers/12345?select=provider.name,provider.phone,states.name,states.code`
99
+
100
+ The response would be
101
+
102
+ ```
103
+ {
104
+ provider: {
105
+ id: 1,
106
+ name: 'John',
107
+ phone: '1234567890'
108
+ },
109
+ states: [{
110
+ id: 1,
111
+ name: 'New York',
112
+ code: 'NY'
113
+ }]
114
+ }
115
+ ```
116
+
117
+
118
+
119
+ OpenAPI spec version:
120
+
121
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
122
+
123
+
124
+ =end
125
+
126
+ require "uri"
127
+
128
+ module VericredClient
129
+ class ZipCountiesApi
130
+ attr_accessor :api_client
131
+
132
+ def initialize(api_client = ApiClient.default)
133
+ @api_client = api_client
134
+ end
135
+
136
+ # Find Zip Counties by Zip Code
137
+ # ### Finding Zip Code and Fips Code
138
+
139
+ Our `Plan` endpoints require a zip code and a fips (county) code. This is
140
+ because plan pricing requires both of these elements. Users are unlikely to
141
+ know their fips code, so we provide this endpoint to look up a `ZipCounty` by
142
+ zip code and return both the selected zip and fips codes.
143
+
144
+
145
+ # @param zip_prefix Partial five-digit Zip
146
+ # @param [Hash] opts the optional parameters
147
+ # @return [InlineResponse2002]
148
+ def zip_counties_get(zip_prefix, opts = {})
149
+ data, _status_code, _headers = zip_counties_get_with_http_info(zip_prefix, opts)
150
+ return data
151
+ end
152
+
153
+ # Find Zip Counties by Zip Code
154
+ # ### Finding Zip Code and Fips Code
155
+
156
+ Our `Plan` endpoints require a zip code and a fips (county) code. This is
157
+ because plan pricing requires both of these elements. Users are unlikely to
158
+ know their fips code, so we provide this endpoint to look up a `ZipCounty` by
159
+ zip code and return both the selected zip and fips codes.
160
+
161
+
162
+ # @param zip_prefix Partial five-digit Zip
163
+ # @param [Hash] opts the optional parameters
164
+ # @return [Array<(InlineResponse2002, Fixnum, Hash)>] InlineResponse2002 data, response status code and response headers
165
+ def zip_counties_get_with_http_info(zip_prefix, opts = {})
166
+ if @api_client.config.debugging
167
+ @api_client.config.logger.debug "Calling API: ZipCountiesApi.zip_counties_get ..."
168
+ end
169
+ # verify the required parameter 'zip_prefix' is set
170
+ fail ArgumentError, "Missing the required parameter 'zip_prefix' when calling ZipCountiesApi.zip_counties_get" if zip_prefix.nil?
171
+ # resource path
172
+ local_var_path = "/zip_counties".sub('{format}','json')
173
+
174
+ # query parameters
175
+ query_params = {}
176
+ query_params[:'zip_prefix'] = zip_prefix
177
+
178
+ # header parameters
179
+ header_params = {}
180
+
181
+ # HTTP header 'Accept' (if needed)
182
+ local_header_accept = []
183
+ local_header_accept_result = @api_client.select_header_accept(local_header_accept) and header_params['Accept'] = local_header_accept_result
184
+
185
+ # HTTP header 'Content-Type'
186
+ local_header_content_type = []
187
+ header_params['Content-Type'] = @api_client.select_header_content_type(local_header_content_type)
188
+
189
+ # form parameters
190
+ form_params = {}
191
+
192
+ # http body (model)
193
+ post_body = nil
194
+ auth_names = []
195
+ data, status_code, headers = @api_client.call_api(:GET, local_var_path,
196
+ :header_params => header_params,
197
+ :query_params => query_params,
198
+ :form_params => form_params,
199
+ :body => post_body,
200
+ :auth_names => auth_names,
201
+ :return_type => 'InlineResponse2002')
202
+ if @api_client.config.debugging
203
+ @api_client.config.logger.debug "API called: ZipCountiesApi#zip_counties_get\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
204
+ end
205
+ return data, status_code, headers
206
+ end
207
+ end
208
+ end
@@ -0,0 +1,474 @@
1
+ =begin
2
+ Vericred API
3
+
4
+ Vericred's API allows you to search for Health Plans that a specific doctor
5
+ accepts.
6
+
7
+ ## Getting Started
8
+
9
+ Visit our [Developer Portal](https://vericred.3scale.net/access_code?access_code=vericred&cms_token=3545ca52af07bde85b7c0c3aa9d1985e) to
10
+ create an account.
11
+
12
+ Once you have created an account, you can create one Application for
13
+ Production and another for our Sandbox (select the appropriate Plan when
14
+ you create the Application).
15
+
16
+ ## Authentication
17
+
18
+ To authenticate, pass the API Key you created in the Developer Portal as
19
+ a `Vericred-Api-Key` header.
20
+
21
+ `curl -H 'Vericred-Api-Key: YOUR_KEY' "https://api.vericred.com/providers?search_term=Foo&zip_code=11215"`
22
+
23
+ ## Versioning
24
+
25
+ Vericred's API default to the latest version. However, if you need a specific
26
+ version, you can request it with an `Accept-Version` header.
27
+
28
+ The current version is `v3`. Previous versions are `v1` and `v2`.
29
+
30
+ `curl -H 'Vericred-Api-Key: YOUR_KEY' -H 'Accept-Version: v2' "https://api.vericred.com/providers?search_term=Foo&zip_code=11215"`
31
+
32
+ ## Pagination
33
+
34
+ Most endpoints are not paginated. It will be noted in the documentation if/when
35
+ an endpoint is paginated.
36
+
37
+ When pagination is present, a `meta` stanza will be present in the response
38
+ with the total number of records
39
+
40
+ ```
41
+ {
42
+ things: [{ id: 1 }, { id: 2 }],
43
+ meta: { total: 500 }
44
+ }
45
+ ```
46
+
47
+ ## Sideloading
48
+
49
+ When we return multiple levels of an object graph (e.g. `Provider`s and their `State`s
50
+ we sideload the associated data. In this example, we would provide an Array of
51
+ `State`s and a `state_id` for each provider. This is done primarily to reduce the
52
+ payload size since many of the `Provider`s will share a `State`
53
+
54
+ ```
55
+ {
56
+ providers: [{ id: 1, state_id: 1}, { id: 2, state_id: 1 }],
57
+ states: [{ id: 1, code: 'NY' }]
58
+ }
59
+ ```
60
+
61
+ If you need the second level of the object graph, you can just match the
62
+ corresponding id.
63
+
64
+ ## Selecting specific data
65
+
66
+ All endpoints allow you to specify which fields you would like to return.
67
+ This allows you to limit the response to contain only the data you need.
68
+
69
+ For example, let's take a request that returns the following JSON by default
70
+
71
+ ```
72
+ {
73
+ provider: {
74
+ id: 1,
75
+ name: 'John',
76
+ phone: '1234567890',
77
+ field_we_dont_care_about: 'value_we_dont_care_about'
78
+ },
79
+ states: [{
80
+ id: 1,
81
+ name: 'New York',
82
+ code: 'NY',
83
+ field_we_dont_care_about: 'value_we_dont_care_about'
84
+ }]
85
+ }
86
+ ```
87
+
88
+ To limit our results to only return the fields we care about, we specify the
89
+ `select` query string parameter for the corresponding fields in the JSON
90
+ document.
91
+
92
+ In this case, we want to select `name` and `phone` from the `provider` key,
93
+ so we would add the parameters `select=provider.name,provider.phone`.
94
+ We also want the `name` and `code` from the `states` key, so we would
95
+ add the parameters `select=states.name,staes.code`. The id field of
96
+ each document is always returned whether or not it is requested.
97
+
98
+ Our final request would be `GET /providers/12345?select=provider.name,provider.phone,states.name,states.code`
99
+
100
+ The response would be
101
+
102
+ ```
103
+ {
104
+ provider: {
105
+ id: 1,
106
+ name: 'John',
107
+ phone: '1234567890'
108
+ },
109
+ states: [{
110
+ id: 1,
111
+ name: 'New York',
112
+ code: 'NY'
113
+ }]
114
+ }
115
+ ```
116
+
117
+
118
+
119
+ OpenAPI spec version:
120
+
121
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
122
+
123
+
124
+ =end
125
+
126
+ require 'date'
127
+ require 'json'
128
+ require 'logger'
129
+ require 'tempfile'
130
+ require 'typhoeus'
131
+ require 'uri'
132
+
133
+ module VericredClient
134
+ class ApiClient
135
+ # The Configuration object holding settings to be used in the API client.
136
+ attr_accessor :config
137
+
138
+ # Defines the headers to be used in HTTP requests of all API calls by default.
139
+ #
140
+ # @return [Hash]
141
+ attr_accessor :default_headers
142
+
143
+ # Initializes the ApiClient
144
+ # @option config [Configuration] Configuraiton for initializing the object, default to Configuration.default
145
+ def initialize(config = Configuration.default)
146
+ @config = config
147
+ @user_agent = "Swagger-Codegen/#{VERSION}/ruby"
148
+ @default_headers = {
149
+ 'Content-Type' => "application/json",
150
+ 'User-Agent' => @user_agent
151
+ }
152
+ end
153
+
154
+ def self.default
155
+ @@default ||= ApiClient.new
156
+ end
157
+
158
+ # Call an API with given options.
159
+ #
160
+ # @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
161
+ # the data deserialized from response body (could be nil), response status code and response headers.
162
+ def call_api(http_method, path, opts = {})
163
+ request = build_request(http_method, path, opts)
164
+ response = request.run
165
+
166
+ if @config.debugging
167
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
168
+ end
169
+
170
+ unless response.success?
171
+ fail ApiError.new(:code => response.code,
172
+ :response_headers => response.headers,
173
+ :response_body => response.body),
174
+ response.status_message
175
+ end
176
+
177
+ if opts[:return_type]
178
+ data = deserialize(response, opts[:return_type])
179
+ else
180
+ data = nil
181
+ end
182
+ return data, response.code, response.headers
183
+ end
184
+
185
+ # Builds the HTTP request
186
+ #
187
+ # @param [String] http_method HTTP method/verb (e.g. POST)
188
+ # @param [String] path URL path (e.g. /account/new)
189
+ # @option opts [Hash] :header_params Header parameters
190
+ # @option opts [Hash] :query_params Query parameters
191
+ # @option opts [Hash] :form_params Query parameters
192
+ # @option opts [Object] :body HTTP body (JSON/XML)
193
+ # @return [Typhoeus::Request] A Typhoeus Request
194
+ def build_request(http_method, path, opts = {})
195
+ url = build_request_url(path)
196
+ http_method = http_method.to_sym.downcase
197
+
198
+ header_params = @default_headers.merge(opts[:header_params] || {})
199
+ query_params = opts[:query_params] || {}
200
+ form_params = opts[:form_params] || {}
201
+
202
+
203
+ req_opts = {
204
+ :method => http_method,
205
+ :headers => header_params,
206
+ :params => query_params,
207
+ :timeout => @config.timeout,
208
+ :ssl_verifypeer => @config.verify_ssl,
209
+ :sslcert => @config.cert_file,
210
+ :sslkey => @config.key_file,
211
+ :verbose => @config.debugging
212
+ }
213
+
214
+ req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
215
+
216
+ if [:post, :patch, :put, :delete].include?(http_method)
217
+ req_body = build_request_body(header_params, form_params, opts[:body])
218
+ req_opts.update :body => req_body
219
+ if @config.debugging
220
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
221
+ end
222
+ end
223
+
224
+ Typhoeus::Request.new(url, req_opts)
225
+ end
226
+
227
+ # Check if the given MIME is a JSON MIME.
228
+ # JSON MIME examples:
229
+ # application/json
230
+ # application/json; charset=UTF8
231
+ # APPLICATION/JSON
232
+ # @param [String] mime MIME
233
+ # @return [Boolean] True if the MIME is applicaton/json
234
+ def json_mime?(mime)
235
+ !(mime =~ /\Aapplication\/json(;.*)?\z/i).nil?
236
+ end
237
+
238
+ # Deserialize the response to the given return type.
239
+ #
240
+ # @param [Response] response HTTP response
241
+ # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
242
+ def deserialize(response, return_type)
243
+ body = response.body
244
+ return nil if body.nil? || body.empty?
245
+
246
+ # return response body directly for String return type
247
+ return body if return_type == 'String'
248
+
249
+ # handle file downloading - save response body into a tmp file and return the File instance
250
+ return download_file(response) if return_type == 'File'
251
+
252
+ # ensuring a default content type
253
+ content_type = response.headers['Content-Type'] || 'application/json'
254
+
255
+ fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
256
+
257
+ begin
258
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
259
+ rescue JSON::ParserError => e
260
+ if %w(String Date DateTime).include?(return_type)
261
+ data = body
262
+ else
263
+ raise e
264
+ end
265
+ end
266
+
267
+ convert_to_type data, return_type
268
+ end
269
+
270
+ # Convert data to the given return type.
271
+ # @param [Object] data Data to be converted
272
+ # @param [String] return_type Return type
273
+ # @return [Mixed] Data in a particular type
274
+ def convert_to_type(data, return_type)
275
+ return nil if data.nil?
276
+ case return_type
277
+ when 'String'
278
+ data.to_s
279
+ when 'Integer'
280
+ data.to_i
281
+ when 'Float'
282
+ data.to_f
283
+ when 'BOOLEAN'
284
+ data == true
285
+ when 'DateTime'
286
+ # parse date time (expecting ISO 8601 format)
287
+ DateTime.parse data
288
+ when 'Date'
289
+ # parse date time (expecting ISO 8601 format)
290
+ Date.parse data
291
+ when 'Object'
292
+ # generic object (usually a Hash), return directly
293
+ data
294
+ when /\AArray<(.+)>\z/
295
+ # e.g. Array<Pet>
296
+ sub_type = $1
297
+ data.map {|item| convert_to_type(item, sub_type) }
298
+ when /\AHash\<String, (.+)\>\z/
299
+ # e.g. Hash<String, Integer>
300
+ sub_type = $1
301
+ {}.tap do |hash|
302
+ data.each {|k, v| hash[k] = convert_to_type(v, sub_type) }
303
+ end
304
+ else
305
+ # models, e.g. Pet
306
+ VericredClient.const_get(return_type).new.tap do |model|
307
+ model.build_from_hash data
308
+ end
309
+ end
310
+ end
311
+
312
+ # Save response body into a file in (the defined) temporary folder, using the filename
313
+ # from the "Content-Disposition" header if provided, otherwise a random filename.
314
+ #
315
+ # @see Configuration#temp_folder_path
316
+ # @return [Tempfile] the file downloaded
317
+ def download_file(response)
318
+ content_disposition = response.headers['Content-Disposition']
319
+ if content_disposition
320
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
321
+ prefix = sanitize_filename(filename)
322
+ else
323
+ prefix = 'download-'
324
+ end
325
+ prefix = prefix + '-' unless prefix.end_with?('-')
326
+
327
+ tempfile = nil
328
+ encoding = response.body.encoding
329
+ Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) do |file|
330
+ file.write(response.body)
331
+ tempfile = file
332
+ end
333
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
334
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
335
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
336
+ "explicitly with `tempfile.delete`"
337
+ tempfile
338
+ end
339
+
340
+ # Sanitize filename by removing path.
341
+ # e.g. ../../sun.gif becomes sun.gif
342
+ #
343
+ # @param [String] filename the filename to be sanitized
344
+ # @return [String] the sanitized filename
345
+ def sanitize_filename(filename)
346
+ filename.gsub(/.*[\/\\]/, '')
347
+ end
348
+
349
+ def build_request_url(path)
350
+ # Add leading and trailing slashes to path
351
+ path = "/#{path}".gsub(/\/+/, '/')
352
+ URI.encode(@config.base_url + path)
353
+ end
354
+
355
+ # Builds the HTTP request body
356
+ #
357
+ # @param [Hash] header_params Header parameters
358
+ # @param [Hash] form_params Query parameters
359
+ # @param [Object] body HTTP body (JSON/XML)
360
+ # @return [String] HTTP body data in the form of string
361
+ def build_request_body(header_params, form_params, body)
362
+ # http form
363
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
364
+ header_params['Content-Type'] == 'multipart/form-data'
365
+ data = {}
366
+ form_params.each do |key, value|
367
+ case value
368
+ when File, Array, nil
369
+ # let typhoeus handle File, Array and nil parameters
370
+ data[key] = value
371
+ else
372
+ data[key] = value.to_s
373
+ end
374
+ end
375
+ elsif body
376
+ data = body.is_a?(String) ? body : body.to_json
377
+ else
378
+ data = nil
379
+ end
380
+ data
381
+ end
382
+
383
+ # Update hearder and query params based on authentication settings.
384
+ #
385
+ # @param [Hash] header_params Header parameters
386
+ # @param [Hash] form_params Query parameters
387
+ # @param [String] auth_names Authentication scheme name
388
+ def update_params_for_auth!(header_params, query_params, auth_names)
389
+ Array(auth_names).each do |auth_name|
390
+ auth_setting = @config.auth_settings[auth_name]
391
+ next unless auth_setting
392
+ case auth_setting[:in]
393
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
394
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
395
+ else fail ArgumentError, 'Authentication token must be in `query` of `header`'
396
+ end
397
+ end
398
+ end
399
+
400
+ # Sets user agent in HTTP header
401
+ #
402
+ # @param [String] user_agent User agent (e.g. swagger-codegen/ruby/1.0.0)
403
+ def user_agent=(user_agent)
404
+ @user_agent = user_agent
405
+ @default_headers['User-Agent'] = @user_agent
406
+ end
407
+
408
+ # Return Accept header based on an array of accepts provided.
409
+ # @param [Array] accepts array for Accept
410
+ # @return [String] the Accept header (e.g. application/json)
411
+ def select_header_accept(accepts)
412
+ return nil if accepts.nil? || accepts.empty?
413
+ # use JSON when present, otherwise use all of the provided
414
+ json_accept = accepts.find { |s| json_mime?(s) }
415
+ return json_accept || accepts.join(',')
416
+ end
417
+
418
+ # Return Content-Type header based on an array of content types provided.
419
+ # @param [Array] content_types array for Content-Type
420
+ # @return [String] the Content-Type header (e.g. application/json)
421
+ def select_header_content_type(content_types)
422
+ # use application/json by default
423
+ return 'application/json' if content_types.nil? || content_types.empty?
424
+ # use JSON when present, otherwise use the first one
425
+ json_content_type = content_types.find { |s| json_mime?(s) }
426
+ return json_content_type || content_types.first
427
+ end
428
+
429
+ # Convert object (array, hash, object, etc) to JSON string.
430
+ # @param [Object] model object to be converted into JSON string
431
+ # @return [String] JSON string representation of the object
432
+ def object_to_http_body(model)
433
+ return model if model.nil? || model.is_a?(String)
434
+ local_body = nil
435
+ if model.is_a?(Array)
436
+ local_body = model.map{|m| object_to_hash(m) }
437
+ else
438
+ local_body = object_to_hash(model)
439
+ end
440
+ local_body.to_json
441
+ end
442
+
443
+ # Convert object(non-array) to hash.
444
+ # @param [Object] obj object to be converted into JSON string
445
+ # @return [String] JSON string representation of the object
446
+ def object_to_hash(obj)
447
+ if obj.respond_to?(:to_hash)
448
+ obj.to_hash
449
+ else
450
+ obj
451
+ end
452
+ end
453
+
454
+ # Build parameter value according to the given collection format.
455
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
456
+ def build_collection_param(param, collection_format)
457
+ case collection_format
458
+ when :csv
459
+ param.join(',')
460
+ when :ssv
461
+ param.join(' ')
462
+ when :tsv
463
+ param.join("\t")
464
+ when :pipes
465
+ param.join('|')
466
+ when :multi
467
+ # return the array directly as typhoeus will handle it as expected
468
+ param
469
+ else
470
+ fail "unknown collection format: #{collection_format.inspect}"
471
+ end
472
+ end
473
+ end
474
+ end