Sha256: 2f0936a0c71ba886eac0f0bd0e27420999a7bcb62cf13adbbcdf65bf0495558d

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

module CurrencyCloud
  
  class ResponseHandler
    
    attr_reader :response
    
    def self.process(response)
      new(response).data
    end
    
    def initialize(response)
      @response = response
    end
    
    def data
      if success?
        return parsed_response
      else
        handle_failure
      end
    end
    
    private
    
    def success?
      [200, 202].include?(response.code)
    end
    
    def handle_failure
      error_class = case response.code
        when 400 then CurrencyCloud::BadRequestError
        when 401 then CurrencyCloud::AuthenticationError
        when 403 then CurrencyCloud::ForbiddenError
        when 404 then CurrencyCloud::NotFoundError
        when 429 then CurrencyCloud::TooManyRequestsError
        when 500 then CurrencyCloud::InternalApplicationError
        else CurrencyCloud::UnexpectedError
      end
      raise error_class.new(response)
    end
    
    def parsed_response
      @parsed_response ||= JSON.parse(response.body)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
currency_cloud-0.5 lib/currency_cloud/response_handler.rb