Sha256: 6c585c0e58bc6df82d6045ba0e863ae3a5d9c2adc8cc510daaf06acbe781eea3

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module FriendlyShipping
  # Represents an HTTP response received from a carrier API.
  class Response
    # @return [Integer] the HTTP response status code
    attr_reader :status

    # @return [String] the HTTP response body
    attr_reader :body

    # @return [Hash] the HTTP response headers
    attr_reader :headers

    # @param status [Integer] the HTTP response status code
    # @param body [String] the HTTP response body
    # @param headers [Hash] the HTTP response headers
    def initialize(status:, body:, headers:)
      @status = status
      @body = body
      @headers = headers || {}
    end

    alias_method :code, :status

    # Constructs a new {Response} from a `RestClient::Response` object.
    # @param response [RestClient::Response] the response to use
    # @return [Response]
    def self.new_from_rest_client_response(response)
      new(status: response&.code, body: response&.body, headers: response&.headers)
    end

    # Returns true if the given object shares the same class and attributes with this response.
    # @param [Object] other
    # @return [Boolean]
    def ==(other)
      other.class == self.class &&
        other.attributes == attributes
    end

    alias_method :eql?, :==

    # Returns this response's attributes as a hash.
    # @return [Hash]
    def hash
      attributes.hash
    end

    protected

    # Returns the status, body, and headers from this response.
    # @return [Array]
    def attributes
      [status, body, headers]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
friendly_shipping-0.9.0 lib/friendly_shipping/response.rb