class Faraday::Utils::Headers

A case-insensitive Hash that preserves the original case of a header when set.

Adapted from Rack::Utils::HeaderHash

Constants

KeyMap

symbol -> string mapper + cache

Attributes

names[R]

Public Class Methods

allocate() click to toggle source
Calls superclass method
# File lib/faraday/utils/headers.rb, line 14
def self.allocate
  new_self = super
  new_self.initialize_names
  new_self
end
from(value) click to toggle source
# File lib/faraday/utils/headers.rb, line 10
def self.from(value)
  new(value)
end
new(hash = nil) click to toggle source
Calls superclass method
# File lib/faraday/utils/headers.rb, line 20
def initialize(hash = nil)
  super()
  @names = {}
  update(hash || {})
end

Public Instance Methods

[](key) click to toggle source
Calls superclass method
# File lib/faraday/utils/headers.rb, line 52
def [](key)
  key = KeyMap[key]
  super(key) || super(@names[key.downcase])
end
[]=(key, val) click to toggle source
Calls superclass method
# File lib/faraday/utils/headers.rb, line 57
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
delete(key) click to toggle source
Calls superclass method
# File lib/faraday/utils/headers.rb, line 71
def delete(key)
  key = KeyMap[key]
  key = @names[key.downcase]
  return unless key

  @names.delete key.downcase
  super(key)
end
fetch(key, *args, &block) click to toggle source
Calls superclass method
# File lib/faraday/utils/headers.rb, line 65
def fetch(key, *args, &block)
  key = KeyMap[key]
  key = @names.fetch(key.downcase, key)
  super(key, *args, &block)
end
has_key?(key)
Alias for: include?
include?(key) click to toggle source
# File lib/faraday/utils/headers.rb, line 80
def include?(key)
  @names.include? key.downcase
end
Also aliased as: has_key?, member?, key?
initialize_copy(other) click to toggle source

on dup/clone, we need to duplicate @names hash

Calls superclass method
# File lib/faraday/utils/headers.rb, line 31
def initialize_copy(other)
  super
  @names = other.names.dup
end
initialize_names() click to toggle source
# File lib/faraday/utils/headers.rb, line 26
def initialize_names
  @names = {}
end
key?(key)
Alias for: include?
member?(key)
Alias for: include?
merge(other) click to toggle source
# File lib/faraday/utils/headers.rb, line 95
def merge(other)
  hash = dup
  hash.merge! other
end
merge!(other) click to toggle source
# File lib/faraday/utils/headers.rb, line 88
def merge!(other)
  other.each { |k, v| self[k] = v }
  self
end
Also aliased as: update
parse(header_string) click to toggle source
# File lib/faraday/utils/headers.rb, line 111
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
replace(other) click to toggle source
# File lib/faraday/utils/headers.rb, line 100
def replace(other)
  clear
  @names.clear
  update other
  self
end
to_hash() click to toggle source
# File lib/faraday/utils/headers.rb, line 107
def to_hash
  {}.update(self)
end
update(other)
Alias for: merge!

Private Instance Methods

add_parsed(key, value) click to toggle source

Join multiple values with a comma.

# File lib/faraday/utils/headers.rb, line 134
def add_parsed(key, value)
  if key?(key)
    self[key] = self[key].to_s
    self[key] << ', ' << value
  else
    self[key] = value
  end
end