class Mercedes::Client
Constants
- BASE_URL
- CarNotAvailable
- NotFound
- ServerError
- TIMEOUT
Public Class Methods
new(options = {})
click to toggle source
# File lib/mercedes/client.rb, line 31 def initialize(options = {}) Configurable::OPTIONS.each do |key| instance_variable_set(:"@#{key}", options[key] || Mercedes.send(key)) end http_client.base_url = BASE_URL end
Private Instance Methods
handle_errors() { || ... }
click to toggle source
# File lib/mercedes/client.rb, line 58 def handle_errors yield.tap do |response| if response.request_timeout? raise CarNotAvailable, 'Car is not available because it is not connected to simulator' elsif response.unauthorized? raise Unauthorized, 'Unauthorized' elsif response.server_error? raise ServerError, 'Server is not available' elsif response.not_found? raise NotFound, 'Resource not found' end
http_client()
click to toggle source
# File lib/mercedes/client.rb, line 41 def http_client @http_client ||= Patron::Session.new do |config| config.timeout = TIMEOUT config.headers = { 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'authorization' => "Bearer #{@token}" } end end
perform_request(method, url, params = {})
click to toggle source
# File lib/mercedes/client.rb, line 52 def perform_request(method, url, params = {}) handle_errors do Response.new(http_client.request(method, url, {}, params)) end end