Class: Webbed::Headers
- Inherits:
-
Hash
- Object
- Hash
- Webbed::Headers
- Defined in:
- lib/webbed/headers.rb
Overview
Representation of HTTP headers.
This class is stolen directly from Rack.
Class Method Summary collapse
Instance Method Summary collapse
- #[](k)
- #[]=(k, v)
- #delete(k)
- #each
- #include?(k) ⇒ Boolean (also: #has_key?, #member?, #key?)
-
#initialize(hash = {}) ⇒ Headers
constructor
A new instance of Headers.
- #merge(other)
- #merge!(other)
- #replace(other)
- #to_hash
- #to_s
Constructor Details
#initialize(hash = {}) ⇒ Headers
Returns a new instance of Headers.
10 11 12 13 14 |
# File 'lib/webbed/headers.rb', line 10 def initialize(hash={}) super() @names = {} hash.each { |k, v| self[k] = v } end |
Class Method Details
.new(hash = {})
6 7 8 |
# File 'lib/webbed/headers.rb', line 6 def self.new(hash={}) Headers === hash ? hash : super(hash) end |
Instance Method Details
#[](k)
33 34 35 36 |
# File 'lib/webbed/headers.rb', line 33 def [](k) super(@names[k]) if @names[k] super(@names[k.downcase]) end |
#[]=(k, v)
38 39 40 41 42 |
# File 'lib/webbed/headers.rb', line 38 def []=(k, v) delete k @names[k] = @names[k.downcase] = k super k, v end |
#delete(k)
44 45 46 47 48 49 |
# File 'lib/webbed/headers.rb', line 44 def delete(k) canonical = k.downcase result = super @names.delete(canonical) @names.delete_if { |name,| name.downcase == canonical } result end |
#each
16 17 18 19 20 |
# File 'lib/webbed/headers.rb', line 16 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?
51 52 53 |
# File 'lib/webbed/headers.rb', line 51 def include?(k) @names.include?(k) || @names.include?(k.downcase) end |
#merge(other)
64 65 66 67 |
# File 'lib/webbed/headers.rb', line 64 def merge(other) hash = dup hash.merge! other end |
#merge!(other)
59 60 61 62 |
# File 'lib/webbed/headers.rb', line 59 def merge!(other) other.each { |k, v| self[k] = v } self end |
#replace(other)
69 70 71 72 73 |
# File 'lib/webbed/headers.rb', line 69 def replace(other) clear other.each { |k, v| self[k] = v } self end |
#to_hash
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/webbed/headers.rb', line 22 def to_hash inject({}) do |hash, (k,v)| if v.respond_to? :to_ary hash[k] = v.to_ary.join("\n") else hash[k] = v end hash end end |
#to_s
75 76 77 78 79 |
# File 'lib/webbed/headers.rb', line 75 def to_s map do |field_name, field_content| "#{field_name}: #{field_content}\r\n" end.join end |