Sha256: b82fbeeca019b9d8c31b16b5fae65c9eae4454c7a6f99902403dbd6f46677e41

Contents?: true

Size: 1016 Bytes

Versions: 70

Compression:

Stored size: 1016 Bytes

Contents

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

module Pact

  class Response < Hash

    include SymbolizeKeys

    ALLOWED_KEYS = [:status, :headers, :body, 'status', 'headers', 'body'].freeze
    private_constant :ALLOWED_KEYS

    def initialize attributes
      merge!(attributes.reject{|key, value| !ALLOWED_KEYS.include?(key)})
    end

    def status
      self[:status]
    end

    def headers
      self[:headers]
    end

    def body
      self[:body]
    end

    def specified? key
      self.key?(key.to_sym)
    end

    def body_allows_any_value?
      body_not_specified? || body_is_empty_hash?
    end

    def [] key
      super key.to_sym
    end

    def self.from_hash hash
      headers = Headers.new(hash[:headers] || hash['headers'] || {})
      new(symbolize_keys(hash).merge(headers: headers))
    end

    private

    def body_is_empty_hash?
      body.is_a?(Hash) && body.empty?
    end

    def body_not_specified?
      !specified?(:body)
    end

  end

end

Version data entries

70 entries across 70 versions & 2 rubygems

Version Path
pact-support-1.21.2 lib/pact/consumer_contract/response.rb
pact-support-1.21.1 lib/pact/consumer_contract/response.rb
pact-support-1.21.0 lib/pact/consumer_contract/response.rb
pact-support-1.20.2 lib/pact/consumer_contract/response.rb
pact-support-1.20.1 lib/pact/consumer_contract/response.rb
pact-support-1.20.0 lib/pact/consumer_contract/response.rb
pact-support-1.19.0 lib/pact/consumer_contract/response.rb
pact-support-1.18.1 lib/pact/consumer_contract/response.rb
pact-support-1.18.0 lib/pact/consumer_contract/response.rb
hs-pact-support-1.17.1 lib/pact/consumer_contract/response.rb
pact-support-1.17.0 lib/pact/consumer_contract/response.rb
pact-support-1.16.10 lib/pact/consumer_contract/response.rb
pact-support-1.16.9 lib/pact/consumer_contract/response.rb
pact-support-1.16.8 lib/pact/consumer_contract/response.rb
pact-support-1.16.7 lib/pact/consumer_contract/response.rb
pact-support-1.16.6 lib/pact/consumer_contract/response.rb
pact-support-1.16.5 lib/pact/consumer_contract/response.rb
pact-support-1.16.4 lib/pact/consumer_contract/response.rb
pact-support-1.16.3 lib/pact/consumer_contract/response.rb
pact-support-1.16.2 lib/pact/consumer_contract/response.rb