class TheCity::Error

Custom error class for rescuing from all The City API errors

Constants

EnhanceYourCalm

Raised when The City returns the HTTP status code 429

RateLimited

Raised when The City returns the HTTP status code 429

Attributes

code[R]
rate_limit[R]
wrapped_exception[R]

Public Class Methods

descendants() click to toggle source

@return [Array]

# File lib/the_city/error.rb, line 26
def self.descendants
  @descendants ||= []
end
errors() click to toggle source

@return [Hash]

# File lib/the_city/error.rb, line 18
def self.errors
  @errors ||= descendants.inject({}) do |hash, klass|
    hash[klass::HTTP_STATUS_CODE] = klass
    hash
  end
end
from_response(response={}) click to toggle source

Create a new error from an HTTP response

@param response [Hash] @return [TheCity::Error]

# File lib/the_city/error.rb, line 12
def self.from_response(response={})
  error, code = parse_error(response[:body])
  new(error, response[:response_headers], code)
end
inherited(descendant) click to toggle source

@return [Array]

# File lib/the_city/error.rb, line 31
def self.inherited(descendant)
  descendants << descendant
end
new(exception=$!, response_headers={}, code=nil) click to toggle source

Initializes a new Error object

@param exception [Exception, String] @param response_headers [Hash] @param code [Integer] @return [TheCity::Error]

Calls superclass method
# File lib/the_city/error.rb, line 41
def initialize(exception=$!, response_headers={}, code=nil)
  @rate_limit = TheCity::RateLimit.new(response_headers)
  @wrapped_exception = exception
  @code = code
  exception.respond_to?(:message) ? super(exception.message) : super(exception.to_s)
end

Private Class Methods

parse_error(body) click to toggle source
# File lib/the_city/error.rb, line 50
def self.parse_error(body)
  if body.nil?
    ['', nil]
  elsif body[:error]
    [body[:error], nil]
  elsif body[:errors]
    first = Array(body[:errors]).first
    if first.is_a?(Hash)
      [first[:message].chomp, first[:code]]
    else
      [first.chomp, nil]
    end
  end
end