class SmartcarError

Custom SmartcarError class to represent errors from Smartcar APIs.

Attributes

code[R]
description[R]
detail[R]
doc_url[R]
request_id[R]
resolution[R]
status_code[R]
type[R]

Public Class Methods

new(status, body, headers) click to toggle source
Calls superclass method
# File lib/smartcar_error.rb, line 7
def initialize(status, body, headers)
  @status_code = status
  if body.is_a?(String)
    super(body)
    @request_id = headers['sc-request-id']
    return
  end
  body = coerce_attributes(body)

  super("#{body[:type]}:#{body[:code]} - #{body[:description]}")
  @request_id = body[:requestId] || headers['sc-request-id']
  set_attributes(body)
end

Private Instance Methods

coerce_attributes(body) click to toggle source
# File lib/smartcar_error.rb, line 23
def coerce_attributes(body)
  body[:type] = body.delete(:error) if body[:error]
  unless body[:description]
    body[:description] = if body[:error_description]
                           body.delete(:error_description)
                         elsif body[:message]
                           body.delete(:message)
                         else
                           'Unknown error'
                         end
  end

  body
end
set_attributes(body) click to toggle source
# File lib/smartcar_error.rb, line 38
def set_attributes(body)
  body.each do |attribute, value|
    instance_variable_set("@#{attribute}", value)
  end
  @doc_url = body[:docURL]
  @type = @error if @error

  return unless @resolution

  @resolution = @resolution.is_a?(String) ? OpenStruct.new({ type: @resolution }) : OpenStruct.new(@resolution)
end