Class: HTTP::Headers
- Inherits:
-
Object
- Object
- HTTP::Headers
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/http/headers.rb,
lib/http/headers/mixin.rb
Defined Under Namespace
Modules: Mixin
Constant Summary collapse
- CANONICAL_HEADER =
Matches HTTP header names when in “Canonical-Http-Format”
/^[A-Z][a-z]*(-[A-Z][a-z]*)*$/
Class Method Summary collapse
-
.coerce(object) ⇒ Headers
Initiates new Headers object from given object.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares headers to another Headers or Array of key/value pairs.
-
#[](name) ⇒ NilClass, ...
Smart version of #get.
-
#add(name, value) ⇒ void
(also: #append)
Append header.
-
#delete(name) ⇒ void
Removes header.
-
#get(name) ⇒ Array
Return array of header values if any.
-
#initialize ⇒ Headers
constructor
:nodoc:.
-
#initialize_copy(orig) ⇒ Object
:nodoc:.
-
#inspect ⇒ Object
:nodoc:.
-
#keys ⇒ Array<String>
List of header names.
-
#merge(other) ⇒ Headers
Returns new Headers instance with ‘other` headers merged in.
-
#merge!(other) ⇒ void
Merge in ‘other` headers.
-
#set(name, value) ⇒ void
(also: #[]=)
Sets header.
-
#to_a ⇒ Array<[String, String]>
Array of key/value pairs.
-
#to_h ⇒ Hash
Converts headers into a Rack-compatible Hash.
Constructor Details
#initialize ⇒ Headers
:nodoc:
14 15 16 |
# File 'lib/http/headers.rb', line 14 def initialize @pile = [] end |
Class Method Details
.coerce(object) ⇒ Headers
Initiates new Headers object from given object.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/http/headers.rb', line 130 def self.coerce(object) unless object.is_a? self object = case when object.respond_to?(:to_hash) then object.to_hash when object.respond_to?(:to_h) then object.to_h when object.respond_to?(:to_a) then object.to_a else fail Error, "Can't coerce #{object.inspect} to Headers" end end headers = new object.each { |k, v| headers.add k, v } headers end |
Instance Method Details
#==(other) ⇒ Boolean
Compares headers to another Headers or Array of key/value pairs
96 97 98 99 |
# File 'lib/http/headers.rb', line 96 def ==(other) return false unless other.respond_to? :to_a @pile == other.to_a end |
#[](name) ⇒ NilClass, ...
Smart version of #get
57 58 59 60 61 62 63 64 65 |
# File 'lib/http/headers.rb', line 57 def [](name) values = get(name) case values.count when 0 then nil when 1 then values.first else values end end |
#add(name, value) ⇒ void Also known as: append
This method returns an undefined value.
Append header
38 39 40 41 |
# File 'lib/http/headers.rb', line 38 def add(name, value) name = canonicalize_header name.to_s Array(value).each { |v| @pile << [name, v] } end |
#delete(name) ⇒ void
This method returns an undefined value.
Removes header
30 31 32 33 |
# File 'lib/http/headers.rb', line 30 def delete(name) name = canonicalize_header name.to_s @pile.delete_if { |k, _| k == name } end |
#get(name) ⇒ Array
Return array of header values if any.
47 48 49 50 |
# File 'lib/http/headers.rb', line 47 def get(name) name = canonicalize_header name.to_s @pile.select { |k, _| k == name }.map { |_, v| v } end |
#initialize_copy(orig) ⇒ Object
:nodoc:
104 105 106 107 |
# File 'lib/http/headers.rb', line 104 def initialize_copy(orig) super @pile = to_a end |
#inspect ⇒ Object
:nodoc:
82 83 84 |
# File 'lib/http/headers.rb', line 82 def inspect "#<#{self.class} #{to_h.inspect}>" end |
#keys ⇒ Array<String>
List of header names
89 90 91 |
# File 'lib/http/headers.rb', line 89 def keys @pile.map { |k, _| k }.uniq end |
#merge(other) ⇒ Headers
Returns new Headers instance with ‘other` headers merged in.
121 122 123 |
# File 'lib/http/headers.rb', line 121 def merge(other) dup.tap { |dupped| dupped.merge! other } end |
#merge!(other) ⇒ void
This method returns an undefined value.
Merge in ‘other` headers
113 114 115 |
# File 'lib/http/headers.rb', line 113 def merge!(other) self.class.coerce(other).to_h.each { |name, values| set name, values } end |
#set(name, value) ⇒ void Also known as: []=
This method returns an undefined value.
Sets header
21 22 23 24 |
# File 'lib/http/headers.rb', line 21 def set(name, value) delete(name) add(name, value) end |
#to_a ⇒ Array<[String, String]>
Array of key/value pairs
77 78 79 |
# File 'lib/http/headers.rb', line 77 def to_a @pile.map { |pair| pair.map(&:dup) } end |
#to_h ⇒ Hash
Converts headers into a Rack-compatible Hash
70 71 72 |
# File 'lib/http/headers.rb', line 70 def to_h Hash[keys.map { |k| [k, self[k]] }] end |