Sha256: e58befb52824a9fdce543d7342f22f7cd31a4dda416bfa6747443eec7dcec705

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

require 'pact/matchers'
require 'pact/symbolize_keys'
require 'pact/consumer_contract/headers'

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 = Hash === headers ? Headers.new(headers) : headers # Could be a NullExpectation - TODO make this more elegant
        @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
        hash = {
          method: method,
          path: path,
        }

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

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

      def full_path
        display_path + display_query
      end

      def content_type
        return nil if headers.is_a? self.class.key_not_found.class
        headers['Content-Type']
      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 }.tap do | hash |
          hash[:method] = method.upcase
        end
      end

      def display_path
        path.empty? ? "/" : path
      end

      def display_query
        (query.nil? || query.empty?) ? '' : "?#{Pact::Reification.from_term(query)}"
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pact-1.3.3 lib/pact/shared/request.rb
pact-1.3.2 lib/pact/shared/request.rb
pact-1.3.1 lib/pact/shared/request.rb