Sha256: a4a7b10154b8cb5f461ce1c6cf36ef2fbb58bf8186f21c3cb2ffc2e603934f75

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

# frozen_literal_string: true

module SeaShanty
  class Response
    attr_reader :body, :headers, :message, :status, :original_response

    class << self
      def from_h(hash)
        new(
          status: hash.fetch(:status).fetch(:code),
          message: hash.fetch(:status).fetch(:message),
          headers: hash.fetch(:headers),
          body: hash.fetch(:body).fetch(:encoding).empty? ? nil : hash.fetch(:body).fetch(:string)
        )
      end
    end

    def initialize(status:, message:, headers:, body:, original_response: ORIGINAL_RESPONSE_NOT_PRESENT)
      @status = Integer(status, status.is_a?(String) ? 10 : 0)
      @message = message
      @headers = headers
      @body = body
      @original_response = original_response
    end

    def to_h
      {
        status: {
          code: status,
          message: message
        },
        headers: headers,
        body: {
          string: body.to_s,
          encoding: body.nil? ? "" : body.encoding.name
        }
      }
    end

    def was_stored?
      ORIGINAL_RESPONSE_NOT_PRESENT != original_response
    end

    def ==(other)
      self.class === other &&
        status == other.status && message == other.message && headers == other.headers &&
        body == other.body
    end

    alias_method :eql?, :==

    def hash
      [self.class, status, message, headers, body].hash
    end

    private

    ORIGINAL_RESPONSE_NOT_PRESENT = :original_response_not_present
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sea_shanty-0.2.0 lib/sea_shanty/response.rb
sea_shanty-0.1.1 lib/sea_shanty/response.rb
sea_shanty-0.1.0 lib/sea_shanty/response.rb