class Faraday::Utils::Headers

Experimental RBS support (using type sampling data from the type_fusion project).

# sig/faraday/utils/headers.rbs

class Faraday::Utils::Headers < Hash
  def []=: (String key, String val) -> untyped
end

Adapted from Rack::Utils::HeaderHash
when set.
A case-insensitive Hash that preserves the original case of a header

def self.allocate

def self.allocate
  new_self = super
  new_self.initialize_names
  new_self
end

def self.from(value)

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

def [](key)

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

def []=(key, val)

Experimental RBS support (using type sampling data from the type_fusion project).

def []=: (String key, String val) -> untyped

This signature was generated using 1 sample from 1 application.

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

def add_parsed(key, value)

Join multiple values with a comma.
def add_parsed(key, value)
  if key?(key)
    self[key] = self[key].to_s
    self[key] << ', ' << value
  else
    self[key] = value
  end
end

def delete(key)

def delete(key)
  key = KeyMap[key]
  key = @names[key.downcase]
  return unless key
  @names.delete key.downcase
  super(key)
end

def fetch(key, *args, &block)

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

def include?(key)

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

def initialize(hash = nil)

def initialize(hash = nil)
  super()
  @names = {}
  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 initialize_names

def initialize_names
  @names = {}
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 parse(header_string)

def parse(header_string)
  return unless header_string && !header_string.empty?
  headers = header_string.split("\r\n")
  # Find the last set of response headers.
  start_index = headers.rindex { |x| x.start_with?('HTTP/') } || 0
  last_response = headers.slice(start_index, headers.size)
  last_response
    .tap { |a| a.shift if a.first.start_with?('HTTP/') }
    .map { |h| h.split(/:\s*/, 2) } # split key and value
    .reject { |p| p[0].nil? } # ignore blank lines
    .each { |key, value| add_parsed(key, value) }
end

def replace(other)

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

def to_hash

def to_hash
  {}.update(self)
end