Sha256: 1bf7dbfe9f883d50df58f4dc5357eb2adbf95f25e302eee9eed015e235f894de

Contents?: true

Size: 1.57 KB

Versions: 24

Compression:

Stored size: 1.57 KB

Contents

require 'pact/matchers'
require 'pact/symbolize_keys'

module Pact

  module Request

    class Base
      include Pact::Matchers
      include Pact::SymbolizeKeys
      extend Pact::Matchers

      attr_reader :method, :path, :headers, :body, :query, :options

      def initialize(method, path, headers, body, query)
        @method = method.to_s
        @path = path.chomp('/')
        @headers = headers
        @body = body
        @query = query
      end

      def to_json(options = {})
        as_json.to_json(options)
      end

      def as_json options = {}
        to_hash
      end

      def to_hash
        base_json = {
          method: method,
          path: path,
        }

        base_json.merge!(body: body) unless body.is_a? self.class.key_not_found.class
        base_json.merge!(headers: headers) unless headers.is_a? self.class.key_not_found.class
        base_json.merge!(query: query) unless query.is_a? self.class.key_not_found.class
        base_json
      end

      def method_and_path
        "#{method.upcase} #{path}"
      end

      def full_path
        fp = ''
        if path.empty?
          fp << "/"
        else
          fp << path
        end
        if query && !query.empty?
          fp << ("?" + (query.kind_of?(Pact::Term) ? query.generate : query))
        end
        fp
      end

      protected

      def self.key_not_found
        raise NotImplementedError
      end

      def to_hash_without_body
        keep_keys = [:method, :path, :headers, :query]
        as_json.reject{ |key, value| !keep_keys.include? key }
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
pact-1.0.21 lib/pact/shared/request.rb
pact-1.0.20 lib/pact/shared/request.rb
pact-1.0.19 lib/pact/shared/request.rb
pact-1.0.18 lib/pact/shared/request.rb