marketcheck_ruby_client 1.0.7

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 (89) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +133 -0
  3. data/docs/Averages.md +13 -0
  4. data/docs/BaseListing.md +27 -0
  5. data/docs/Build.md +19 -0
  6. data/docs/Dealer.md +23 -0
  7. data/docs/DealerApi.md +230 -0
  8. data/docs/Error.md +9 -0
  9. data/docs/FacetItem.md +9 -0
  10. data/docs/FacetsApi.md +71 -0
  11. data/docs/GraphsApi.md +73 -0
  12. data/docs/HistoricalListing.md +19 -0
  13. data/docs/HistoryApi.md +65 -0
  14. data/docs/InventoryApi.md +113 -0
  15. data/docs/Listing.md +23 -0
  16. data/docs/ListingDebugAttributes.md +13 -0
  17. data/docs/ListingExtraAttributes.md +10 -0
  18. data/docs/ListingMedia.md +9 -0
  19. data/docs/ListingVDP.md +10 -0
  20. data/docs/ListingsApi.md +324 -0
  21. data/docs/Location.md +12 -0
  22. data/docs/MakeModel.md +9 -0
  23. data/docs/MarketAveragesApi.md +70 -0
  24. data/docs/MarketTrendsApi.md +72 -0
  25. data/docs/PlotPoint.md +14 -0
  26. data/docs/SearchResponse.md +9 -0
  27. data/docs/TrendPoint.md +12 -0
  28. data/docs/VINDecoderApi.md +59 -0
  29. data/git_push.sh +52 -0
  30. data/lib/marketcheck_ruby_client.rb +65 -0
  31. data/lib/swagger_client/api/dealer_api.rb +312 -0
  32. data/lib/swagger_client/api/facets_api.rb +105 -0
  33. data/lib/swagger_client/api/graphs_api.rb +108 -0
  34. data/lib/swagger_client/api/history_api.rb +95 -0
  35. data/lib/swagger_client/api/inventory_api.rb +155 -0
  36. data/lib/swagger_client/api/listings_api.rb +474 -0
  37. data/lib/swagger_client/api/market_averages_api.rb +102 -0
  38. data/lib/swagger_client/api/market_trends_api.rb +108 -0
  39. data/lib/swagger_client/api/vin_decoder_api.rb +89 -0
  40. data/lib/swagger_client/api_client.rb +332 -0
  41. data/lib/swagger_client/api_error.rb +36 -0
  42. data/lib/swagger_client/configuration.rb +163 -0
  43. data/lib/swagger_client/models/averages.rb +217 -0
  44. data/lib/swagger_client/models/base_listing.rb +370 -0
  45. data/lib/swagger_client/models/build.rb +284 -0
  46. data/lib/swagger_client/models/dealer.rb +329 -0
  47. data/lib/swagger_client/models/error.rb +173 -0
  48. data/lib/swagger_client/models/facet_item.rb +173 -0
  49. data/lib/swagger_client/models/historical_listing.rb +284 -0
  50. data/lib/swagger_client/models/listing.rb +327 -0
  51. data/lib/swagger_client/models/listing_debug_attributes.rb +217 -0
  52. data/lib/swagger_client/models/listing_extra_attributes.rb +188 -0
  53. data/lib/swagger_client/models/listing_media.rb +175 -0
  54. data/lib/swagger_client/models/listing_vdp.rb +185 -0
  55. data/lib/swagger_client/models/location.rb +207 -0
  56. data/lib/swagger_client/models/make_model.rb +171 -0
  57. data/lib/swagger_client/models/plot_point.rb +228 -0
  58. data/lib/swagger_client/models/search_response.rb +175 -0
  59. data/lib/swagger_client/models/trend_point.rb +206 -0
  60. data/lib/swagger_client/version.rb +15 -0
  61. data/marketcheck_ruby_client-1.0.6.gem +0 -0
  62. data/marketcheck_ruby_client.gemspec +32 -0
  63. data/spec/api/dealer_api_spec.rb +109 -0
  64. data/spec/api/facets_api_spec.rb +57 -0
  65. data/spec/api/graphs_api_spec.rb +58 -0
  66. data/spec/api/history_api_spec.rb +54 -0
  67. data/spec/api/inventory_api_spec.rb +70 -0
  68. data/spec/api/listings_api_spec.rb +148 -0
  69. data/spec/api/market_averages_api_spec.rb +57 -0
  70. data/spec/api/market_trends_api_spec.rb +57 -0
  71. data/spec/api/vin_decoder_api_spec.rb +52 -0
  72. data/spec/models/averages_spec.rb +96 -0
  73. data/spec/models/base_listing_spec.rb +236 -0
  74. data/spec/models/build_spec.rb +156 -0
  75. data/spec/models/dealer_spec.rb +196 -0
  76. data/spec/models/error_spec.rb +56 -0
  77. data/spec/models/facet_item_spec.rb +56 -0
  78. data/spec/models/historical_listing_spec.rb +156 -0
  79. data/spec/models/listing_debug_attributes_spec.rb +96 -0
  80. data/spec/models/listing_extra_attributes_spec.rb +66 -0
  81. data/spec/models/listing_media_spec.rb +56 -0
  82. data/spec/models/listing_spec.rb +196 -0
  83. data/spec/models/listing_vdp_spec.rb +66 -0
  84. data/spec/models/location_spec.rb +86 -0
  85. data/spec/models/make_model_spec.rb +56 -0
  86. data/spec/models/plot_point_spec.rb +106 -0
  87. data/spec/models/search_response_spec.rb +56 -0
  88. data/spec/models/trend_point_spec.rb +86 -0
  89. metadata +337 -0
@@ -0,0 +1,102 @@
1
+ =begin
2
+ Marketcheck Cars API
3
+
4
+ <b>Access the New, Used and Certified cars inventories for all Car Dealers in US.</b> <br/>The data is sourced from online listings by over 40,000 Car dealers in US. At any time, there are about 5.2M searchable listings (about 1.8M unique VINs) for Used & Certified cars and about 5M (about 2.3M unique VINs) New Car listings from all over US. We use this API at the back for our website <a href='http://www.marketcheck.com' target='_blank'>www.marketcheck.com</a> and our Android and iOS mobile apps too.<br/><h5> Few useful links : </h5><ul><li>A quick view of the API and the use cases is depicated <a href='https://portals.marketcheck.com/mcapi/' target='_blank'>here</a></li><li>The Postman collection with various usages of the API is shared here https://www.getpostman.com/collections/2752684ff636cdd7bac2</li><li> [Coming soon] API Key : Request your API Key <a href='https://portals.marketcheck.com/api' target='_blank'> here </a> </li><li> [Coming soon] Benchmarks : See the API benchmarks <a href='https://portals.marketcheck.com/mcapi/benchmarks'>here</a></li></ul>
5
+
6
+ OpenAPI spec version: 1.0.3
7
+
8
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
9
+
10
+
11
+ =end
12
+
13
+ require "uri"
14
+
15
+ module SwaggerClient
16
+ class MarketAveragesApi
17
+ attr_accessor :api_client
18
+
19
+ def initialize(api_client = ApiClient.default)
20
+ @api_client = api_client
21
+ end
22
+
23
+ # Get Averages for YMM
24
+ # [Merged with the /search API - Please check the 'stats' parameter to the Search API above] Get market averages for price / miles / msrp / dom (days on market) fields for active cars matching the given reference VIN's basic specification or Year, Make, Model, Trim (Optional) criteria
25
+ # @param api_key The API Authentication Key. Mandatory with all API calls.
26
+ # @param [Hash] opts the optional parameters
27
+ # @option opts [String] :vin VIN as a reference to the type of car for which averages data is to be returned
28
+ # @option opts [String] :year Year of the car
29
+ # @option opts [String] :make Make of the car
30
+ # @option opts [String] :model Model of the Car
31
+ # @option opts [String] :trim Trim of the Car
32
+ # @option opts [String] :fields Comma separated list of fields to generate stats for. Allowed fields in the list are - price, miles, msrp, dom (days on market)
33
+ # @return [Averages]
34
+ def get_averages(api_key, opts = {})
35
+ data, status_code, headers = get_averages_with_http_info(api_key, opts)
36
+ return data
37
+ end
38
+
39
+ # Get Averages for YMM
40
+ # [Merged with the /search API - Please check the &#39;stats&#39; parameter to the Search API above] Get market averages for price / miles / msrp / dom (days on market) fields for active cars matching the given reference VIN&#39;s basic specification or Year, Make, Model, Trim (Optional) criteria
41
+ # @param api_key The API Authentication Key. Mandatory with all API calls.
42
+ # @param [Hash] opts the optional parameters
43
+ # @option opts [String] :vin VIN as a reference to the type of car for which averages data is to be returned
44
+ # @option opts [String] :year Year of the car
45
+ # @option opts [String] :make Make of the car
46
+ # @option opts [String] :model Model of the Car
47
+ # @option opts [String] :trim Trim of the Car
48
+ # @option opts [String] :fields Comma separated list of fields to generate stats for. Allowed fields in the list are - price, miles, msrp, dom (days on market)
49
+ # @return [Array<(Averages, Fixnum, Hash)>] Averages data, response status code and response headers
50
+ def get_averages_with_http_info(api_key, opts = {})
51
+ if @api_client.config.debugging
52
+ @api_client.config.logger.debug "Calling API: MarketAveragesApi#get_averages ..."
53
+ end
54
+
55
+ # verify the required parameter 'api_key' is set
56
+ fail "Missing the required parameter 'api_key' when calling get_averages" if api_key.nil?
57
+
58
+ # resource path
59
+ local_var_path = "/averages".sub('{format}','json')
60
+
61
+ # query parameters
62
+ query_params = {}
63
+ query_params[:'api_key'] = api_key
64
+ query_params[:'vin'] = opts[:'vin'] if opts[:'vin']
65
+ query_params[:'year'] = opts[:'year'] if opts[:'year']
66
+ query_params[:'make'] = opts[:'make'] if opts[:'make']
67
+ query_params[:'model'] = opts[:'model'] if opts[:'model']
68
+ query_params[:'trim'] = opts[:'trim'] if opts[:'trim']
69
+ query_params[:'fields'] = opts[:'fields'] if opts[:'fields']
70
+
71
+ # header parameters
72
+ header_params = {}
73
+
74
+ # HTTP header 'Accept' (if needed)
75
+ _header_accept = ['application/json']
76
+ _header_accept_result = @api_client.select_header_accept(_header_accept) and header_params['Accept'] = _header_accept_result
77
+
78
+ # HTTP header 'Content-Type'
79
+ _header_content_type = []
80
+ header_params['Content-Type'] = @api_client.select_header_content_type(_header_content_type)
81
+
82
+ # form parameters
83
+ form_params = {}
84
+
85
+ # http body (model)
86
+ post_body = nil
87
+
88
+ auth_names = []
89
+ data, status_code, headers = @api_client.call_api(:GET, local_var_path,
90
+ :header_params => header_params,
91
+ :query_params => query_params,
92
+ :form_params => form_params,
93
+ :body => post_body,
94
+ :auth_names => auth_names,
95
+ :return_type => 'Averages')
96
+ if @api_client.config.debugging
97
+ @api_client.config.logger.debug "API called: MarketAveragesApi#get_averages\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
98
+ end
99
+ return data, status_code, headers
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,108 @@
1
+ =begin
2
+ Marketcheck Cars API
3
+
4
+ <b>Access the New, Used and Certified cars inventories for all Car Dealers in US.</b> <br/>The data is sourced from online listings by over 40,000 Car dealers in US. At any time, there are about 5.2M searchable listings (about 1.8M unique VINs) for Used & Certified cars and about 5M (about 2.3M unique VINs) New Car listings from all over US. We use this API at the back for our website <a href='http://www.marketcheck.com' target='_blank'>www.marketcheck.com</a> and our Android and iOS mobile apps too.<br/><h5> Few useful links : </h5><ul><li>A quick view of the API and the use cases is depicated <a href='https://portals.marketcheck.com/mcapi/' target='_blank'>here</a></li><li>The Postman collection with various usages of the API is shared here https://www.getpostman.com/collections/2752684ff636cdd7bac2</li><li> [Coming soon] API Key : Request your API Key <a href='https://portals.marketcheck.com/api' target='_blank'> here </a> </li><li> [Coming soon] Benchmarks : See the API benchmarks <a href='https://portals.marketcheck.com/mcapi/benchmarks'>here</a></li></ul>
5
+
6
+ OpenAPI spec version: 1.0.3
7
+
8
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
9
+
10
+
11
+ =end
12
+
13
+ require "uri"
14
+
15
+ module SwaggerClient
16
+ class MarketTrendsApi
17
+ attr_accessor :api_client
18
+
19
+ def initialize(api_client = ApiClient.default)
20
+ @api_client = api_client
21
+ end
22
+
23
+ # Get Trends for criteria
24
+ # Get historical market trends for cars matching the given VIN's basic specification or Year, Make, Model, Trim (Optional) criteria
25
+ # @param api_key The API Authentication Key. Mandatory with all API calls.
26
+ # @param vin VIN as a reference to the type of car for which trend data is to be returned
27
+ # @param car_type Car type. Allowed values are - new / used / certified
28
+ # @param [Hash] opts the optional parameters
29
+ # @option opts [String] :year Year of the car
30
+ # @option opts [String] :make Make of the car
31
+ # @option opts [String] :model Model of the Car
32
+ # @option opts [String] :trim Trim of the Car
33
+ # @return [Array<TrendPoint>]
34
+ def get_trends(api_key, vin, car_type, opts = {})
35
+ data, status_code, headers = get_trends_with_http_info(api_key, vin, car_type, opts)
36
+ return data
37
+ end
38
+
39
+ # Get Trends for criteria
40
+ # Get historical market trends for cars matching the given VIN&#39;s basic specification or Year, Make, Model, Trim (Optional) criteria
41
+ # @param api_key The API Authentication Key. Mandatory with all API calls.
42
+ # @param vin VIN as a reference to the type of car for which trend data is to be returned
43
+ # @param car_type Car type. Allowed values are - new / used / certified
44
+ # @param [Hash] opts the optional parameters
45
+ # @option opts [String] :year Year of the car
46
+ # @option opts [String] :make Make of the car
47
+ # @option opts [String] :model Model of the Car
48
+ # @option opts [String] :trim Trim of the Car
49
+ # @return [Array<(Array<TrendPoint>, Fixnum, Hash)>] Array<TrendPoint> data, response status code and response headers
50
+ def get_trends_with_http_info(api_key, vin, car_type, opts = {})
51
+ if @api_client.config.debugging
52
+ @api_client.config.logger.debug "Calling API: MarketTrendsApi#get_trends ..."
53
+ end
54
+
55
+ # verify the required parameter 'api_key' is set
56
+ fail "Missing the required parameter 'api_key' when calling get_trends" if api_key.nil?
57
+
58
+ # verify the required parameter 'vin' is set
59
+ fail "Missing the required parameter 'vin' when calling get_trends" if vin.nil?
60
+
61
+ # verify the required parameter 'car_type' is set
62
+ fail "Missing the required parameter 'car_type' when calling get_trends" if car_type.nil?
63
+
64
+ # resource path
65
+ local_var_path = "/trends".sub('{format}','json')
66
+
67
+ # query parameters
68
+ query_params = {}
69
+ query_params[:'api_key'] = api_key
70
+ query_params[:'vin'] = vin
71
+ query_params[:'car_type'] = car_type
72
+ query_params[:'year'] = opts[:'year'] if opts[:'year']
73
+ query_params[:'make'] = opts[:'make'] if opts[:'make']
74
+ query_params[:'model'] = opts[:'model'] if opts[:'model']
75
+ query_params[:'trim'] = opts[:'trim'] if opts[:'trim']
76
+
77
+ # header parameters
78
+ header_params = {}
79
+
80
+ # HTTP header 'Accept' (if needed)
81
+ _header_accept = ['application/json']
82
+ _header_accept_result = @api_client.select_header_accept(_header_accept) and header_params['Accept'] = _header_accept_result
83
+
84
+ # HTTP header 'Content-Type'
85
+ _header_content_type = []
86
+ header_params['Content-Type'] = @api_client.select_header_content_type(_header_content_type)
87
+
88
+ # form parameters
89
+ form_params = {}
90
+
91
+ # http body (model)
92
+ post_body = nil
93
+
94
+ auth_names = []
95
+ data, status_code, headers = @api_client.call_api(:GET, local_var_path,
96
+ :header_params => header_params,
97
+ :query_params => query_params,
98
+ :form_params => form_params,
99
+ :body => post_body,
100
+ :auth_names => auth_names,
101
+ :return_type => 'Array<TrendPoint>')
102
+ if @api_client.config.debugging
103
+ @api_client.config.logger.debug "API called: MarketTrendsApi#get_trends\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
104
+ end
105
+ return data, status_code, headers
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,89 @@
1
+ =begin
2
+ Marketcheck Cars API
3
+
4
+ <b>Access the New, Used and Certified cars inventories for all Car Dealers in US.</b> <br/>The data is sourced from online listings by over 40,000 Car dealers in US. At any time, there are about 5.2M searchable listings (about 1.8M unique VINs) for Used & Certified cars and about 5M (about 2.3M unique VINs) New Car listings from all over US. We use this API at the back for our website <a href='http://www.marketcheck.com' target='_blank'>www.marketcheck.com</a> and our Android and iOS mobile apps too.<br/><h5> Few useful links : </h5><ul><li>A quick view of the API and the use cases is depicated <a href='https://portals.marketcheck.com/mcapi/' target='_blank'>here</a></li><li>The Postman collection with various usages of the API is shared here https://www.getpostman.com/collections/2752684ff636cdd7bac2</li><li> [Coming soon] API Key : Request your API Key <a href='https://portals.marketcheck.com/api' target='_blank'> here </a> </li><li> [Coming soon] Benchmarks : See the API benchmarks <a href='https://portals.marketcheck.com/mcapi/benchmarks'>here</a></li></ul>
5
+
6
+ OpenAPI spec version: 1.0.3
7
+
8
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
9
+
10
+
11
+ =end
12
+
13
+ require "uri"
14
+
15
+ module SwaggerClient
16
+ class VINDecoderApi
17
+ attr_accessor :api_client
18
+
19
+ def initialize(api_client = ApiClient.default)
20
+ @api_client = api_client
21
+ end
22
+
23
+ # VIN Decoder
24
+ # Get the basic information on specifications for a car identified by a valid VIN
25
+ # @param api_key The API Authentication Key. Mandatory with all API calls.
26
+ # @param vin VIN to decode
27
+ # @param [Hash] opts the optional parameters
28
+ # @return [Build]
29
+ def decode(api_key, vin, opts = {})
30
+ data, status_code, headers = decode_with_http_info(api_key, vin, opts)
31
+ return data
32
+ end
33
+
34
+ # VIN Decoder
35
+ # Get the basic information on specifications for a car identified by a valid VIN
36
+ # @param api_key The API Authentication Key. Mandatory with all API calls.
37
+ # @param vin VIN to decode
38
+ # @param [Hash] opts the optional parameters
39
+ # @return [Array<(Build, Fixnum, Hash)>] Build data, response status code and response headers
40
+ def decode_with_http_info(api_key, vin, opts = {})
41
+ if @api_client.config.debugging
42
+ @api_client.config.logger.debug "Calling API: VINDecoderApi#decode ..."
43
+ end
44
+
45
+ # verify the required parameter 'api_key' is set
46
+ fail "Missing the required parameter 'api_key' when calling decode" if api_key.nil?
47
+
48
+ # verify the required parameter 'vin' is set
49
+ fail "Missing the required parameter 'vin' when calling decode" if vin.nil?
50
+
51
+ # resource path
52
+ local_var_path = "/vin/{vin}/specs".sub('{format}','json').sub('{' + 'vin' + '}', vin.to_s)
53
+
54
+ # query parameters
55
+ query_params = {}
56
+ query_params[:'api_key'] = api_key
57
+
58
+ # header parameters
59
+ header_params = {}
60
+
61
+ # HTTP header 'Accept' (if needed)
62
+ _header_accept = ['application/json']
63
+ _header_accept_result = @api_client.select_header_accept(_header_accept) and header_params['Accept'] = _header_accept_result
64
+
65
+ # HTTP header 'Content-Type'
66
+ _header_content_type = []
67
+ header_params['Content-Type'] = @api_client.select_header_content_type(_header_content_type)
68
+
69
+ # form parameters
70
+ form_params = {}
71
+
72
+ # http body (model)
73
+ post_body = nil
74
+
75
+ auth_names = []
76
+ data, status_code, headers = @api_client.call_api(:GET, local_var_path,
77
+ :header_params => header_params,
78
+ :query_params => query_params,
79
+ :form_params => form_params,
80
+ :body => post_body,
81
+ :auth_names => auth_names,
82
+ :return_type => 'Build')
83
+ if @api_client.config.debugging
84
+ @api_client.config.logger.debug "API called: VINDecoderApi#decode\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
85
+ end
86
+ return data, status_code, headers
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,332 @@
1
+ =begin
2
+ Marketcheck Cars API
3
+
4
+ <b>Access the New, Used and Certified cars inventories for all Car Dealers in US.</b> <br/>The data is sourced from online listings by over 40,000 Car dealers in US. At any time, there are about 5.2M searchable listings (about 1.8M unique VINs) for Used & Certified cars and about 5M (about 2.3M unique VINs) New Car listings from all over US. We use this API at the back for our website <a href='http://www.marketcheck.com' target='_blank'>www.marketcheck.com</a> and our Android and iOS mobile apps too.<br/><h5> Few useful links : </h5><ul><li>A quick view of the API and the use cases is depicated <a href='https://portals.marketcheck.com/mcapi/' target='_blank'>here</a></li><li>The Postman collection with various usages of the API is shared here https://www.getpostman.com/collections/2752684ff636cdd7bac2</li><li> [Coming soon] API Key : Request your API Key <a href='https://portals.marketcheck.com/api' target='_blank'> here </a> </li><li> [Coming soon] Benchmarks : See the API benchmarks <a href='https://portals.marketcheck.com/mcapi/benchmarks'>here</a></li></ul>
5
+
6
+ OpenAPI spec version: 1.0.0
7
+
8
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
9
+
10
+
11
+ =end
12
+
13
+ require 'date'
14
+ require 'json'
15
+ require 'logger'
16
+ require 'tempfile'
17
+ require 'typhoeus'
18
+ require 'uri'
19
+
20
+ module SwaggerClient
21
+ class ApiClient
22
+ # The Configuration object holding settings to be used in the API client.
23
+ attr_accessor :config
24
+
25
+ # Defines the headers to be used in HTTP requests of all API calls by default.
26
+ #
27
+ # @return [Hash]
28
+ attr_accessor :default_headers
29
+
30
+ def initialize(config = Configuration.default)
31
+ @config = config
32
+ @user_agent = "Swagger-Codegen/#{VERSION}/ruby"
33
+ @default_headers = {
34
+ 'Content-Type' => "application/json",
35
+ 'User-Agent' => @user_agent
36
+ }
37
+ end
38
+
39
+ def self.default
40
+ @@default ||= ApiClient.new
41
+ end
42
+
43
+ # Call an API with given options.
44
+ #
45
+ # @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
46
+ # the data deserialized from response body (could be nil), response status code and response headers.
47
+ def call_api(http_method, path, opts = {})
48
+ request = build_request(http_method, path, opts)
49
+ response = request.run
50
+
51
+ if @config.debugging
52
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
53
+ end
54
+
55
+ unless response.success?
56
+ fail ApiError.new(:code => response.code,
57
+ :response_headers => response.headers,
58
+ :response_body => response.body),
59
+ response.status_message
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
+ def build_request(http_method, path, opts = {})
71
+ url = build_request_url(path)
72
+ http_method = http_method.to_sym.downcase
73
+
74
+ header_params = @default_headers.merge(opts[:header_params] || {})
75
+ query_params = opts[:query_params] || {}
76
+ form_params = opts[:form_params] || {}
77
+
78
+
79
+
80
+ req_opts = {
81
+ :method => http_method,
82
+ :headers => header_params,
83
+ :params => query_params,
84
+ :timeout => @config.timeout,
85
+ :ssl_verifypeer => @config.verify_ssl,
86
+ :sslcert => @config.cert_file,
87
+ :sslkey => @config.key_file,
88
+ :verbose => @config.debugging
89
+ }
90
+
91
+ req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
92
+
93
+ if [:post, :patch, :put, :delete].include?(http_method)
94
+ req_body = build_request_body(header_params, form_params, opts[:body])
95
+ req_opts.update :body => req_body
96
+ if @config.debugging
97
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
98
+ end
99
+ end
100
+
101
+ Typhoeus::Request.new(url, req_opts)
102
+ end
103
+
104
+ # Check if the given MIME is a JSON MIME.
105
+ # JSON MIME examples:
106
+ # application/json
107
+ # application/json; charset=UTF8
108
+ # APPLICATION/JSON
109
+ def json_mime?(mime)
110
+ !!(mime =~ /\Aapplication\/json(;.*)?\z/i)
111
+ end
112
+
113
+ # Deserialize the response to the given return type.
114
+ #
115
+ # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
116
+ def deserialize(response, return_type)
117
+ body = response.body
118
+ return nil if body.nil? || body.empty?
119
+
120
+ # return response body directly for String return type
121
+ return body if return_type == 'String'
122
+
123
+ # handle file downloading - save response body into a tmp file and return the File instance
124
+ return download_file(response) if return_type == 'File'
125
+
126
+ # ensuring a default content type
127
+ content_type = response.headers['Content-Type'] || 'application/json'
128
+
129
+ fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
130
+
131
+ begin
132
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
133
+ rescue JSON::ParserError => e
134
+ if %w(String Date DateTime).include?(return_type)
135
+ data = body
136
+ else
137
+ raise e
138
+ end
139
+ end
140
+
141
+ convert_to_type data, return_type
142
+ end
143
+
144
+ # Convert data to the given return type.
145
+ def convert_to_type(data, return_type)
146
+ return nil if data.nil?
147
+ case return_type
148
+ when 'String'
149
+ data.to_s
150
+ when 'Integer'
151
+ data.to_i
152
+ when 'Float'
153
+ data.to_f
154
+ when 'BOOLEAN'
155
+ data == true
156
+ when 'DateTime'
157
+ # parse date time (expecting ISO 8601 format)
158
+ DateTime.parse data
159
+ when 'Date'
160
+ # parse date time (expecting ISO 8601 format)
161
+ Date.parse data
162
+ when 'Object'
163
+ # generic object (usually a Hash), return directly
164
+ data
165
+ when /\AArray<(.+)>\z/
166
+ # e.g. Array<Pet>
167
+ sub_type = $1
168
+ data.map {|item| convert_to_type(item, sub_type) }
169
+ when /\AHash\<String, (.+)\>\z/
170
+ # e.g. Hash<String, Integer>
171
+ sub_type = $1
172
+ {}.tap do |hash|
173
+ data.each {|k, v| hash[k] = convert_to_type(v, sub_type) }
174
+ end
175
+ else
176
+ # models, e.g. Pet
177
+ SwaggerClient.const_get(return_type).new.tap do |model|
178
+ model.build_from_hash data
179
+ end
180
+ end
181
+ end
182
+
183
+ # Save response body into a file in (the defined) temporary folder, using the filename
184
+ # from the "Content-Disposition" header if provided, otherwise a random filename.
185
+ #
186
+ # @see Configuration#temp_folder_path
187
+ # @return [Tempfile] the file downloaded
188
+ def download_file(response)
189
+ content_disposition = response.headers['Content-Disposition']
190
+ if content_disposition
191
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
192
+ prefix = sanitize_filename(filename)
193
+ else
194
+ prefix = 'download-'
195
+ end
196
+ prefix = prefix + '-' unless prefix.end_with?('-')
197
+
198
+ tempfile = nil
199
+ encoding = response.body.encoding
200
+ Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) do |file|
201
+ file.write(response.body)
202
+ tempfile = file
203
+ end
204
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
205
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
206
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
207
+ "explicitly with `tempfile.delete`"
208
+ tempfile
209
+ end
210
+
211
+ # Sanitize filename by removing path.
212
+ # e.g. ../../sun.gif becomes sun.gif
213
+ #
214
+ # @param [String] filename the filename to be sanitized
215
+ # @return [String] the sanitized filename
216
+ def sanitize_filename(filename)
217
+ filename.gsub /.*[\/\\]/, ''
218
+ end
219
+
220
+ def build_request_url(path)
221
+ # Add leading and trailing slashes to path
222
+ path = "/#{path}".gsub(/\/+/, '/')
223
+ URI.encode(@config.base_url + path)
224
+ end
225
+
226
+ def build_request_body(header_params, form_params, body)
227
+ # http form
228
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
229
+ header_params['Content-Type'] == 'multipart/form-data'
230
+ data = {}
231
+ form_params.each do |key, value|
232
+ case value
233
+ when File, Array, nil
234
+ # let typhoeus handle File, Array and nil parameters
235
+ data[key] = value
236
+ else
237
+ data[key] = value.to_s
238
+ end
239
+ end
240
+ elsif body
241
+ data = body.is_a?(String) ? body : body.to_json
242
+ else
243
+ data = nil
244
+ end
245
+ data
246
+ end
247
+
248
+ # Update hearder and query params based on authentication settings.
249
+ def update_params_for_auth!(header_params, query_params, auth_names)
250
+ Array(auth_names).each do |auth_name|
251
+ auth_setting = @config.auth_settings[auth_name]
252
+ next unless auth_setting
253
+ case auth_setting[:in]
254
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
255
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
256
+ else fail ArgumentError, 'Authentication token must be in `query` of `header`'
257
+ end
258
+ end
259
+ end
260
+
261
+ def user_agent=(user_agent)
262
+ @user_agent = user_agent
263
+ @default_headers['User-Agent'] = @user_agent
264
+ end
265
+
266
+ # Return Accept header based on an array of accepts provided.
267
+ # @param [Array] accepts array for Accept
268
+ # @return [String] the Accept header (e.g. application/json)
269
+ def select_header_accept(accepts)
270
+ return nil if accepts.nil? || accepts.empty?
271
+ # use JSON when present, otherwise use all of the provided
272
+ json_accept = accepts.find { |s| json_mime?(s) }
273
+ return json_accept || accepts.join(',')
274
+ end
275
+
276
+ # Return Content-Type header based on an array of content types provided.
277
+ # @param [Array] content_types array for Content-Type
278
+ # @return [String] the Content-Type header (e.g. application/json)
279
+ def select_header_content_type(content_types)
280
+ # use application/json by default
281
+ return 'application/json' if content_types.nil? || content_types.empty?
282
+ # use JSON when present, otherwise use the first one
283
+ json_content_type = content_types.find { |s| json_mime?(s) }
284
+ return json_content_type || content_types.first
285
+ end
286
+
287
+ # Convert object (array, hash, object, etc) to JSON string.
288
+ # @param [Object] model object to be converted into JSON string
289
+ # @return [String] JSON string representation of the object
290
+ def object_to_http_body(model)
291
+ return model if model.nil? || model.is_a?(String)
292
+ _body = nil
293
+ if model.is_a?(Array)
294
+ _body = model.map{|m| object_to_hash(m) }
295
+ else
296
+ _body = object_to_hash(model)
297
+ end
298
+ _body.to_json
299
+ end
300
+
301
+ # Convert object(non-array) to hash.
302
+ # @param [Object] obj object to be converted into JSON string
303
+ # @return [String] JSON string representation of the object
304
+ def object_to_hash(obj)
305
+ if obj.respond_to?(:to_hash)
306
+ obj.to_hash
307
+ else
308
+ obj
309
+ end
310
+ end
311
+
312
+ # Build parameter value according to the given collection format.
313
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
314
+ def build_collection_param(param, collection_format)
315
+ case collection_format
316
+ when :csv
317
+ param.join(',')
318
+ when :ssv
319
+ param.join(' ')
320
+ when :tsv
321
+ param.join("\t")
322
+ when :pipes
323
+ param.join('|')
324
+ when :multi
325
+ # return the array directly as typhoeus will handle it as expected
326
+ param
327
+ else
328
+ fail "unknown collection format: #{collection_format.inspect}"
329
+ end
330
+ end
331
+ end
332
+ end