Sha256: 2b525e10017d096b6d6f9e38f22f0f6f61dc2efd2bdf89b4d479ef12d0189a8e

Contents?: true

Size: 1.13 KB

Versions: 6

Compression:

Stored size: 1.13 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: method,
          uri: uri,
          body: body,
          headers: headers,
          credentials: 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

6 entries across 6 versions & 1 rubygems

Version Path
poms-2.6.0 lib/poms/api/request.rb
poms-2.5.1 lib/poms/api/request.rb
poms-2.5.0 lib/poms/api/request.rb
poms-2.4.0 lib/poms/api/request.rb
poms-2.3.1 lib/poms/api/request.rb
poms-2.3.0 lib/poms/api/request.rb