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.3.1 lib/pact/consumer_contract/headers.rb
pact-support-1.3.0 lib/pact/consumer_contract/headers.rb
pact-support-1.2.99.alpha.1 lib/pact/consumer_contract/headers.rb
pact-support-1.3.0.alpha.2 lib/pact/consumer_contract/headers.rb
pact-support-1.3.0.alpha.1 lib/pact/consumer_contract/headers.rb
pact-support-1.2.5 lib/pact/consumer_contract/headers.rb
pact-support-1.2.4 lib/pact/consumer_contract/headers.rb
pact-support-1.2.3 lib/pact/consumer_contract/headers.rb
pact-support-1.2.2 lib/pact/consumer_contract/headers.rb
pact-support-1.2.1 lib/pact/consumer_contract/headers.rb
pact-support-1.2.0 lib/pact/consumer_contract/headers.rb
pact-support-1.1.8 lib/pact/consumer_contract/headers.rb
pact-support-1.1.7 lib/pact/consumer_contract/headers.rb
pact-support-1.1.6 lib/pact/consumer_contract/headers.rb
pact-support-1.1.5 lib/pact/consumer_contract/headers.rb
pact-support-1.1.4 lib/pact/consumer_contract/headers.rb
pact-support-1.1.3 lib/pact/consumer_contract/headers.rb
pact-support-1.1.2 lib/pact/consumer_contract/headers.rb
pact-support-1.1.1 lib/pact/consumer_contract/headers.rb
pact-support-1.1.0 lib/pact/consumer_contract/headers.rb