module Edmunds::Request
Constants
- API_URL
- API_VERSION
- DEFAULT_TIMEOUT
Public Instance Methods
api_call(api_path, path, options={})
click to toggle source
# File lib/edmunds/request.rb, line 10 def api_call(api_path, path, options={}) url = build_api_url api_path, path request_params = merge_required_params options raise 'No api_key found' if request_params[:api_key].empty? response = Timeout::timeout(timeout) { HTTP.headers(http_headers).get(url, params: request_params) } parse_response response end
Private Instance Methods
build_api_url(api_path, path)
click to toggle source
# File lib/edmunds/request.rb, line 22 def build_api_url(api_path, path) api_version = 'v' + API_VERSION API_URL + '/' + api_path + '/' + api_version + path end
http_headers()
click to toggle source
# File lib/edmunds/request.rb, line 34 def http_headers { 'User-Agent' => 'EdmundsApi ruby client', 'Accept' => 'application/json' } end
merge_required_params(options)
click to toggle source
# File lib/edmunds/request.rb, line 27 def merge_required_params(options) { api_key: Edmunds.configuration.api_key.to_s.strip, fmt: Edmunds.configuration.request_params.fmt }.merge(options) end
parse_response(response)
click to toggle source
# File lib/edmunds/request.rb, line 45 def parse_response(response) begin body = JSON.parse response.body rescue body = {} end if response.code == 200 body else raise_error response.code, body end end
raise_error(code, body)
click to toggle source
# File lib/edmunds/request.rb, line 59 def raise_error(code, body) klass = Edmunds::Error::ERRORS[code] if klass.nil? klass = Edmunds::Error::Unknown end raise klass.from_response code, body end
timeout()
click to toggle source
# File lib/edmunds/request.rb, line 41 def timeout Edmunds.configuration.timeout || DEFAULT_TIMEOUT end