Sha256: 81e2c759fa45fefabcb884dba36f67a79e189dc874c55680891357b42a72daa3

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

require 'forwardable'

module Poms
  module Api
    # The `Request` object is an implementation-agnostic description of an HTTP
    # request, representing a combination of an HTTP method, URI, body and
    # headers.
    class Request
      attr_reader :method, :uri, :credentials, :body, :headers

      def initialize(
        uri:, method: :get, credentials: nil, body: nil, headers: {}
      )
        @uri = uri
        @method = method.to_sym
        @body = body || ''
        @headers = headers.to_h.freeze
        @credentials = credentials
        validate!
        freeze
      end

      def merge(new_attributes)
        self.class.new(**attributes.merge(new_attributes))
      end

      def attributes
        {
          method:,
          uri:,
          body:,
          headers:,
          credentials:
        }
      end

      def get?
        @method == :get
      end

      def post?
        @method == :post
      end

      private

      def validate!
        raise ArgumentError, 'method should be :get or :post' unless
          %i[get post].include?(@method)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
poms-3.0.0 lib/poms/api/request.rb