class CurrencyCloud::ResponseHandler

Attributes

params[R]
response[R]
route[R]
verb[R]

Public Class Methods

new(verb, route, params, response) click to toggle source
# File lib/currency_cloud/response_handler.rb, line 5
def initialize(verb, route, params, response)
  @verb = verb
  @route = route
  @params = params
  @response = response
end

Public Instance Methods

process() click to toggle source
# File lib/currency_cloud/response_handler.rb, line 12
def process
  return parsed_response if success?
  handle_failure
end

Private Instance Methods

handle_failure() click to toggle source
# File lib/currency_cloud/response_handler.rb, line 23
def handle_failure
  error_class = case response.code
                when 400 then BadRequestError
                when 401 then AuthenticationError
                when 403 then ForbiddenError
                when 404 then NotFoundError
                when 429 then TooManyRequestsError
                when 500 then InternalApplicationError
                end
  raise error_class.new(verb, route, params, response) if error_class
  raise UnexpectedError.new(verb, route, params, response)
end
parsed_response() click to toggle source
# File lib/currency_cloud/response_handler.rb, line 36
def parsed_response
  @parsed_response ||= JSON.parse(response.body)
end
success?() click to toggle source
# File lib/currency_cloud/response_handler.rb, line 19
def success?
  [200, 202].include?(response.code)
end