Sha256: 161391b61918f79447e722f2a40ccf0ec234cb472850e9e1b3ba298844f41a4e

Contents?: true

Size: 1.2 KB

Versions: 113

Compression:

Stored size: 1.2 KB

Contents

module Pact

  class DuplicateHeaderError < StandardError; end
  class InvalidHeaderNameTypeError < StandardError; end

  class Headers < Hash

    def initialize hash = {}
      hash.each_pair do | key, value |
        check_for_invalid key
        self[find_matching_key(key)] = value
      end
      self.freeze
    end

    def [] key
      super(find_matching_key(key))
    end

    def fetch *args, &block
      args[0] = find_matching_key(args[0]) if args.first
      super(*args, &block)
    end

    def key? key
      super(find_matching_key(key))
    end

    alias_method :has_key?, :key?
    alias_method :include?, :key?

    private

    def find_matching_key key
      key = key.to_s
      match = keys.find { |k| k.downcase == key.downcase }
      match.nil? ? key : match
    end

    def check_for_invalid key
      unless (String === key || Symbol === key)
        raise InvalidHeaderNameTypeError.new "Header name (#{key}) must be a String or a Symbol."
      end
      if key? key
        raise DuplicateHeaderError.new "Duplicate header found (#{find_matching_key(key)} and #{key}. Please use a comma separated single value when multiple headers with the same name are required."
      end
    end

  end

end

Version data entries

113 entries across 113 versions & 3 rubygems

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