Sha256: 3d56ca818045cebc43cf679e1cc4f0479c041122237fff0085e4df8ef0182ffd
Contents?: true
Size: 1.52 KB
Versions: 3
Compression:
Stored size: 1.52 KB
Contents
require 'timeout' module Edmunds module Request API_URL = 'https://api.edmunds.com/api' API_VERSION = Edmunds.configuration.api_version.to_s DEFAULT_TIMEOUT = 15 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 def build_api_url(api_path, path) api_version = 'v' + API_VERSION API_URL + '/' + api_path + '/' + api_version + path end def merge_required_params(options) { api_key: Edmunds.configuration.api_key.to_s.strip, fmt: Edmunds.configuration.request_params.fmt }.merge(options) end def http_headers { 'User-Agent' => 'EdmundsApi ruby client', 'Accept' => 'application/json' } end def timeout Edmunds.configuration.timeout || DEFAULT_TIMEOUT end 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 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 end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
edmunds_api-0.1.8 | lib/edmunds/request.rb |
edmunds_api-0.1.7 | lib/edmunds/request.rb |
edmunds_api-0.1.6 | lib/edmunds/request.rb |