simplyrets 1.0.7 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +23 -21
  3. data/Makefile +48 -0
  4. data/README.org +5 -58
  5. data/example.rb +14 -4
  6. data/lib/simplyrets.rb +42 -21
  7. data/lib/simplyrets/api/default_api.rb +292 -59
  8. data/lib/simplyrets/api_client.rb +370 -0
  9. data/lib/simplyrets/api_error.rb +36 -0
  10. data/lib/simplyrets/configuration.rb +170 -0
  11. data/lib/simplyrets/models/agent.rb +146 -11
  12. data/lib/simplyrets/models/broker.rb +133 -7
  13. data/lib/simplyrets/models/contact_information.rb +149 -17
  14. data/lib/simplyrets/models/error.rb +138 -9
  15. data/lib/simplyrets/models/geographic_data.rb +150 -12
  16. data/lib/simplyrets/models/listing.rb +248 -49
  17. data/lib/simplyrets/models/mls_information.rb +148 -12
  18. data/lib/simplyrets/models/office.rb +146 -11
  19. data/lib/simplyrets/models/open_house.rb +215 -12
  20. data/lib/simplyrets/models/parking.rb +148 -17
  21. data/lib/simplyrets/models/property.rb +353 -129
  22. data/lib/simplyrets/models/sales.rb +163 -25
  23. data/lib/simplyrets/models/school.rb +167 -24
  24. data/lib/simplyrets/models/street_address.rb +204 -26
  25. data/lib/simplyrets/models/tax.rb +150 -18
  26. data/lib/simplyrets/version.rb +15 -0
  27. data/multi-query-param.patch +44 -0
  28. data/simplyrets.gemspec +4 -4
  29. data/spec/api/default_api_spec.rb +172 -0
  30. data/spec/models/agent_spec.rb +75 -0
  31. data/spec/models/broker_spec.rb +45 -0
  32. data/spec/models/contact_information_spec.rb +65 -0
  33. data/spec/models/error_spec.rb +55 -0
  34. data/spec/models/geographic_data_spec.rb +85 -0
  35. data/spec/models/listing_spec.rb +245 -0
  36. data/spec/models/mls_information_spec.rb +75 -0
  37. data/spec/models/office_spec.rb +75 -0
  38. data/spec/models/open_house_spec.rb +115 -0
  39. data/spec/models/parking_spec.rb +65 -0
  40. data/spec/models/property_spec.rb +365 -0
  41. data/spec/models/sales_spec.rb +85 -0
  42. data/spec/models/school_spec.rb +75 -0
  43. data/spec/models/street_address_spec.rb +105 -0
  44. data/spec/models/tax_spec.rb +65 -0
  45. data/spec/spec_helper.rb +78 -0
  46. metadata +33 -18
  47. data/lib/simplyrets.rb~ +0 -34
  48. data/lib/simplyrets/api/#default_api.rb# +0 -146
  49. data/lib/simplyrets/simplyrets/api_error.rb +0 -26
  50. data/lib/simplyrets/simplyrets/configuration.rb +0 -101
  51. data/lib/simplyrets/simplyrets/request.rb +0 -248
  52. data/lib/simplyrets/simplyrets/response.rb +0 -156
  53. data/lib/simplyrets/simplyrets/version.rb +0 -5
  54. data/simplyrets-1.0.6.gem +0 -0
@@ -1,34 +0,0 @@
1
- # SimplyRets common files
2
- require 'simplyrets/simplyrets'
3
- require 'simplyrets/simplyrets/configuration'
4
- require 'simplyrets/simplyrets/api_error'
5
- require 'simplyrets/simplyrets/request'
6
- require 'simplyrets/simplyrets/response'
7
- require 'simplyrets/simplyrets/version'
8
-
9
- # Models
10
- require 'simplyrets/models/base_object'
11
- require 'simplyrets/models/street_address'
12
- require 'simplyrets/models/property'
13
- require 'simplyrets/models/listing'
14
- require 'simplyrets/models/open_house'
15
- require 'simplyrets/models/office'
16
- require 'simplyrets/models/agent'
17
- require 'simplyrets/models/sales'
18
- require 'simplyrets/models/school'
19
- require 'simplyrets/models/parking'
20
- require 'simplyrets/models/contact_information'
21
- require 'simplyrets/models/tax'
22
- require 'simplyrets/models/geographic_data'
23
- require 'simplyrets/models/broker'
24
- require 'simplyrets/models/mls_information'
25
- require 'simplyrets/models/error'
26
-
27
- # APIs
28
- require 'simplyrets/api/default_api'
29
-
30
- module SimplyRetsClient
31
- # Initialize the default configuration
32
- SimplyRets.configuration = SimplyRets::Configuration.new
33
- SimplyRets.configure { |config| }
34
- end
@@ -1,146 +0,0 @@
1
- require "uri"
2
-
3
- module SimplyRetsClient
4
- class Properties_api
5
-
6
- # The SimplyRets Listings API
7
- # This is the main endpoint for accessing your properties. View\nall of the available query parameters and make requests below!\nThe API uses Basic Authorization, which most HTTP libraries\nwill handle for you. To use the test test data (which is what\nthis pages uses), you can use the api key `simplyrets` and\nsecret `simplyrets`. Note that the test listings are not live\nMLS listings but the data, query parameters, and response\nbodies will all work the same.\n
8
- # @param [Hash] opts the optional parameters
9
- # @option opts [String] :q A fuzzy keyword search.\n
10
- # @option opts [Integer] :minprice Filter listings by a minimum price.\n
11
- # @option opts [Integer] :maxprice Filter listings by a maximum price\n
12
- # @option opts [String] :agent Filter the listings returned by an agent ID. Note, the\nAgent ID is provided by your MLS.\n
13
- # @option opts [Integer] :minarea Filter listings by a minimum area size in Sq Ft.\n
14
- # @option opts [Integer] :maxarea Filter listings by a maximum area size in Sq Ft.\n
15
- # @option opts [Integer] :minbaths Filter listings by a minimum number of bathrooms.\n
16
- # @option opts [Integer] :maxbaths Filter listings by a maximum number of bathrooms.\n
17
- # @option opts [Integer] :minbeds Filter listings by a minimum number of bedrooms.\n
18
- # @option opts [Integer] :maxbeds Filter listings by a maximum number of bedrooms.\n
19
- # @option opts [Integer] :limit Set the number of listings to return in the response.\nThis defaults to 20 listings, and can be a maximum of 50.\nTo paginate through to the next page of listings, take a\nlook at the `offset` parameter, or the Link in the HTTP\nHeader.\n
20
- # @option opts [Integer] :offset Used as a cursor for pagination. Increase the offset parameter\nby the limit to go to the next \"page\" of listings. Also take\na look at the Link HTTP Header for pre-built pagination.\n
21
- # @option opts [Array<String>] :brokers Filter the listings returned by brokerage with a Broker ID.\nYou can specific multiple broker parameters. Note, the Broker\nID is provided by your MLS.\n
22
- # @option opts [Array<String>] :features Filter the listings returned by specific features. You\ncan specify multiple.\n
23
- # @option opts [Array<String>] :amenities Filter the listings returned by specific amenities. You\ncan specify multiple amenities.\n
24
- # @option opts [Array<String>] :neighborhoods Filter the listings returned by specific neighborhoods and\nsubdivisions. You can specify multiple.\n
25
- # @option opts [Array<String>] :points A list of latitude longitude coordinates which contain\nthe property. Our documentation generator does not yet\nsupport using a list of parameters.\n
26
- # @option opts [Array<String>] :counties Filter the listings returned by specific counties. You can\nspecify multiple.\n
27
- # @option opts [Array<String>] :status Filter the listings returned by a specific status. You can\nspecify multiple.\n
28
- # @option opts [String] :type Request listings by a specific property type. This\ndefaults to Residential, and you can only specify multiple property types in a single query.\n
29
- # @option opts [String] :sort Sort the response by a specific field. Values starting\nwith a minus (-) denote descending order, while the others\nare ascending.\n
30
- # @option opts [String] :include Include a limited set of extra fields which are not found\nin the default response body\n- &#39;rooms&#39; include parameter will include\n any additional rooms as a list.\n- &#39;pool&#39; includes an additional pool description\n- &#39;association&#39; includes additional home owners association data\n
31
- # @return [Array<Listing>]
32
- def self.properties(opts = {})
33
- if SimplyRets.configuration.debug
34
- SimplyRets.logger.debug "Calling API: DefaultApi#properties ..."
35
- end
36
-
37
- if opts[:'sort'] && !['listprice', '-listprice', 'listdate', '-listdate', 'beds', '-beds', 'baths', '-baths'].include?(opts[:'sort'])
38
- fail 'invalid value for "sort", must be one of listprice, -listprice, listdate, -listdate, beds, -beds, baths, -baths'
39
- end
40
-
41
- if opts[:'include'] && !['pool', 'rooms', 'association'].include?(opts[:'include'])
42
- fail 'invalid value for "include", must be one of pool, rooms, association'
43
- end
44
-
45
- # resource path
46
- path = "/properties".sub('{format}','json')
47
-
48
- # query parameters
49
- query_params = {}
50
- query_params[:'q'] = opts[:'q'] if opts[:'q']
51
- query_params[:'minprice'] = opts[:'minprice'] if opts[:'minprice']
52
- query_params[:'maxprice'] = opts[:'maxprice'] if opts[:'maxprice']
53
- query_params[:'agent'] = opts[:'agent'] if opts[:'agent']
54
- query_params[:'minarea'] = opts[:'minarea'] if opts[:'minarea']
55
- query_params[:'maxarea'] = opts[:'maxarea'] if opts[:'maxarea']
56
- query_params[:'minbaths'] = opts[:'minbaths'] if opts[:'minbaths']
57
- query_params[:'maxbaths'] = opts[:'maxbaths'] if opts[:'maxbaths']
58
- query_params[:'minbeds'] = opts[:'minbeds'] if opts[:'minbeds']
59
- query_params[:'maxbeds'] = opts[:'maxbeds'] if opts[:'maxbeds']
60
- query_params[:'limit'] = opts[:'limit'] if opts[:'limit']
61
- query_params[:'offset'] = opts[:'offset'] if opts[:'offset']
62
- query_params[:'brokers'] = opts[:'brokers'] if opts[:'brokers']
63
- query_params[:'features'] = opts[:'features'] if opts[:'features']
64
- query_params[:'amenities'] = opts[:'amenities'] if opts[:'amenities']
65
- query_params[:'neighborhoods'] = opts[:'neighborhoods'] if opts[:'neighborhoods']
66
- query_params[:'points'] = opts[:'points'] if opts[:'points']
67
- query_params[:'counties'] = opts[:'counties'] if opts[:'counties']
68
- query_params[:'status'] = opts[:'status'] if opts[:'status']
69
- query_params[:'type'] = opts[:'type'] if opts[:'type']
70
- query_params[:'sort'] = opts[:'sort'] if opts[:'sort']
71
- query_params[:'include'] = opts[:'include'] if opts[:'include']
72
-
73
- # header parameters
74
- header_params = {}
75
-
76
- # HTTP header 'Accept' (if needed)
77
- _header_accept = []
78
- _header_accept_result = SimplyRets::Request.select_header_accept(_header_accept) and header_params['Accept'] = _header_accept_result
79
-
80
- # HTTP header 'Content-Type'
81
- _header_content_type = []
82
- header_params['Content-Type'] = SimplyRets::Request.select_header_content_type(_header_content_type)
83
-
84
- # form parameters
85
- form_params = {}
86
-
87
- # http body (model)
88
- post_body = nil
89
-
90
-
91
- auth_names = ['basicAuth']
92
- response = SimplyRets::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
93
- result = response.deserialize('Array<Listing>')
94
- if SimplyRets.configuration.debug
95
- SimplyRets.logger.debug "API called: DefaultApi#properties. Result: #{result.inspect}"
96
- end
97
- result
98
- end
99
-
100
- # Single Listing Endpoint
101
- # Use this endpoint for accessing a single listing.\n
102
- # @param listing_id A listings listingId (unique indentifier).
103
- # @param [Hash] opts the optional parameters
104
- # @return [Listing]
105
- def self.property(listing_id, opts = {})
106
- if SimplyRets.configuration.debug
107
- SimplyRets.logger.debug "Calling API: DefaultApi#property ..."
108
- end
109
-
110
- # verify the required parameter 'listing_id' is set
111
- fail "Missing the required parameter 'listing_id' when calling property" if listing_id.nil?
112
-
113
- # resource path
114
- path = "/properties/{listingId}".sub('{format}','json').sub('{' + 'listingId' + '}', listing_id.to_s)
115
-
116
- # query parameters
117
- query_params = {}
118
-
119
- # header parameters
120
- header_params = {}
121
-
122
- # HTTP header 'Accept' (if needed)
123
- _header_accept = []
124
- _header_accept_result = SimplyRets::Request.select_header_accept(_header_accept) and header_params['Accept'] = _header_accept_result
125
-
126
- # HTTP header 'Content-Type'
127
- _header_content_type = []
128
- header_params['Content-Type'] = SimplyRets::Request.select_header_content_type(_header_content_type)
129
-
130
- # form parameters
131
- form_params = {}
132
-
133
- # http body (model)
134
- post_body = nil
135
-
136
-
137
- auth_names = ['basicAuth']
138
- response = SimplyRets::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
139
- result = response.deserialize('Listing')
140
- if SimplyRets.configuration.debug
141
- SimplyRets.logger.debug "API called: DefaultApi#property. Result: #{result.inspect}"
142
- end
143
- result
144
- end
145
- end
146
- end
@@ -1,26 +0,0 @@
1
- module SimplyRetsClient
2
- module SimplyRets
3
- class ApiError < StandardError
4
- attr_reader :code, :response_headers, :response_body
5
-
6
- # Usage examples:
7
- # ApiError.new
8
- # ApiError.new("message")
9
- # ApiError.new(:code => 500, :response_headers => {}, :response_body => "")
10
- # ApiError.new(:code => 404, :message => "Not Found")
11
- def initialize(arg = nil)
12
- if arg.is_a? Hash
13
- arg.each do |k, v|
14
- if k.to_s == 'message'
15
- super v
16
- else
17
- instance_variable_set "@#{k}", v
18
- end
19
- end
20
- else
21
- super arg
22
- end
23
- end
24
- end
25
- end
26
- end
@@ -1,101 +0,0 @@
1
- require 'logger'
2
-
3
- module SimplyRetsClient
4
- module SimplyRets
5
- class Configuration
6
- attr_accessor :scheme, :host, :base_path, :user_agent, :format, :auth_token, :inject_format, :force_ending_format
7
-
8
- # Defines the username used with HTTP basic authentication.
9
- #
10
- # @return [String]
11
- attr_accessor :username
12
-
13
- # Defines the password used with HTTP basic authentication.
14
- #
15
- # @return [String]
16
- attr_accessor :password
17
-
18
- # Defines API keys used with API Key authentications.
19
- #
20
- # @return [Hash] key: parameter name, value: parameter value (API key)
21
- #
22
- # @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string)
23
- # config.api_key['api_key'] = 'xxx'
24
- attr_accessor :api_key
25
-
26
- # Defines API key prefixes used with API Key authentications.
27
- #
28
- # @return [Hash] key: parameter name, value: API key prefix
29
- #
30
- # @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers)
31
- # config.api_key_prefix['api_key'] = 'Token'
32
- attr_accessor :api_key_prefix
33
-
34
- # Set this to false to skip verifying SSL certificate when calling API from https server.
35
- # Default to true.
36
- #
37
- # @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
38
- #
39
- # @return [true, false]
40
- attr_accessor :verify_ssl
41
-
42
- # Set this to customize the certificate file to verify the peer.
43
- #
44
- # @return [String] the path to the certificate file
45
- #
46
- # @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
47
- # https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
48
- attr_accessor :ssl_ca_cert
49
-
50
- # Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
51
- # details will be logged with `logger.debug` (see the `logger` attribute).
52
- # Default to false.
53
- #
54
- # @return [true, false]
55
- attr_accessor :debug
56
-
57
- # Defines the logger used for debugging.
58
- # Default to `Rails.logger` (when in Rails) or logging to STDOUT.
59
- #
60
- # @return [#debug]
61
- attr_accessor :logger
62
-
63
- # Defines the temporary folder to store downloaded files
64
- # (for API endpoints that have file response).
65
- # Default to use `Tempfile`.
66
- #
67
- # @return [String]
68
- attr_accessor :temp_folder_path
69
-
70
- # Defines the headers to be used in HTTP requests of all API calls by default.
71
- #
72
- # @return [Hash]
73
- attr_accessor :default_headers
74
-
75
- # Defaults go in here..
76
- def initialize
77
- @format = 'json'
78
- @scheme = 'https'
79
- @host = 'api.simplyrets.com'
80
- @base_path = '/'
81
- @user_agent = "ruby-simplyrets-#{SimplyRets::VERSION}"
82
- @inject_format = false
83
- @force_ending_format = false
84
-
85
- @default_headers = {
86
- 'Content-Type' => "application/#{@format.downcase}",
87
- 'User-Agent' => @user_agent
88
- }
89
-
90
- # keys for API key authentication (param-name => api-key)
91
- @api_key = {}
92
- @api_key_prefix = {}
93
-
94
- @verify_ssl = true
95
-
96
- @debug = false
97
- @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
98
- end
99
- end
100
- end
101
- end
@@ -1,248 +0,0 @@
1
- require 'uri'
2
- require 'typhoeus'
3
-
4
- module SimplyRetsClient
5
- module SimplyRets
6
- class Request
7
- attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params, :auth_names, :response
8
-
9
- # All requests must have an HTTP method and a path
10
- # Optionals parameters are :params, :headers, :body, :format, :host
11
- def initialize(http_method, path, attributes = {})
12
- @http_method = http_method.to_sym.downcase
13
- @path = path
14
-
15
- attributes.each { |name, value| send "#{name}=", value }
16
-
17
- @format ||= SimplyRets.configuration.format
18
- @params ||= {}
19
-
20
- # Apply default headers
21
- @headers = SimplyRets.configuration.default_headers.merge(@headers || {})
22
-
23
- # Stick in the auth token if there is one
24
- if SimplyRets.authenticated?
25
- @headers.merge!({:auth_token => SimplyRets.configuration.auth_token})
26
- end
27
-
28
- update_params_for_auth!
29
-
30
- end
31
-
32
-
33
- # Update hearder and query params based on authentication settings.
34
- def update_params_for_auth!
35
- (@auth_names || []).each do |auth_name|
36
- case auth_name
37
- when 'basicAuth'
38
- @headers ||= {}
39
- http_auth_header = 'Basic ' + ["#{SimplyRets.configuration.username}:#{SimplyRets.configuration.password}"].pack('m').delete("\r\n")
40
- @headers['Authorization'] = http_auth_header
41
-
42
- end
43
- end
44
- end
45
-
46
-
47
- # Get API key (with prefix if set).
48
- # @param [String] param_name the parameter name of API key auth
49
- def get_api_key_with_prefix(param_name)
50
- if SimplyRets.configuration.api_key_prefix[param_name]
51
- "#{SimplyRets.configuration.api_key_prefix[param_name]} #{SimplyRets.configuration.api_key[param_name]}"
52
- else
53
- SimplyRets.configuration.api_key[param_name]
54
- end
55
- end
56
-
57
- # Construct the request URL.
58
- def url(options = {})
59
- _path = self.interpreted_path
60
- _path = "/#{_path}" unless _path.start_with?('/')
61
- "#{SimplyRets.configuration.scheme}://#{SimplyRets.configuration.host}#{_path}"
62
- end
63
-
64
- # Iterate over the params hash, injecting any path values into the path string
65
- # e.g. /word.{format}/{word}/entries => /word.json/cat/entries
66
- def interpreted_path
67
- p = self.path.dup
68
-
69
- # Stick a .{format} placeholder into the path if there isn't
70
- # one already or an actual format like json or xml
71
- # e.g. /words/blah => /words.{format}/blah
72
- if SimplyRets.configuration.inject_format
73
- unless ['.json', '.xml', '{format}'].any? {|s| p.downcase.include? s }
74
- p = p.sub(/^(\/?\w+)/, "\\1.#{format}")
75
- end
76
- end
77
-
78
- # Stick a .{format} placeholder on the end of the path if there isn't
79
- # one already or an actual format like json or xml
80
- # e.g. /words/blah => /words/blah.{format}
81
- if SimplyRets.configuration.force_ending_format
82
- unless ['.json', '.xml', '{format}'].any? {|s| p.downcase.include? s }
83
- p = "#{p}.#{format}"
84
- end
85
- end
86
-
87
- p = p.sub("{format}", self.format.to_s)
88
-
89
- URI.encode [SimplyRets.configuration.base_path, p].join("/").gsub(/\/+/, '/')
90
- end
91
-
92
- # If body is an object, JSONify it before making the actual request.
93
- # For form parameters, remove empty value
94
- def outgoing_body
95
- # http form
96
- if headers['Content-Type'] == 'application/x-www-form-urlencoded'
97
- data = form_params.dup
98
- data.each do |key, value|
99
- data[key] = value.to_s if value && !value.is_a?(File) # remove emtpy form parameter
100
- end
101
- elsif @body # http body is JSON
102
- data = @body.is_a?(String) ? @body : @body.to_json
103
- else
104
- data = nil
105
- end
106
-
107
- if SimplyRets.configuration.debug
108
- SimplyRets.logger.debug "HTTP request body param ~BEGIN~\n#{data}\n~END~\n"
109
- end
110
-
111
- data
112
- end
113
-
114
- def make
115
- request_options = {
116
- :method => self.http_method,
117
- :headers => self.headers,
118
- :params => self.params,
119
- :ssl_verifypeer => SimplyRets.configuration.verify_ssl,
120
- :cainfo => SimplyRets.configuration.ssl_ca_cert,
121
- :verbose => SimplyRets.configuration.debug
122
- }
123
-
124
- if [:post, :patch, :put, :delete].include?(self.http_method)
125
- request_options.update :body => self.outgoing_body
126
- end
127
-
128
- raw = Typhoeus::Request.new(self.url, request_options).run
129
-
130
- @response = Response.new(raw)
131
-
132
- if SimplyRets.configuration.debug
133
- SimplyRets.logger.debug "HTTP response body ~BEGIN~\n#{@response.body}\n~END~\n"
134
- end
135
-
136
- # record as last response
137
- SimplyRets.last_response = @response
138
-
139
- unless @response.success?
140
- fail ApiError.new(:code => @response.code,
141
- :response_headers => @response.headers,
142
- :response_body => @response.body),
143
- @response.status_message
144
- end
145
-
146
- @response
147
- end
148
-
149
- def response_code_pretty
150
- return unless @response
151
- @response.code.to_s
152
- end
153
-
154
- def response_headers_pretty
155
- return unless @response
156
- # JSON.pretty_generate(@response.headers).gsub(/\n/, '<br/>') # <- This was for RestClient
157
- @response.headers.gsub(/\n/, '<br/>') # <- This is for Typhoeus
158
- end
159
-
160
- # return 'Accept' based on an array of accept provided
161
- # @param [Array] header_accept_array Array fo 'Accept'
162
- # @return String Accept (e.g. application/json)
163
- def self.select_header_accept header_accept_array
164
- if header_accept_array.empty?
165
- return
166
- elsif header_accept_array.any?{ |s| s.casecmp('application/json')==0 }
167
- 'application/json' # look for json data by default
168
- else
169
- header_accept_array.join(',')
170
- end
171
- end
172
-
173
- # return the content type based on an array of content-type provided
174
- # @param [Array] content_type_array Array fo content-type
175
- # @return String Content-Type (e.g. application/json)
176
- def self.select_header_content_type content_type_array
177
- if content_type_array.empty?
178
- 'application/json' # use application/json by default
179
- elsif content_type_array.any?{ |s| s.casecmp('application/json')==0 }
180
- 'application/json' # use application/json if it's included
181
- else
182
- content_type_array[0]; # otherwise, use the first one
183
- end
184
- end
185
-
186
- # static method to convert object (array, hash, object, etc) to JSON string
187
- # @param model object to be converted into JSON string
188
- # @return string JSON string representation of the object
189
- def self.object_to_http_body model
190
- return if model.nil?
191
- _body = nil
192
- if model.is_a?(Array)
193
- _body = model.map{|m| object_to_hash(m) }
194
- else
195
- _body = object_to_hash(model)
196
- end
197
- _body.to_json
198
- end
199
-
200
- # static method to convert object(non-array) to hash
201
- # @param obj object to be converted into JSON string
202
- # @return string JSON string representation of the object
203
- def self.object_to_hash obj
204
- if obj.respond_to?(:to_hash)
205
- obj.to_hash
206
- else
207
- obj
208
- end
209
- end
210
-
211
- end
212
- end
213
- end
214
-
215
- # PATCH: array parameter rendering
216
- #
217
- # Typheous defaults to rendering array query parameters with
218
- # an index:
219
- #
220
- # ?points[0]=23.232,-98.232&points[1]=23.232,-98.232
221
- #
222
- # The simplyrets api uses the form:
223
- #
224
- # ?points=23.232,-98.232&points=23.232,-98.232
225
- #
226
- # this monkey-patch removes the array index when the query
227
- # parameter is rendered.
228
- module Ethon
229
- class Easy
230
- module Queryable
231
- private
232
- def recursively_generate_pairs(h, prefix, pairs)
233
- case h
234
- when Hash
235
- h.each_pair do |k,v|
236
- key = prefix.nil? ? k : "#{prefix}"
237
- pairs_for(v, key, pairs)
238
- end
239
- when Array
240
- h.each_with_index do |v, i|
241
- key = "#{prefix}"
242
- pairs_for(v, key, pairs)
243
- end
244
- end
245
- end
246
- end
247
- end
248
- end