Sha256: 8edda04b614e5ae8b2c95d55630b68597d61c46ba0987c81478594caf010ec85

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

module Duracloud
  class ResponseHandler

    def self.call(response)
      new(response).call
    end

    attr_reader :response

    def initialize(response)
      @response = response
    end

    def call
      handle_error
      log_response
    end

    def log_response
      if loggable_response_body?
        Duracloud.logger.info(response.body)
      end
    end

    def loggable_response_body?
      %w(POST PUT DELETE).include?(response.request_method) &&
        response.plain_text? &&
        response.has_body?
    end

    def handle_error
      if response.error?
        raise exception, error_message
      end
    end

    def error_message
      if response.plain_text? && response.has_body?
        response.body
      else
        [ response.status, response.reason ].join(' ')
      end
    end

    def exception
      case response.status
      when 400
        BadRequestError
      when 404
        NotFoundError
      when 409
        ConflictError
      else
        if response.status >= 500
          ServerError
        else
          Error
        end
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
duracloud-client-0.10.2 lib/duracloud/response_handler.rb
duracloud-client-0.10.1 lib/duracloud/response_handler.rb
duracloud-client-0.10.0 lib/duracloud/response_handler.rb