Sha256: fa6daa79634d7c27e85d8c6a901076be7643166f7b070b6b7fb6e204bf8c668f

Contents?: true

Size: 1.97 KB

Versions: 2

Compression:

Stored size: 1.97 KB

Contents

require_relative 'exceptions'
require_relative 'errors'

module SmartyStreets
  class StatusCodeSender
    def initialize(inner)
      @inner = inner
    end

    def send(request)
      response = @inner.send(request)

      if response.status_code == '429'
        response.error = parse_rate_limit_response(response)
      end
      assign_exception(response) if response.error == nil

      response
    end

    def parse_rate_limit_response(response)
      error_message = ""
      if !response.payload.nil?
        response_json = JSON.parse(response.payload)
        response_json["errors"].each do |error|
          error_message += (" " + error["message"])
        end
        error_message.strip!
      end
      if error_message == ""
        error_message = TOO_MANY_REQUESTS
      end
      TooManyRequestsError.new(error_message)
    end

    def assign_exception(response)
      response.error = case response.status_code
                         when '304'          
                           NotModifiedInfo.new(NOT_MODIFIED)
                         when '401'
                           BadCredentialsError.new(BAD_CREDENTIALS)
                         when '402'
                           PaymentRequiredError.new(PAYMENT_REQUIRED)
                         when '413'
                           RequestEntityTooLargeError.new(REQUEST_ENTITY_TOO_LARGE)
                         when '400'
                           BadRequestError.new(BAD_REQUEST)
                         when '422'
                           UnprocessableEntityError.new(UNPROCESSABLE_ENTITY)
                         when '429'
                           TooManyRequestsError.new(TOO_MANY_REQUESTS)
                         when '500'
                           InternalServerError.new(INTERNAL_SERVER_ERROR)
                         when '503'
                           ServiceUnavailableError.new(SERVICE_UNAVAILABLE)
                         else
                           nil
                       end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
smartystreets_ruby_sdk-5.20.6 lib/smartystreets_ruby_sdk/status_code_sender.rb
smartystreets_ruby_sdk-5.20.4 lib/smartystreets_ruby_sdk/status_code_sender.rb