simplyrets 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +6 -3
  3. data/Makefile +1 -2
  4. data/README.org +2 -0
  5. data/lib/simplyrets.rb +12 -1
  6. data/lib/simplyrets/api/default_api.rb +132 -128
  7. data/lib/simplyrets/api_client.rb +48 -9
  8. data/lib/simplyrets/api_client.rb.orig +373 -0
  9. data/lib/simplyrets/api_error.rb +12 -1
  10. data/lib/simplyrets/configuration.rb +23 -0
  11. data/lib/simplyrets/models/agent.rb +61 -27
  12. data/lib/simplyrets/models/broker.rb +57 -20
  13. data/lib/simplyrets/models/contact_information.rb +62 -27
  14. data/lib/simplyrets/models/error.rb +59 -23
  15. data/lib/simplyrets/models/geographic_data.rb +61 -28
  16. data/lib/simplyrets/models/listing.rb +94 -70
  17. data/lib/simplyrets/models/mls_information.rb +95 -37
  18. data/lib/simplyrets/models/office.rb +62 -28
  19. data/lib/simplyrets/models/open_house.rb +66 -36
  20. data/lib/simplyrets/models/parking.rb +59 -24
  21. data/lib/simplyrets/models/property.rb +128 -136
  22. data/lib/simplyrets/models/sales.rb +61 -28
  23. data/lib/simplyrets/models/school.rb +60 -34
  24. data/lib/simplyrets/models/street_address.rb +66 -55
  25. data/lib/simplyrets/models/tax.rb +70 -35
  26. data/lib/simplyrets/version.rb +13 -2
  27. data/ruby-client.meta +1 -0
  28. data/ruby-client.zip +0 -0
  29. data/ruby-client/.gitignore +50 -0
  30. data/ruby-client/.rspec +2 -0
  31. data/ruby-client/.swagger-codegen-ignore +23 -0
  32. data/ruby-client/LICENSE +201 -0
  33. data/ruby-client/README.md +128 -0
  34. data/ruby-client/docs/Agent.md +11 -0
  35. data/ruby-client/docs/Broker.md +8 -0
  36. data/ruby-client/docs/ContactInformation.md +10 -0
  37. data/ruby-client/docs/DefaultApi.md +306 -0
  38. data/ruby-client/docs/Error.md +9 -0
  39. data/ruby-client/docs/GeographicData.md +12 -0
  40. data/ruby-client/docs/Listing.md +30 -0
  41. data/ruby-client/docs/MlsInformation.md +11 -0
  42. data/ruby-client/docs/Office.md +11 -0
  43. data/ruby-client/docs/OpenHouse.md +15 -0
  44. data/ruby-client/docs/Parking.md +10 -0
  45. data/ruby-client/docs/Property.md +43 -0
  46. data/ruby-client/docs/Sales.md +12 -0
  47. data/ruby-client/docs/School.md +11 -0
  48. data/ruby-client/docs/StreetAddress.md +16 -0
  49. data/ruby-client/docs/Tax.md +10 -0
  50. data/ruby-client/git_push.sh +67 -0
  51. data/ruby-client/lib/swagger_client.rb +66 -0
  52. data/ruby-client/lib/swagger_client/api/default_api.rb +383 -0
  53. data/ruby-client/lib/swagger_client/api_client.rb +373 -0
  54. data/ruby-client/lib/swagger_client/api_error.rb +47 -0
  55. data/ruby-client/lib/swagger_client/configuration.rb +193 -0
  56. data/ruby-client/lib/swagger_client/models/agent.rb +230 -0
  57. data/ruby-client/lib/swagger_client/models/broker.rb +200 -0
  58. data/ruby-client/lib/swagger_client/models/contact_information.rb +220 -0
  59. data/ruby-client/lib/swagger_client/models/error.rb +210 -0
  60. data/ruby-client/lib/swagger_client/models/geographic_data.rb +240 -0
  61. data/ruby-client/lib/swagger_client/models/listing.rb +422 -0
  62. data/ruby-client/lib/swagger_client/models/mls_information.rb +263 -0
  63. data/ruby-client/lib/swagger_client/models/office.rb +230 -0
  64. data/ruby-client/lib/swagger_client/models/open_house.rb +270 -0
  65. data/ruby-client/lib/swagger_client/models/parking.rb +219 -0
  66. data/ruby-client/lib/swagger_client/models/property.rb +574 -0
  67. data/ruby-client/lib/swagger_client/models/sales.rb +240 -0
  68. data/ruby-client/lib/swagger_client/models/school.rb +230 -0
  69. data/ruby-client/lib/swagger_client/models/street_address.rb +280 -0
  70. data/ruby-client/lib/swagger_client/models/tax.rb +220 -0
  71. data/ruby-client/lib/swagger_client/version.rb +26 -0
  72. data/ruby-client/spec/api/default_api_spec.rb +132 -0
  73. data/ruby-client/spec/api_client_spec.rb +296 -0
  74. data/ruby-client/spec/configuration_spec.rb +48 -0
  75. data/ruby-client/spec/models/agent_spec.rb +71 -0
  76. data/ruby-client/spec/models/broker_spec.rb +53 -0
  77. data/ruby-client/spec/models/contact_information_spec.rb +65 -0
  78. data/ruby-client/spec/models/error_spec.rb +59 -0
  79. data/ruby-client/spec/models/geographic_data_spec.rb +77 -0
  80. data/ruby-client/spec/models/listing_spec.rb +185 -0
  81. data/ruby-client/spec/models/mls_information_spec.rb +74 -0
  82. data/ruby-client/spec/models/office_spec.rb +71 -0
  83. data/ruby-client/spec/models/open_house_spec.rb +95 -0
  84. data/ruby-client/spec/models/parking_spec.rb +65 -0
  85. data/ruby-client/spec/models/property_spec.rb +266 -0
  86. data/ruby-client/spec/models/sales_spec.rb +77 -0
  87. data/ruby-client/spec/models/school_spec.rb +71 -0
  88. data/ruby-client/spec/models/street_address_spec.rb +101 -0
  89. data/ruby-client/spec/models/tax_spec.rb +65 -0
  90. data/ruby-client/spec/spec_helper.rb +122 -0
  91. data/ruby-client/swagger_client.gemspec +55 -0
  92. data/simplyrets.gemspec +1 -1
  93. metadata +71 -5
@@ -1,12 +1,23 @@
1
1
  =begin
2
2
  SimplyRETS API
3
3
 
4
- The SimplyRETS API is an exciting step towards making it easier for\ndevelopers and real estate agents to build something awesome with\nreal estate data!\n\nThe documentation below makes live requests to our API using the\ntrial data. To get set up with the API using live MLS data, you\nmust have RETS credentials from your MLS, which you can then use to\ncreate an app with SimplyRETS. For more information on that\nprocess, please see our [FAQ](https://simplyrets.com/faq), [Getting\nStarted](https://simplyrets.com/blog/getting-set-up.html) page, or\n[contact us](https://simplyrets.com/\\#home-contact).\n\nBelow you'll find the API endpoints, query parameters, response bodies,\nand other information about using the SimplyRETS API. You can run\nqueries by clicking the 'Try it Out' button at the bottom of each\nsection.\n\n### Authentication\nThe SimplyRETS API uses Basic Authentication. When you create an\napp, you'll get a set of API credentials to access your\nlistings. If you're trying out the test data, you can use\n`simplyrets:simplyrets` for connecting to the API.\n\n### Media Types\nThe SimplyRETS API uses the `Accept` header to allow clients to\ncontrol media types (content versions). We maintain backwards\ncompatibility with API clients by allowing them to specify a\ncontent version. We highly recommend setting and explicity media\ntype when your application reaches production. Both the structure\nand content of our API response bodies is subject to change so we\ncan add new features while respecting the stability of applications\nwhich have already been developed.\n\nTo always use the latest SimplyRETS content version, simply use\n`application/json` in your application `Accept` header.\n\nIf you want to pin your clients media type to a specific version,\nyou can use the vendor-specific SimplyRETS media type, e.g.\n`application/vnd.simplyrets-v0.1+json\"`\n\nTo view all valid content-types for making an `OPTIONS`, make a\nrequest to the SimplyRETS api root\n\n`curl -XOPTIONS -u simplyrets:simplyrets https://api.simplyrets.com/`\n\nThe default media types used in our API responses may change in the\nfuture. If you're building an application and care about the\nstability of the API, be sure to request a specific media type in the\nAccept header as shown in the examples below.\n\nThe wordpress plugin automatically sets the `Accept` header for the\ncompatible SimplyRETS media types.\n\n### Pagination\nThere a few pieces of useful information about each request stored\nin the HTTP Headers:\n\n- `X-Total-Count` shows you the total amount of listings that match\n your current query.\n- `Link` contains pre-built pagination links for accessing the next\n'page' of listings that match your query. Read more about that\n[here](https://simplyrets.com/blog/api-pagination.html).\n
4
+ The SimplyRETS API is an exciting step towards making it easier for developers and real estate agents to build something awesome with real estate data! The documentation below makes live requests to our API using the trial data. To get set up with the API using live MLS data, you must have RETS credentials from your MLS, which you can then use to create an app with SimplyRETS. For more information on that process, please see our [FAQ](https://simplyrets.com/faq), [Getting Started](https://simplyrets.com/blog/getting-set-up.html) page, or [contact us](https://simplyrets.com/\\#home-contact). Below you'll find the API endpoints, query parameters, response bodies, and other information about using the SimplyRETS API. You can run queries by clicking the 'Try it Out' button at the bottom of each section. ### Authentication The SimplyRETS API uses Basic Authentication. When you create an app, you'll get a set of API credentials to access your listings. If you're trying out the test data, you can use `simplyrets:simplyrets` for connecting to the API. ### Media Types The SimplyRETS API uses the `Accept` header to allow clients to control media types (content versions). We maintain backwards compatibility with API clients by allowing them to specify a content version. We highly recommend setting and explicity media type when your application reaches production. Both the structure and content of our API response bodies is subject to change so we can add new features while respecting the stability of applications which have already been developed. To always use the latest SimplyRETS content version, simply use `application/json` in your application `Accept` header. If you want to pin your clients media type to a specific version, you can use the vendor-specific SimplyRETS media type, e.g. `application/vnd.simplyrets-v0.1+json\"` To view all valid content-types for making an `OPTIONS`, make a request to the SimplyRETS api root `curl -XOPTIONS -u simplyrets:simplyrets https://api.simplyrets.com/` The default media types used in our API responses may change in the future. If you're building an application and care about the stability of the API, be sure to request a specific media type in the Accept header as shown in the examples below. The wordpress plugin automatically sets the `Accept` header for the compatible SimplyRETS media types. ### Pagination There a few pieces of useful information about each request stored in the HTTP Headers: - `X-Total-Count` shows you the total amount of listings that match your current query. - `Link` contains pre-built pagination links for accessing the next 'page' of listings that match your query. Read more about that [here](https://simplyrets.com/blog/api-pagination.html).
5
5
 
6
6
  OpenAPI spec version: 1.0.0
7
7
 
8
8
  Generated by: https://github.com/swagger-api/swagger-codegen.git
9
9
 
10
+ Licensed under the Apache License, Version 2.0 (the "License");
11
+ you may not use this file except in compliance with the License.
12
+ You may obtain a copy of the License at
13
+
14
+ http://www.apache.org/licenses/LICENSE-2.0
15
+
16
+ Unless required by applicable law or agreed to in writing, software
17
+ distributed under the License is distributed on an "AS IS" BASIS,
18
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ See the License for the specific language governing permissions and
20
+ limitations under the License.
10
21
 
11
22
  =end
12
23
 
@@ -27,6 +38,8 @@ module SimplyRetsClient
27
38
  # @return [Hash]
28
39
  attr_accessor :default_headers
29
40
 
41
+ # Initializes the ApiClient
42
+ # @option config [Configuration] Configuraiton for initializing the object, default to Configuration.default
30
43
  def initialize(config = Configuration.default)
31
44
  @config = config
32
45
  @user_agent = "Swagger-Codegen/#{VERSION}/ruby"
@@ -67,6 +80,15 @@ module SimplyRetsClient
67
80
  return data, response.code, response.headers
68
81
  end
69
82
 
83
+ # Builds the HTTP request
84
+ #
85
+ # @param [String] http_method HTTP method/verb (e.g. POST)
86
+ # @param [String] path URL path (e.g. /account/new)
87
+ # @option opts [Hash] :header_params Header parameters
88
+ # @option opts [Hash] :query_params Query parameters
89
+ # @option opts [Hash] :form_params Query parameters
90
+ # @option opts [Object] :body HTTP body (JSON/XML)
91
+ # @return [Typhoeus::Request] A Typhoeus Request
70
92
  def build_request(http_method, path, opts = {})
71
93
  url = build_request_url(path)
72
94
  http_method = http_method.to_sym.downcase
@@ -75,10 +97,8 @@ module SimplyRetsClient
75
97
  query_params = opts[:query_params] || {}
76
98
  form_params = opts[:form_params] || {}
77
99
 
78
-
79
100
  update_params_for_auth! header_params, query_params, opts[:auth_names]
80
101
 
81
-
82
102
  req_opts = {
83
103
  :method => http_method,
84
104
  :headers => header_params,
@@ -108,12 +128,15 @@ module SimplyRetsClient
108
128
  # application/json
109
129
  # application/json; charset=UTF8
110
130
  # APPLICATION/JSON
131
+ # @param [String] mime MIME
132
+ # @return [Boolean] True if the MIME is applicaton/json
111
133
  def json_mime?(mime)
112
- !!(mime =~ /\Aapplication\/json(;.*)?\z/i)
134
+ !(mime =~ /\Aapplication\/json(;.*)?\z/i).nil?
113
135
  end
114
136
 
115
137
  # Deserialize the response to the given return type.
116
138
  #
139
+ # @param [Response] response HTTP response
117
140
  # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
118
141
  def deserialize(response, return_type)
119
142
  body = response.body
@@ -144,6 +167,9 @@ module SimplyRetsClient
144
167
  end
145
168
 
146
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
147
173
  def convert_to_type(data, return_type)
148
174
  return nil if data.nil?
149
175
  case return_type
@@ -216,7 +242,7 @@ module SimplyRetsClient
216
242
  # @param [String] filename the filename to be sanitized
217
243
  # @return [String] the sanitized filename
218
244
  def sanitize_filename(filename)
219
- filename.gsub /.*[\/\\]/, ''
245
+ filename.gsub(/.*[\/\\]/, '')
220
246
  end
221
247
 
222
248
  def build_request_url(path)
@@ -225,6 +251,12 @@ module SimplyRetsClient
225
251
  URI.encode(@config.base_url + path)
226
252
  end
227
253
 
254
+ # Builds the HTTP request body
255
+ #
256
+ # @param [Hash] header_params Header parameters
257
+ # @param [Hash] form_params Query parameters
258
+ # @param [Object] body HTTP body (JSON/XML)
259
+ # @return [String] HTTP body data in the form of string
228
260
  def build_request_body(header_params, form_params, body)
229
261
  # http form
230
262
  if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
@@ -248,6 +280,10 @@ module SimplyRetsClient
248
280
  end
249
281
 
250
282
  # Update hearder and query params based on authentication settings.
283
+ #
284
+ # @param [Hash] header_params Header parameters
285
+ # @param [Hash] form_params Query parameters
286
+ # @param [String] auth_names Authentication scheme name
251
287
  def update_params_for_auth!(header_params, query_params, auth_names)
252
288
  Array(auth_names).each do |auth_name|
253
289
  auth_setting = @config.auth_settings[auth_name]
@@ -260,6 +296,9 @@ module SimplyRetsClient
260
296
  end
261
297
  end
262
298
 
299
+ # Sets user agent in HTTP header
300
+ #
301
+ # @param [String] user_agent User agent (e.g. swagger-codegen/ruby/1.0.0)
263
302
  def user_agent=(user_agent)
264
303
  @user_agent = user_agent
265
304
  @default_headers['User-Agent'] = @user_agent
@@ -291,13 +330,13 @@ module SimplyRetsClient
291
330
  # @return [String] JSON string representation of the object
292
331
  def object_to_http_body(model)
293
332
  return model if model.nil? || model.is_a?(String)
294
- _body = nil
333
+ local_body = nil
295
334
  if model.is_a?(Array)
296
- _body = model.map{|m| object_to_hash(m) }
335
+ local_body = model.map{|m| object_to_hash(m) }
297
336
  else
298
- _body = object_to_hash(model)
337
+ local_body = object_to_hash(model)
299
338
  end
300
- _body.to_json
339
+ local_body.to_json
301
340
  end
302
341
 
303
342
  # Convert object(non-array) to hash.
@@ -0,0 +1,373 @@
1
+ =begin
2
+ SimplyRETS API
3
+
4
+ The SimplyRETS API is an exciting step towards making it easier for developers and real estate agents to build something awesome with real estate data! The documentation below makes live requests to our API using the trial data. To get set up with the API using live MLS data, you must have RETS credentials from your MLS, which you can then use to create an app with SimplyRETS. For more information on that process, please see our [FAQ](https://simplyrets.com/faq), [Getting Started](https://simplyrets.com/blog/getting-set-up.html) page, or [contact us](https://simplyrets.com/\\#home-contact). Below you'll find the API endpoints, query parameters, response bodies, and other information about using the SimplyRETS API. You can run queries by clicking the 'Try it Out' button at the bottom of each section. ### Authentication The SimplyRETS API uses Basic Authentication. When you create an app, you'll get a set of API credentials to access your listings. If you're trying out the test data, you can use `simplyrets:simplyrets` for connecting to the API. ### Media Types The SimplyRETS API uses the `Accept` header to allow clients to control media types (content versions). We maintain backwards compatibility with API clients by allowing them to specify a content version. We highly recommend setting and explicity media type when your application reaches production. Both the structure and content of our API response bodies is subject to change so we can add new features while respecting the stability of applications which have already been developed. To always use the latest SimplyRETS content version, simply use `application/json` in your application `Accept` header. If you want to pin your clients media type to a specific version, you can use the vendor-specific SimplyRETS media type, e.g. `application/vnd.simplyrets-v0.1+json\"` To view all valid content-types for making an `OPTIONS`, make a request to the SimplyRETS api root `curl -XOPTIONS -u simplyrets:simplyrets https://api.simplyrets.com/` The default media types used in our API responses may change in the future. If you're building an application and care about the stability of the API, be sure to request a specific media type in the Accept header as shown in the examples below. The wordpress plugin automatically sets the `Accept` header for the compatible SimplyRETS media types. ### Pagination There a few pieces of useful information about each request stored in the HTTP Headers: - `X-Total-Count` shows you the total amount of listings that match your current query. - `Link` contains pre-built pagination links for accessing the next 'page' of listings that match your query. Read more about that [here](https://simplyrets.com/blog/api-pagination.html).
5
+
6
+ OpenAPI spec version: 1.0.0
7
+
8
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
9
+
10
+ Licensed under the Apache License, Version 2.0 (the "License");
11
+ you may not use this file except in compliance with the License.
12
+ You may obtain a copy of the License at
13
+
14
+ http://www.apache.org/licenses/LICENSE-2.0
15
+
16
+ Unless required by applicable law or agreed to in writing, software
17
+ distributed under the License is distributed on an "AS IS" BASIS,
18
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ See the License for the specific language governing permissions and
20
+ limitations under the License.
21
+
22
+ =end
23
+
24
+ require 'date'
25
+ require 'json'
26
+ require 'logger'
27
+ require 'tempfile'
28
+ require 'typhoeus'
29
+ require 'uri'
30
+
31
+ module SimplyRetsClient
32
+ class ApiClient
33
+ # The Configuration object holding settings to be used in the API client.
34
+ attr_accessor :config
35
+
36
+ # Defines the headers to be used in HTTP requests of all API calls by default.
37
+ #
38
+ # @return [Hash]
39
+ attr_accessor :default_headers
40
+
41
+ # Initializes the ApiClient
42
+ # @option config [Configuration] Configuraiton for initializing the object, default to Configuration.default
43
+ def initialize(config = Configuration.default)
44
+ @config = config
45
+ @user_agent = "Swagger-Codegen/#{VERSION}/ruby"
46
+ @default_headers = {
47
+ 'Content-Type' => "application/json",
48
+ 'User-Agent' => @user_agent
49
+ }
50
+ end
51
+
52
+ def self.default
53
+ @@default ||= ApiClient.new
54
+ end
55
+
56
+ # Call an API with given options.
57
+ #
58
+ # @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
59
+ # the data deserialized from response body (could be nil), response status code and response headers.
60
+ def call_api(http_method, path, opts = {})
61
+ request = build_request(http_method, path, opts)
62
+ response = request.run
63
+
64
+ if @config.debugging
65
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
66
+ end
67
+
68
+ unless response.success?
69
+ fail ApiError.new(:code => response.code,
70
+ :response_headers => response.headers,
71
+ :response_body => response.body),
72
+ response.status_message
73
+ end
74
+
75
+ if opts[:return_type]
76
+ data = deserialize(response, opts[:return_type])
77
+ else
78
+ data = nil
79
+ end
80
+ return data, response.code, response.headers
81
+ end
82
+
83
+ # Builds the HTTP request
84
+ #
85
+ # @param [String] http_method HTTP method/verb (e.g. POST)
86
+ # @param [String] path URL path (e.g. /account/new)
87
+ # @option opts [Hash] :header_params Header parameters
88
+ # @option opts [Hash] :query_params Query parameters
89
+ # @option opts [Hash] :form_params Query parameters
90
+ # @option opts [Object] :body HTTP body (JSON/XML)
91
+ # @return [Typhoeus::Request] A Typhoeus Request
92
+ def build_request(http_method, path, opts = {})
93
+ url = build_request_url(path)
94
+ http_method = http_method.to_sym.downcase
95
+
96
+ header_params = @default_headers.merge(opts[:header_params] || {})
97
+ query_params = opts[:query_params] || {}
98
+ form_params = opts[:form_params] || {}
99
+
100
+ update_params_for_auth! header_params, query_params, opts[:auth_names]
101
+
102
+ req_opts = {
103
+ :method => http_method,
104
+ :headers => header_params,
105
+ :params => query_params,
106
+ :timeout => @config.timeout,
107
+ :ssl_verifypeer => @config.verify_ssl,
108
+ :sslcert => @config.cert_file,
109
+ :sslkey => @config.key_file,
110
+ :verbose => @config.debugging
111
+ }
112
+
113
+ req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
114
+
115
+ if [:post, :patch, :put, :delete].include?(http_method)
116
+ req_body = build_request_body(header_params, form_params, opts[:body])
117
+ req_opts.update :body => req_body
118
+ if @config.debugging
119
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
120
+ end
121
+ end
122
+
123
+ Typhoeus::Request.new(url, req_opts)
124
+ end
125
+
126
+ # Check if the given MIME is a JSON MIME.
127
+ # JSON MIME examples:
128
+ # application/json
129
+ # application/json; charset=UTF8
130
+ # APPLICATION/JSON
131
+ # @param [String] mime MIME
132
+ # @return [Boolean] True if the MIME is applicaton/json
133
+ def json_mime?(mime)
134
+ !(mime =~ /\Aapplication\/json(;.*)?\z/i).nil?
135
+ end
136
+
137
+ # Deserialize the response to the given return type.
138
+ #
139
+ # @param [Response] response HTTP response
140
+ # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
141
+ def deserialize(response, return_type)
142
+ body = response.body
143
+ return nil if body.nil? || body.empty?
144
+
145
+ # return response body directly for String return type
146
+ return body if return_type == 'String'
147
+
148
+ # handle file downloading - save response body into a tmp file and return the File instance
149
+ return download_file(response) if return_type == 'File'
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
+ SimplyRetsClient.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
+ #
214
+ # @see Configuration#temp_folder_path
215
+ # @return [Tempfile] the file downloaded
216
+ def download_file(response)
217
+ content_disposition = response.headers['Content-Disposition']
218
+ if content_disposition
219
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
220
+ prefix = sanitize_filename(filename)
221
+ else
222
+ prefix = 'download-'
223
+ end
224
+ prefix = prefix + '-' unless prefix.end_with?('-')
225
+
226
+ tempfile = nil
227
+ encoding = response.body.encoding
228
+ Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) do |file|
229
+ file.write(response.body)
230
+ tempfile = file
231
+ end
232
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
233
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
234
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
235
+ "explicitly with `tempfile.delete`"
236
+ tempfile
237
+ end
238
+
239
+ # Sanitize filename by removing path.
240
+ # e.g. ../../sun.gif becomes sun.gif
241
+ #
242
+ # @param [String] filename the filename to be sanitized
243
+ # @return [String] the sanitized filename
244
+ def sanitize_filename(filename)
245
+ filename.gsub(/.*[\/\\]/, '')
246
+ end
247
+
248
+ def build_request_url(path)
249
+ # Add leading and trailing slashes to path
250
+ path = "/#{path}".gsub(/\/+/, '/')
251
+ URI.encode(@config.base_url + path)
252
+ end
253
+
254
+ # Builds the HTTP request body
255
+ #
256
+ # @param [Hash] header_params Header parameters
257
+ # @param [Hash] form_params Query parameters
258
+ # @param [Object] body HTTP body (JSON/XML)
259
+ # @return [String] HTTP body data in the form of string
260
+ def build_request_body(header_params, form_params, body)
261
+ # http form
262
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
263
+ header_params['Content-Type'] == 'multipart/form-data'
264
+ data = {}
265
+ form_params.each do |key, value|
266
+ case value
267
+ when File, Array, nil
268
+ # let typhoeus handle File, Array and nil parameters
269
+ data[key] = value
270
+ else
271
+ data[key] = value.to_s
272
+ end
273
+ end
274
+ elsif body
275
+ data = body.is_a?(String) ? body : body.to_json
276
+ else
277
+ data = nil
278
+ end
279
+ data
280
+ end
281
+
282
+ # Update hearder and query params based on authentication settings.
283
+ #
284
+ # @param [Hash] header_params Header parameters
285
+ # @param [Hash] form_params Query parameters
286
+ # @param [String] auth_names Authentication scheme name
287
+ def update_params_for_auth!(header_params, query_params, auth_names)
288
+ Array(auth_names).each do |auth_name|
289
+ auth_setting = @config.auth_settings[auth_name]
290
+ next unless auth_setting
291
+ case auth_setting[:in]
292
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
293
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
294
+ else fail ArgumentError, 'Authentication token must be in `query` of `header`'
295
+ end
296
+ end
297
+ end
298
+
299
+ # Sets user agent in HTTP header
300
+ #
301
+ # @param [String] user_agent User agent (e.g. swagger-codegen/ruby/1.0.0)
302
+ def user_agent=(user_agent)
303
+ @user_agent = user_agent
304
+ @default_headers['User-Agent'] = @user_agent
305
+ end
306
+
307
+ # Return Accept header based on an array of accepts provided.
308
+ # @param [Array] accepts array for Accept
309
+ # @return [String] the Accept header (e.g. application/json)
310
+ def select_header_accept(accepts)
311
+ return nil if accepts.nil? || accepts.empty?
312
+ # use JSON when present, otherwise use all of the provided
313
+ json_accept = accepts.find { |s| json_mime?(s) }
314
+ return json_accept || accepts.join(',')
315
+ end
316
+
317
+ # Return Content-Type header based on an array of content types provided.
318
+ # @param [Array] content_types array for Content-Type
319
+ # @return [String] the Content-Type header (e.g. application/json)
320
+ def select_header_content_type(content_types)
321
+ # use application/json by default
322
+ return 'application/json' if content_types.nil? || content_types.empty?
323
+ # use JSON when present, otherwise use the first one
324
+ json_content_type = content_types.find { |s| json_mime?(s) }
325
+ return json_content_type || content_types.first
326
+ end
327
+
328
+ # Convert object (array, hash, object, etc) to JSON string.
329
+ # @param [Object] model object to be converted into JSON string
330
+ # @return [String] JSON string representation of the object
331
+ def object_to_http_body(model)
332
+ return model if model.nil? || model.is_a?(String)
333
+ local_body = nil
334
+ if model.is_a?(Array)
335
+ local_body = model.map{|m| object_to_hash(m) }
336
+ else
337
+ local_body = object_to_hash(model)
338
+ end
339
+ local_body.to_json
340
+ end
341
+
342
+ # Convert object(non-array) to hash.
343
+ # @param [Object] obj object to be converted into JSON string
344
+ # @return [String] JSON string representation of the object
345
+ def object_to_hash(obj)
346
+ if obj.respond_to?(:to_hash)
347
+ obj.to_hash
348
+ else
349
+ obj
350
+ end
351
+ end
352
+
353
+ # Build parameter value according to the given collection format.
354
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
355
+ def build_collection_param(param, collection_format)
356
+ case collection_format
357
+ when :csv
358
+ param.join(',')
359
+ when :ssv
360
+ param.join(' ')
361
+ when :tsv
362
+ param.join("\t")
363
+ when :pipes
364
+ param.join('|')
365
+ when :multi
366
+ # return the array directly as typhoeus will handle it as expected
367
+ param
368
+ else
369
+ fail "unknown collection format: #{collection_format.inspect}"
370
+ end
371
+ end
372
+ end
373
+ end