agrid-client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +5 -0
  3. data/Gemfile.lock +65 -0
  4. data/README.md +15 -0
  5. data/agrid_client.gemspec +32 -0
  6. data/lib/agrid_client.rb +69 -0
  7. data/lib/agrid_client/api/cities_api.rb +80 -0
  8. data/lib/agrid_client/api/companies_api.rb +265 -0
  9. data/lib/agrid_client/api/filters_api.rb +91 -0
  10. data/lib/agrid_client/api/leads_api.rb +87 -0
  11. data/lib/agrid_client/api/quotes_api.rb +162 -0
  12. data/lib/agrid_client/api/services_api.rb +83 -0
  13. data/lib/agrid_client/api_client.rb +365 -0
  14. data/lib/agrid_client/api_error.rb +36 -0
  15. data/lib/agrid_client/configuration.rb +167 -0
  16. data/lib/agrid_client/models/address.rb +121 -0
  17. data/lib/agrid_client/models/category.rb +75 -0
  18. data/lib/agrid_client/models/city_company.rb +67 -0
  19. data/lib/agrid_client/models/city_with_full_state.rb +73 -0
  20. data/lib/agrid_client/models/city_with_state_name.rb +67 -0
  21. data/lib/agrid_client/models/company.rb +108 -0
  22. data/lib/agrid_client/models/company_ranking.rb +45 -0
  23. data/lib/agrid_client/models/concerns/swagger_model.rb +106 -0
  24. data/lib/agrid_client/models/customer.rb +90 -0
  25. data/lib/agrid_client/models/customer_input.rb +73 -0
  26. data/lib/agrid_client/models/data_check.rb +45 -0
  27. data/lib/agrid_client/models/filter.rb +75 -0
  28. data/lib/agrid_client/models/filter_category.rb +73 -0
  29. data/lib/agrid_client/models/image.rb +37 -0
  30. data/lib/agrid_client/models/inline_response_200.rb +67 -0
  31. data/lib/agrid_client/models/inline_response_200_1.rb +78 -0
  32. data/lib/agrid_client/models/item.rb +90 -0
  33. data/lib/agrid_client/models/lead.rb +77 -0
  34. data/lib/agrid_client/models/lead_input.rb +90 -0
  35. data/lib/agrid_client/models/location.rb +66 -0
  36. data/lib/agrid_client/models/phone.rb +58 -0
  37. data/lib/agrid_client/models/price_range.rb +73 -0
  38. data/lib/agrid_client/models/quote.rb +124 -0
  39. data/lib/agrid_client/models/sale.rb +28 -0
  40. data/lib/agrid_client/models/service.rb +90 -0
  41. data/lib/agrid_client/models/state.rb +67 -0
  42. data/lib/agrid_client/version.rb +3 -0
  43. data/spec/api/cities_api_spec.rb +49 -0
  44. data/spec/api/companies_api_spec.rb +69 -0
  45. data/spec/api/filters_api_spec.rb +54 -0
  46. data/spec/api/leads_api_spec.rb +52 -0
  47. data/spec/api/quotes_api_spec.rb +74 -0
  48. data/spec/api/services_api_spec.rb +52 -0
  49. data/spec/models/address_spec.rb +116 -0
  50. data/spec/models/category_spec.rb +66 -0
  51. data/spec/models/city_with_full_state_spec.rb +66 -0
  52. data/spec/models/city_with_state_name_spec.rb +56 -0
  53. data/spec/models/company_spec.rb +116 -0
  54. data/spec/models/customer_input_spec.rb +66 -0
  55. data/spec/models/customer_spec.rb +86 -0
  56. data/spec/models/filter_category_spec.rb +66 -0
  57. data/spec/models/filter_spec.rb +66 -0
  58. data/spec/models/inline_response_200_1_spec.rb +66 -0
  59. data/spec/models/inline_response_200_spec.rb +46 -0
  60. data/spec/models/item_spec.rb +86 -0
  61. data/spec/models/lead_input_spec.rb +86 -0
  62. data/spec/models/lead_spec.rb +76 -0
  63. data/spec/models/location_spec.rb +56 -0
  64. data/spec/models/phone_spec.rb +46 -0
  65. data/spec/models/price_range_spec.rb +56 -0
  66. data/spec/models/quote_spec.rb +116 -0
  67. data/spec/models/service_spec.rb +76 -0
  68. data/spec/models/state_spec.rb +56 -0
  69. data/spec/spec_helper.rb +23 -0
  70. metadata +319 -0
@@ -0,0 +1,91 @@
1
+ =begin
2
+ Agrid Quotes API
3
+
4
+
5
+
6
+ OpenAPI spec version: 1.0
7
+
8
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
9
+
10
+
11
+ =end
12
+
13
+ require "uri"
14
+
15
+ module AgridClient
16
+ class FiltersApi
17
+ attr_accessor :api_client
18
+
19
+ def initialize(api_client = ApiClient.default)
20
+ @api_client = api_client
21
+ end
22
+
23
+ #
24
+ # Returns all filters of a service.\n You can some of the query parameters in /v1/services/:service_id/quotes\n to fetch all filters and filter_categories of the set of that quotes.
25
+ # @param service_id param to filter quotes by service
26
+ # @param [Hash] opts the optional parameters
27
+ # @option opts [String] :city_id the city id
28
+ # @option opts [Float] :min_price the minimium price for a quote
29
+ # @option opts [Float] :max_price the maximum price for a quote
30
+ # @return [Filter]
31
+ def services_service_id_filters_get(service_id, opts = {})
32
+ data, _status_code, _headers = services_service_id_filters_get_with_http_info(service_id, opts)
33
+ return data
34
+ end
35
+
36
+ #
37
+ # Returns all filters of a service.\n You can some of the query parameters in /v1/services/:service_id/quotes\n to fetch all filters and filter_categories of the set of that quotes.
38
+ # @param service_id param to filter quotes by service
39
+ # @param [Hash] opts the optional parameters
40
+ # @option opts [String] :city_id the city id
41
+ # @option opts [Float] :min_price the minimium price for a quote
42
+ # @option opts [Float] :max_price the maximum price for a quote
43
+ # @return [Array<(Filter, Fixnum, Hash)>] Filter data, response status code and response headers
44
+ def services_service_id_filters_get_with_http_info(service_id, opts = {})
45
+ if @api_client.config.debugging
46
+ @api_client.config.logger.debug "Calling API: FiltersApi#services_service_id_filters_get ..."
47
+ end
48
+
49
+ # verify the required parameter 'service_id' is set
50
+ fail "Missing the required parameter 'service_id' when calling services_service_id_filters_get" if service_id.nil?
51
+
52
+ # resource path
53
+ local_var_path = "/services/{service_id}/filters".sub('{format}','json').sub('{' + 'service_id' + '}', service_id.to_s)
54
+
55
+ # query parameters
56
+ query_params = {}
57
+ query_params[:'city_id'] = opts[:'city_id'] if opts[:'city_id']
58
+ query_params[:'min_price'] = opts[:'min_price'] if opts[:'min_price']
59
+ query_params[:'max_price'] = opts[:'max_price'] if opts[:'max_price']
60
+
61
+ # header parameters
62
+ header_params = {}
63
+
64
+ # HTTP header 'Accept' (if needed)
65
+ local_header_accept = ['application/json']
66
+ local_header_accept_result = @api_client.select_header_accept(local_header_accept) and header_params['Accept'] = local_header_accept_result
67
+
68
+ # HTTP header 'Content-Type'
69
+ local_header_content_type = ['application/json']
70
+ header_params['Content-Type'] = @api_client.select_header_content_type(local_header_content_type)
71
+
72
+ # form parameters
73
+ form_params = {}
74
+
75
+ # http body (model)
76
+ post_body = nil
77
+ auth_names = []
78
+ data, status_code, headers = @api_client.call_api(:GET, local_var_path,
79
+ :header_params => header_params,
80
+ :query_params => query_params,
81
+ :form_params => form_params,
82
+ :body => post_body,
83
+ :auth_names => auth_names,
84
+ :return_type => 'Filter')
85
+ if @api_client.config.debugging
86
+ @api_client.config.logger.debug "API called: FiltersApi#services_service_id_filters_get\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
87
+ end
88
+ return data, status_code, headers
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,87 @@
1
+ =begin
2
+ Agrid Quotes API
3
+
4
+
5
+
6
+ OpenAPI spec version: 1.0
7
+
8
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
9
+
10
+
11
+ =end
12
+
13
+ require "uri"
14
+
15
+ module AgridClient
16
+ class LeadsApi
17
+ attr_accessor :api_client
18
+
19
+ def initialize(api_client = ApiClient.default)
20
+ @api_client = api_client
21
+ end
22
+
23
+ #
24
+ # Create a new lead
25
+ # @param service_id Service Id
26
+ # @param lead Lead Id
27
+ # @param [Hash] opts the optional parameters
28
+ # @return [Lead]
29
+ def services_service_id_leads_post(service_id, lead, opts = {})
30
+ data, _status_code, _headers = services_service_id_leads_post_with_http_info(service_id, lead, opts)
31
+ return data
32
+ end
33
+
34
+ #
35
+ # Create a new lead
36
+ # @param service_id Service Id
37
+ # @param lead Lead Id
38
+ # @param [Hash] opts the optional parameters
39
+ # @return [Array<(Lead, Fixnum, Hash)>] Lead data, response status code and response headers
40
+ def services_service_id_leads_post_with_http_info(service_id, lead, opts = {})
41
+ if @api_client.config.debugging
42
+ @api_client.config.logger.debug "Calling API: LeadsApi#services_service_id_leads_post ..."
43
+ end
44
+
45
+ # verify the required parameter 'service_id' is set
46
+ fail "Missing the required parameter 'service_id' when calling services_service_id_leads_post" if service_id.nil?
47
+
48
+ # verify the required parameter 'lead' is set
49
+ fail "Missing the required parameter 'lead' when calling services_service_id_leads_post" if lead.nil?
50
+
51
+ # resource path
52
+ local_var_path = "/services/{service_id}/leads".sub('{format}','json').sub('{' + 'service_id' + '}', service_id.to_s)
53
+
54
+ # query parameters
55
+ query_params = {}
56
+
57
+ # header parameters
58
+ header_params = {}
59
+
60
+ # HTTP header 'Accept' (if needed)
61
+ local_header_accept = ['application/json']
62
+ local_header_accept_result = @api_client.select_header_accept(local_header_accept) and header_params['Accept'] = local_header_accept_result
63
+
64
+ # HTTP header 'Content-Type'
65
+ local_header_content_type = ['application/json']
66
+ header_params['Content-Type'] = @api_client.select_header_content_type(local_header_content_type)
67
+
68
+ # form parameters
69
+ form_params = {}
70
+
71
+ # http body (model)
72
+ post_body = @api_client.object_to_http_body(lead)
73
+ auth_names = []
74
+ data, status_code, headers = @api_client.call_api(:POST, local_var_path,
75
+ :header_params => header_params,
76
+ :query_params => query_params,
77
+ :form_params => form_params,
78
+ :body => post_body,
79
+ :auth_names => auth_names,
80
+ :return_type => 'Lead')
81
+ if @api_client.config.debugging
82
+ @api_client.config.logger.debug "API called: LeadsApi#services_service_id_leads_post\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
83
+ end
84
+ return data, status_code, headers
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,162 @@
1
+ =begin
2
+ Agrid Quotes API
3
+
4
+
5
+
6
+ OpenAPI spec version: 1.0
7
+
8
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
9
+
10
+
11
+ =end
12
+
13
+ require "uri"
14
+
15
+ module AgridClient
16
+ class QuotesApi
17
+ attr_accessor :api_client
18
+
19
+ def initialize(api_client = ApiClient.default)
20
+ @api_client = api_client
21
+ end
22
+
23
+ #
24
+ # Returns a specific quote
25
+ # @param id Param to filter company using id
26
+ # @param [Hash] opts the optional parameters
27
+ # @return [Quote]
28
+ def quotes_id_get(id, opts = {})
29
+ data, _status_code, _headers = quotes_id_get_with_http_info(id, opts)
30
+ return data
31
+ end
32
+
33
+ #
34
+ # Returns a specific quote
35
+ # @param id Param to filter company using id
36
+ # @param [Hash] opts the optional parameters
37
+ # @return [Array<(Quote, Fixnum, Hash)>] Quote data, response status code and response headers
38
+ def quotes_id_get_with_http_info(id, opts = {})
39
+ if @api_client.config.debugging
40
+ @api_client.config.logger.debug "Calling API: QuotesApi#quotes_id_get ..."
41
+ end
42
+
43
+ # verify the required parameter 'id' is set
44
+ fail "Missing the required parameter 'id' when calling quotes_id_get" if id.nil?
45
+
46
+ # resource path
47
+ local_var_path = "/quotes/{id}".sub('{format}','json').sub('{' + 'id' + '}', id.to_s)
48
+
49
+ # query parameters
50
+ query_params = {}
51
+
52
+ # header parameters
53
+ header_params = {}
54
+
55
+ # HTTP header 'Accept' (if needed)
56
+ local_header_accept = ['application/json']
57
+ local_header_accept_result = @api_client.select_header_accept(local_header_accept) and header_params['Accept'] = local_header_accept_result
58
+
59
+ # HTTP header 'Content-Type'
60
+ local_header_content_type = ['application/json']
61
+ header_params['Content-Type'] = @api_client.select_header_content_type(local_header_content_type)
62
+
63
+ # form parameters
64
+ form_params = {}
65
+
66
+ # http body (model)
67
+ post_body = nil
68
+ auth_names = []
69
+ data, status_code, headers = @api_client.call_api(:GET, local_var_path,
70
+ :header_params => header_params,
71
+ :query_params => query_params,
72
+ :form_params => form_params,
73
+ :body => post_body,
74
+ :auth_names => auth_names,
75
+ :return_type => 'Quote')
76
+ if @api_client.config.debugging
77
+ @api_client.config.logger.debug "API called: QuotesApi#quotes_id_get\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
78
+ end
79
+ return data, status_code, headers
80
+ end
81
+
82
+ #
83
+ # Returns a set of quotes.
84
+ # @param service_id Param to filter quotes by service
85
+ # @param [Hash] opts the optional parameters
86
+ # @option opts [String] :company_id Param to filter quotes by company
87
+ # @option opts [String] :city_id Param to filter quotes by city
88
+ # @option opts [Integer] :limit Limit number of quotes on response(default is 12)
89
+ # @option opts [Integer] :offset Number to skip rows before beginning to return quotes
90
+ # @option opts [Array<String>] :filter_categories Example: &lt;br&gt; filter_categories[:filter_id][]&#x3D;:filter_category_id&amp;filter_categories[:filter_id][]&#x3D;:filter_category_id &lt;br&gt;&lt;br&gt; :filter_id is the id of filter&lt;br&gt; :filter_category_id is the id of filter category
91
+ # @option opts [Float] :min_price Param to filter quotes with price is at least equal to :min_price
92
+ # @option opts [Float] :max_price Param to filter quotes with price is at most equal to :max_price
93
+ # @return [InlineResponse2001]
94
+ def quotes_get(service_id, opts = {})
95
+ data, _status_code, _headers = quotes_get_with_http_info(service_id, opts)
96
+ return data
97
+ end
98
+
99
+ #
100
+ # Returns a set of quotes.
101
+ # @param service_id Param to filter quotes by service
102
+ # @param [Hash] opts the optional parameters
103
+ # @option opts [String] :company_id Param to filter quotes by company
104
+ # @option opts [String] :city_id Param to filter quotes by city
105
+ # @option opts [Integer] :limit Limit number of quotes on response(default is 12)
106
+ # @option opts [Integer] :offset Number to skip rows before beginning to return quotes
107
+ # @option opts [Array<String>] :filter_categories Example: &lt;br&gt; filter_categories[:filter_id][]&#x3D;:filter_category_id&amp;filter_categories[:filter_id][]&#x3D;:filter_category_id &lt;br&gt;&lt;br&gt; :filter_id is the id of filter&lt;br&gt; :filter_category_id is the id of filter category
108
+ # @option opts [Float] :min_price Param to filter quotes with price is at least equal to :min_price
109
+ # @option opts [Float] :max_price Param to filter quotes with price is at most equal to :max_price
110
+ # @return [Array<(InlineResponse2001, Fixnum, Hash)>] InlineResponse2001 data, response status code and response headers
111
+ def quotes_get_with_http_info(service_id, opts = {})
112
+ if @api_client.config.debugging
113
+ @api_client.config.logger.debug "Calling API: QuotesApi#quotes_get ..."
114
+ end
115
+
116
+ # verify the required parameter 'service_id' is set
117
+ fail "Missing the required parameter 'service_id' when calling quotes_get" if service_id.nil?
118
+
119
+ # resource path
120
+ local_var_path = "/services/{service_id}/quotes".sub('{format}','json').sub('{' + 'service_id' + '}', service_id.to_s)
121
+
122
+ # query parameters
123
+ query_params = {}
124
+ query_params[:'company_id'] = opts[:'company_id'] if opts[:'company_id']
125
+ query_params[:'city_id'] = opts[:'city_id'] if opts[:'city_id']
126
+ query_params[:'limit'] = opts[:'limit'] if opts[:'limit']
127
+ query_params[:'offset'] = opts[:'offset'] if opts[:'offset']
128
+ query_params[:'filter_categories'] = @api_client.build_collection_param(opts[:'filter_categories'], :csv) if opts[:'filter_categories']
129
+ query_params[:'min_price'] = opts[:'min_price'] if opts[:'min_price']
130
+ query_params[:'max_price'] = opts[:'max_price'] if opts[:'max_price']
131
+
132
+ # header parameters
133
+ header_params = {}
134
+
135
+ # HTTP header 'Accept' (if needed)
136
+ local_header_accept = ['application/json']
137
+ local_header_accept_result = @api_client.select_header_accept(local_header_accept) and header_params['Accept'] = local_header_accept_result
138
+
139
+ # HTTP header 'Content-Type'
140
+ local_header_content_type = ['application/json']
141
+ header_params['Content-Type'] = @api_client.select_header_content_type(local_header_content_type)
142
+
143
+ # form parameters
144
+ form_params = {}
145
+
146
+ # http body (model)
147
+ post_body = nil
148
+ auth_names = []
149
+ data, status_code, headers = @api_client.call_api(:GET, local_var_path,
150
+ :header_params => header_params,
151
+ :query_params => query_params,
152
+ :form_params => form_params,
153
+ :body => post_body,
154
+ :auth_names => auth_names,
155
+ :return_type => 'InlineResponse2001')
156
+ if @api_client.config.debugging
157
+ @api_client.config.logger.debug "API called: QuotesApi#quotes_get\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
158
+ end
159
+ return data, status_code, headers
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,83 @@
1
+ =begin
2
+ Agrid Quotes API
3
+
4
+
5
+
6
+ OpenAPI spec version: 1.0
7
+
8
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
9
+
10
+
11
+ =end
12
+
13
+ require "uri"
14
+
15
+ module AgridClient
16
+ class ServicesApi
17
+ attr_accessor :api_client
18
+
19
+ def initialize(api_client = ApiClient.default)
20
+ @api_client = api_client
21
+ end
22
+
23
+ #
24
+ # Returns all available services
25
+ # @param [Hash] opts the optional parameters
26
+ # @option opts [String] :city_id City ID to filter services by city
27
+ # @option opts [String] :tag Tag to filter a subset of services
28
+ # @return [Array<Service>]
29
+ def services_get(opts = {})
30
+ data, _status_code, _headers = services_get_with_http_info(opts)
31
+ return data
32
+ end
33
+
34
+ #
35
+ # Returns all available services
36
+ # @param [Hash] opts the optional parameters
37
+ # @option opts [String] :city_id City ID to filter services by city
38
+ # @option opts [String] :tag Tag to filter a subset of services
39
+ # @return [Array<(Array<Service>, Fixnum, Hash)>] Array<Service> data, response status code and response headers
40
+ def services_get_with_http_info(opts = {})
41
+ if @api_client.config.debugging
42
+ @api_client.config.logger.debug "Calling API: ServicesApi#services_get ..."
43
+ end
44
+
45
+ # resource path
46
+ local_var_path = "/services".sub('{format}','json')
47
+
48
+ # query parameters
49
+ query_params = {}
50
+ query_params[:'city_id'] = opts[:'city_id'] if opts[:'city_id']
51
+ query_params[:'tag'] = opts[:'tag'] if opts[:'tag']
52
+
53
+ # header parameters
54
+ header_params = {}
55
+
56
+ # HTTP header 'Accept' (if needed)
57
+ local_header_accept = ['application/json']
58
+ local_header_accept_result = @api_client.select_header_accept(local_header_accept) and header_params['Accept'] = local_header_accept_result
59
+
60
+ # HTTP header 'Content-Type'
61
+ local_header_content_type = ['application/json']
62
+ header_params['Content-Type'] = @api_client.select_header_content_type(local_header_content_type)
63
+
64
+ # form parameters
65
+ form_params = {}
66
+
67
+ # http body (model)
68
+ post_body = nil
69
+ auth_names = []
70
+ data, status_code, headers = @api_client.call_api(:GET, local_var_path,
71
+ :header_params => header_params,
72
+ :query_params => query_params,
73
+ :form_params => form_params,
74
+ :body => post_body,
75
+ :auth_names => auth_names,
76
+ :return_type => 'Array<Service>')
77
+ if @api_client.config.debugging
78
+ @api_client.config.logger.debug "API called: ServicesApi#services_get\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
79
+ end
80
+ return data, status_code, headers
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,365 @@
1
+ =begin
2
+ Agrid Quotes API
3
+
4
+
5
+
6
+ OpenAPI spec version: 1.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 AgridClient
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
+ # Initializes the ApiClient
31
+ # @option config [Configuration] Configuraiton for initializing the object, default to Configuration.default
32
+ def initialize(config = Configuration.default)
33
+ @config = config
34
+ @user_agent = "Swagger-Codegen/#{VERSION}/ruby"
35
+ @default_headers = {
36
+ 'Content-Type' => "application/json",
37
+ 'User-Agent' => @user_agent
38
+ }
39
+ if config.app_credentials
40
+ @default_headers['AGRID-APP-ID'] = config.app_credentials[:id]
41
+ @default_headers['AGRID-APP-KEY'] = config.app_credentials[:key]
42
+ end
43
+ end
44
+
45
+ def self.default
46
+ @@default ||= ApiClient.new
47
+ end
48
+
49
+ # Call an API with given options.
50
+ #
51
+ # @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
52
+ # the data deserialized from response body (could be nil), response status code and response headers.
53
+ def call_api(http_method, path, opts = {})
54
+ request = build_request(http_method, path, opts)
55
+ response = request.run
56
+
57
+ if @config.debugging
58
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
59
+ end
60
+
61
+ unless response.success?
62
+ fail ApiError.new(:code => response.code,
63
+ :response_headers => response.headers,
64
+ :response_body => response.body),
65
+ response.status_message
66
+ end
67
+
68
+ if opts[:return_type]
69
+ data = deserialize(response, opts[:return_type])
70
+ else
71
+ data = nil
72
+ end
73
+ return data, response.code, response.headers
74
+ end
75
+
76
+ # Builds the HTTP request
77
+ #
78
+ # @param [String] http_method HTTP method/verb (e.g. POST)
79
+ # @param [String] path URL path (e.g. /account/new)
80
+ # @option opts [Hash] :header_params Header parameters
81
+ # @option opts [Hash] :query_params Query parameters
82
+ # @option opts [Hash] :form_params Query parameters
83
+ # @option opts [Object] :body HTTP body (JSON/XML)
84
+ # @return [Typhoeus::Request] A Typhoeus Request
85
+ def build_request(http_method, path, opts = {})
86
+ url = build_request_url(path)
87
+ http_method = http_method.to_sym.downcase
88
+
89
+ header_params = @default_headers.merge(opts[:header_params] || {})
90
+ query_params = opts[:query_params] || {}
91
+ form_params = opts[:form_params] || {}
92
+
93
+
94
+ req_opts = {
95
+ :method => http_method,
96
+ :headers => header_params,
97
+ :params => query_params,
98
+ :timeout => @config.timeout,
99
+ :ssl_verifypeer => @config.verify_ssl,
100
+ :sslcert => @config.cert_file,
101
+ :sslkey => @config.key_file,
102
+ :verbose => @config.debugging
103
+ }
104
+
105
+ req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
106
+
107
+ if [:post, :patch, :put, :delete].include?(http_method)
108
+ req_body = build_request_body(header_params, form_params, opts[:body])
109
+ req_opts.update :body => req_body
110
+ if @config.debugging
111
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
112
+ end
113
+ end
114
+
115
+ Typhoeus::Request.new(url, req_opts)
116
+ end
117
+
118
+ # Check if the given MIME is a JSON MIME.
119
+ # JSON MIME examples:
120
+ # application/json
121
+ # application/json; charset=UTF8
122
+ # APPLICATION/JSON
123
+ # @param [String] mime MIME
124
+ # @return [Boolean] True if the MIME is applicaton/json
125
+ def json_mime?(mime)
126
+ !(mime =~ /\Aapplication\/json(;.*)?\z/i).nil?
127
+ end
128
+
129
+ # Deserialize the response to the given return type.
130
+ #
131
+ # @param [Response] response HTTP response
132
+ # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
133
+ def deserialize(response, return_type)
134
+ body = response.body
135
+ return nil if body.nil? || body.empty?
136
+
137
+ # return response body directly for String return type
138
+ return body if return_type == 'String'
139
+
140
+ # handle file downloading - save response body into a tmp file and return the File instance
141
+ return download_file(response) if return_type == 'File'
142
+
143
+ # ensuring a default content type
144
+ content_type = response.headers['Content-Type'] || 'application/json'
145
+
146
+ fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
147
+
148
+ begin
149
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
150
+ rescue JSON::ParserError => e
151
+ if %w(String Date DateTime).include?(return_type)
152
+ data = body
153
+ else
154
+ raise e
155
+ end
156
+ end
157
+
158
+ convert_to_type data, return_type
159
+ end
160
+
161
+ # Convert data to the given return type.
162
+ # @param [Object] data Data to be converted
163
+ # @param [String] return_type Return type
164
+ # @return [Mixed] Data in a particular type
165
+ def convert_to_type(data, return_type)
166
+ return nil if data.nil?
167
+ case return_type
168
+ when 'String'
169
+ data.to_s
170
+ when 'Integer'
171
+ data.to_i
172
+ when 'Float'
173
+ data.to_f
174
+ when 'BOOLEAN'
175
+ data == true
176
+ when 'DateTime'
177
+ # parse date time (expecting ISO 8601 format)
178
+ DateTime.parse data
179
+ when 'Date'
180
+ # parse date time (expecting ISO 8601 format)
181
+ Date.parse data
182
+ when 'Object'
183
+ # generic object (usually a Hash), return directly
184
+ data
185
+ when /\AArray<(.+)>\z/
186
+ # e.g. Array<Pet>
187
+ sub_type = $1
188
+ data.map {|item| convert_to_type(item, sub_type) }
189
+ when /\AHash\<String, (.+)\>\z/
190
+ # e.g. Hash<String, Integer>
191
+ sub_type = $1
192
+ {}.tap do |hash|
193
+ data.each {|k, v| hash[k] = convert_to_type(v, sub_type) }
194
+ end
195
+ else
196
+ # models, e.g. Pet
197
+ AgridClient.const_get(return_type).new.tap do |model|
198
+ model.build_from_hash data
199
+ end
200
+ end
201
+ end
202
+
203
+ # Save response body into a file in (the defined) temporary folder, using the filename
204
+ # from the "Content-Disposition" header if provided, otherwise a random filename.
205
+ #
206
+ # @see Configuration#temp_folder_path
207
+ # @return [Tempfile] the file downloaded
208
+ def download_file(response)
209
+ content_disposition = response.headers['Content-Disposition']
210
+ if content_disposition
211
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
212
+ prefix = sanitize_filename(filename)
213
+ else
214
+ prefix = 'download-'
215
+ end
216
+ prefix = prefix + '-' unless prefix.end_with?('-')
217
+
218
+ tempfile = nil
219
+ encoding = response.body.encoding
220
+ Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) do |file|
221
+ file.write(response.body)
222
+ tempfile = file
223
+ end
224
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
225
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
226
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
227
+ "explicitly with `tempfile.delete`"
228
+ tempfile
229
+ end
230
+
231
+ # Sanitize filename by removing path.
232
+ # e.g. ../../sun.gif becomes sun.gif
233
+ #
234
+ # @param [String] filename the filename to be sanitized
235
+ # @return [String] the sanitized filename
236
+ def sanitize_filename(filename)
237
+ filename.gsub(/.*[\/\\]/, '')
238
+ end
239
+
240
+ def build_request_url(path)
241
+ # Add leading and trailing slashes to path
242
+ path = "/#{path}".gsub(/\/+/, '/')
243
+ URI.encode(@config.base_url + path)
244
+ end
245
+
246
+ # Builds the HTTP request body
247
+ #
248
+ # @param [Hash] header_params Header parameters
249
+ # @param [Hash] form_params Query parameters
250
+ # @param [Object] body HTTP body (JSON/XML)
251
+ # @return [String] HTTP body data in the form of string
252
+ def build_request_body(header_params, form_params, body)
253
+ # http form
254
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
255
+ header_params['Content-Type'] == 'multipart/form-data'
256
+ data = {}
257
+ form_params.each do |key, value|
258
+ case value
259
+ when File, Array, nil
260
+ # let typhoeus handle File, Array and nil parameters
261
+ data[key] = value
262
+ else
263
+ data[key] = value.to_s
264
+ end
265
+ end
266
+ elsif body
267
+ data = body.is_a?(String) ? body : body.to_json
268
+ else
269
+ data = nil
270
+ end
271
+ data
272
+ end
273
+
274
+ # Update hearder and query params based on authentication settings.
275
+ #
276
+ # @param [Hash] header_params Header parameters
277
+ # @param [Hash] form_params Query parameters
278
+ # @param [String] auth_names Authentication scheme name
279
+ def update_params_for_auth!(header_params, query_params, auth_names)
280
+ Array(auth_names).each do |auth_name|
281
+ auth_setting = @config.auth_settings[auth_name]
282
+ next unless auth_setting
283
+ case auth_setting[:in]
284
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
285
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
286
+ else fail ArgumentError, 'Authentication token must be in `query` of `header`'
287
+ end
288
+ end
289
+ end
290
+
291
+ # Sets user agent in HTTP header
292
+ #
293
+ # @param [String] user_agent User agent (e.g. swagger-codegen/ruby/1.0.0)
294
+ def user_agent=(user_agent)
295
+ @user_agent = user_agent
296
+ @default_headers['User-Agent'] = @user_agent
297
+ end
298
+
299
+ # Return Accept header based on an array of accepts provided.
300
+ # @param [Array] accepts array for Accept
301
+ # @return [String] the Accept header (e.g. application/json)
302
+ def select_header_accept(accepts)
303
+ return nil if accepts.nil? || accepts.empty?
304
+ # use JSON when present, otherwise use all of the provided
305
+ json_accept = accepts.find { |s| json_mime?(s) }
306
+ return json_accept || accepts.join(',')
307
+ end
308
+
309
+ # Return Content-Type header based on an array of content types provided.
310
+ # @param [Array] content_types array for Content-Type
311
+ # @return [String] the Content-Type header (e.g. application/json)
312
+ def select_header_content_type(content_types)
313
+ # use application/json by default
314
+ return 'application/json' if content_types.nil? || content_types.empty?
315
+ # use JSON when present, otherwise use the first one
316
+ json_content_type = content_types.find { |s| json_mime?(s) }
317
+ return json_content_type || content_types.first
318
+ end
319
+
320
+ # Convert object (array, hash, object, etc) to JSON string.
321
+ # @param [Object] model object to be converted into JSON string
322
+ # @return [String] JSON string representation of the object
323
+ def object_to_http_body(model)
324
+ return model if model.nil? || model.is_a?(String)
325
+ local_body = nil
326
+ if model.is_a?(Array)
327
+ local_body = model.map{|m| object_to_hash(m) }
328
+ else
329
+ local_body = object_to_hash(model)
330
+ end
331
+ local_body.to_json
332
+ end
333
+
334
+ # Convert object(non-array) to hash.
335
+ # @param [Object] obj object to be converted into JSON string
336
+ # @return [String] JSON string representation of the object
337
+ def object_to_hash(obj)
338
+ if obj.respond_to?(:to_hash)
339
+ obj.to_hash
340
+ else
341
+ obj
342
+ end
343
+ end
344
+
345
+ # Build parameter value according to the given collection format.
346
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
347
+ def build_collection_param(param, collection_format)
348
+ case collection_format
349
+ when :csv
350
+ param.join(',')
351
+ when :ssv
352
+ param.join(' ')
353
+ when :tsv
354
+ param.join("\t")
355
+ when :pipes
356
+ param.join('|')
357
+ when :multi
358
+ # return the array directly as typhoeus will handle it as expected
359
+ param
360
+ else
361
+ fail "unknown collection format: #{collection_format.inspect}"
362
+ end
363
+ end
364
+ end
365
+ end