Class: HTTPI::Utils::Headers
- Inherits:
-
Hash
- Object
- Hash
- HTTPI::Utils::Headers
- Defined in:
- lib/httpi/utils.rb
Overview
A case-insensitive Hash that preserves the original case of a header when set.
Class Method Summary collapse
Instance Method Summary collapse
- #[](k) ⇒ Object
- #[]=(k, v) ⇒ Object
-
#clear ⇒ Object
on clear, we need to clear @names hash.
- #delete(k) ⇒ Object
- #each ⇒ Object
- #include?(k) ⇒ Boolean (also: #has_key?, #member?, #key?)
-
#initialize(hash = {}) ⇒ Headers
constructor
A new instance of Headers.
-
#initialize_copy(other) ⇒ Object
on dup/clone, we need to duplicate @names hash.
- #merge(other) ⇒ Object
- #merge!(other) ⇒ Object
- #replace(other) ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(hash = {}) ⇒ Headers
Returns a new instance of Headers.
18 19 20 21 22 |
# File 'lib/httpi/utils.rb', line 18 def initialize(hash = {}) super() @names = {} hash.each { |k, v| self[k] = v } end |
Class Method Details
.[](headers) ⇒ Object
10 11 12 13 14 15 16 |
# 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 |
Instance Method Details
#[](k) ⇒ Object
48 49 50 |
# File 'lib/httpi/utils.rb', line 48 def [](k) super(k) || super(@names[k.downcase]) end |
#[]=(k, v) ⇒ Object
52 53 54 55 56 57 |
# 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 ⇒ Object
on clear, we need to clear @names hash
31 32 33 34 |
# File 'lib/httpi/utils.rb', line 31 def clear super @names.clear end |
#delete(k) ⇒ Object
59 60 61 62 63 |
# File 'lib/httpi/utils.rb', line 59 def delete(k) canonical = k.downcase result = super @names.delete(canonical) result end |
#each ⇒ Object
36 37 38 39 40 |
# 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 |
#include?(k) ⇒ Boolean Also known as: has_key?, member?, key?
65 66 67 |
# File 'lib/httpi/utils.rb', line 65 def include?(k) super || @names.include?(k.downcase) end |
#initialize_copy(other) ⇒ Object
on dup/clone, we need to duplicate @names hash
25 26 27 28 |
# File 'lib/httpi/utils.rb', line 25 def initialize_copy(other) super @names = other.names.dup end |
#merge(other) ⇒ Object
78 79 80 81 |
# File 'lib/httpi/utils.rb', line 78 def merge(other) hash = dup hash.merge! other end |
#merge!(other) ⇒ Object
73 74 75 76 |
# File 'lib/httpi/utils.rb', line 73 def merge!(other) other.each { |k, v| self[k] = v } self end |
#replace(other) ⇒ Object
83 84 85 86 87 |
# File 'lib/httpi/utils.rb', line 83 def replace(other) clear other.each { |k, v| self[k] = v } self end |
#to_hash ⇒ Object
42 43 44 45 46 |
# File 'lib/httpi/utils.rb', line 42 def to_hash hash = {} each { |k, v| hash[k] = v } hash end |