class Faraday::Utils::Headers

Adapted from Rack::Utils::HeaderHash

def self.from(value)

def self.from(value)
  new(value)
end

def [](k)

def [](k)
  k = KeyMap[k]
  super(k) || super(@names[k.downcase])
end

def []=(k, v)

def []=(k, v)
  k = KeyMap[k]
  k = (@names[k.downcase] ||= k)
  # join multiple values with a comma
  v = v.to_ary.join(', ') if v.respond_to? :to_ary
  super(k, v)
end

def delete(k)

def delete(k)
  k = KeyMap[k]
  if k = @names[k.downcase]
    @names.delete k.downcase
    super(k)
  end
end

def encode_with(coder)

def encode_with(coder)
  coder['names'] = @names
end

def fetch(k, *args, &block)

def fetch(k, *args, &block)
  k = KeyMap[k]
  key = @names.fetch(k.downcase, k)
  super(key, *args, &block)
end

def include?(k)

def include?(k)
  @names.include? k.downcase
end

def init_with(coder)

def init_with(coder)
  @names = coder['names']
end

def initialize(hash = nil)

def initialize(hash = nil)
  super()
  @names = {}
  self.update(hash || {})
end

def initialize_copy(other)

on dup/clone, we need to duplicate @names hash
def initialize_copy(other)
  super
  @names = other.names.dup
end

def merge(other)

def merge(other)
  hash = dup
  hash.merge! other
end

def merge!(other)

def merge!(other)
  other.each { |k, v| self[k] = v }
  self
end

def names

def names
  @names
end

def parse(header_string)

def parse(header_string)
  return unless header_string && !header_string.empty?
  header_string.split(/\r\n/).
    tap  { |a| a.shift if a.first.index('HTTP/') == 0 }. # drop the HTTP status line
    map  { |h| h.split(/:\s*/, 2) }.reject { |p| p[0].nil? }. # split key and value, ignore blank lines
    each { |key, value|
      # join multiple values with a comma
      if self[key]
        self[key] << ', ' << value
      else
        self[key] = value
      end
    }
end

def replace(other)

def replace(other)
  clear
  @names.clear
  self.update other
  self
end

def to_hash() ::Hash.new.update(self) end

def to_hash() ::Hash.new.update(self) end