class HTTPI::Utils::Headers

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

Public Class Methods

[](headers) click to toggle source
# File lib/httpi/utils.rb, line 10
def self.[](headers)
  if headers.is_a?(Headers) && !headers.frozen?
    return headers
  else
    return self.new(headers)
  end
end
new(hash = {}) click to toggle source
Calls superclass method
# File lib/httpi/utils.rb, line 18
def initialize(hash = {})
  super()
  @names = {}
  hash.each { |k, v| self[k] = v }
end

Public Instance Methods

[](k) click to toggle source
Calls superclass method
# File lib/httpi/utils.rb, line 48
def [](k)
  super(k) || super(@names[k.downcase])
end
[]=(k, v) click to toggle source
Calls superclass method
# File lib/httpi/utils.rb, line 52
def []=(k, v)
  canonical = k.downcase.freeze
  delete k if @names[canonical] && @names[canonical] != k # .delete is expensive, don't invoke it unless necessary
  @names[canonical] = k
  super k, v
end
clear() click to toggle source

on clear, we need to clear @names hash

Calls superclass method
# File lib/httpi/utils.rb, line 31
def clear
  super
  @names.clear
end
delete(k) click to toggle source
Calls superclass method
# File lib/httpi/utils.rb, line 59
def delete(k)
  canonical = k.downcase
  result = super @names.delete(canonical)
  result
end
each() { |k, respond_to?(:to_ary) ? to_ary.join("\n") : v| ... } click to toggle source
Calls superclass method
# File lib/httpi/utils.rb, line 36
def each
  super do |k, v|
    yield(k, v.respond_to?(:to_ary) ? v.to_ary.join("\n") : v)
  end
end
has_key?(k)
Alias for: include?
include?(k) click to toggle source
Calls superclass method
# File lib/httpi/utils.rb, line 65
def include?(k)
  super || @names.include?(k.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/httpi/utils.rb, line 25
def initialize_copy(other)
  super
  @names = other.names.dup
end
key?(k)
Alias for: include?
member?(k)
Alias for: include?
merge(other) click to toggle source
# File lib/httpi/utils.rb, line 78
def merge(other)
  hash = dup
  hash.merge! other
end
merge!(other) click to toggle source
# File lib/httpi/utils.rb, line 73
def merge!(other)
  other.each { |k, v| self[k] = v }
  self
end
replace(other) click to toggle source
# File lib/httpi/utils.rb, line 83
def replace(other)
  clear
  other.each { |k, v| self[k] = v }
  self
end
to_hash() click to toggle source
# File lib/httpi/utils.rb, line 42
def to_hash
  hash = {}
  each { |k, v| hash[k] = v }
  hash
end

Protected Instance Methods

names() click to toggle source
# File lib/httpi/utils.rb, line 90
def names
  @names
end