Class: HTTP::Headers
- Inherits:
-
Object
- Object
- HTTP::Headers
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/http/headers.rb,
lib/http/headers/mixin.rb
Overview
HTTP Headers container.
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]*)*$/
- HEADER_NAME_RE =
Matches valid header field name according to RFC.
/^[A-Za-z0-9!#\$%&'*+\-.^_`|~]+$/
- SET_COOKIE =
Set-Cookie (response) header name
"Set-Cookie".freeze
- COOKIE =
Cookie (request) header name
"Cookie".freeze
Class Method Summary collapse
-
.coerce(object) ⇒ Headers
(also: [])
Coerces given
object
into Headers.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares headers to another Headers or Array of key/value pairs.
-
#[](name) ⇒ nil, ...
Smart version of #get.
-
#add(name, value) ⇒ void
(also: #append)
Appends header.
-
#delete(name) ⇒ void
Removes header.
-
#each ⇒ Enumerator, Headers
Calls the given block once for each key/value pair in headers container.
-
#empty? ⇒ Boolean
Returns
true
ifself
has no key/value pairs. -
#get(name) ⇒ Array<String>
Returns list of header values if any.
-
#hash ⇒ Fixnum
Compute a hash-code for this headers container.
-
#initialize ⇒ Headers
constructor
Class constructor.
-
#initialize_copy(orig) ⇒ Object
private
Properly clones internal key/value storage.
-
#inspect ⇒ String
Returns human-readable representation of
self
instance. -
#keys ⇒ Array<String>
Returns list of header names.
-
#merge(other) ⇒ Headers
Returns new instance with
other
headers merged in. -
#merge!(other) ⇒ void
Merges
other
headers intoself
. -
#set(name, value) ⇒ void
(also: #[]=)
Sets header.
-
#to_a ⇒ Array<[String, String]>
Returns headers key/value pairs.
-
#to_h ⇒ Hash
(also: #to_hash)
Returns Rack-compatible headers Hash.
Constructor Details
#initialize ⇒ Headers
Class constructor.
26 27 28 |
# File 'lib/http/headers.rb', line 26 def initialize @pile = [] end |
Class Method Details
.coerce(object) ⇒ Headers Also known as: []
Coerces given object
into Headers.
176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/http/headers.rb', line 176 def 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
117 118 119 120 |
# File 'lib/http/headers.rb', line 117 def ==(other) return false unless other.respond_to? :to_a @pile == other.to_a end |
#[](name) ⇒ nil, ...
Smart version of #get.
75 76 77 78 79 80 81 82 83 |
# File 'lib/http/headers.rb', line 75 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.
Appends header.
54 55 56 57 |
# File 'lib/http/headers.rb', line 54 def add(name, value) name = normalize_header name.to_s Array(value).each { |v| @pile << [name, v.to_s] } end |
#delete(name) ⇒ void
This method returns an undefined value.
Removes header.
44 45 46 47 |
# File 'lib/http/headers.rb', line 44 def delete(name) name = normalize_header name.to_s @pile.delete_if { |k, _| k == name } end |
#each ⇒ Enumerator, Headers
Calls the given block once for each key/value pair in headers container.
126 127 128 129 130 |
# File 'lib/http/headers.rb', line 126 def each return to_enum(__method__) unless block_given? @pile.each { |arr| yield(arr) } self end |
#empty? ⇒ Boolean
Returns true
if self
has no key/value pairs
136 |
# File 'lib/http/headers.rb', line 136 def_delegator :@pile, :empty? |
#get(name) ⇒ Array<String>
Returns list of header values if any.
65 66 67 68 |
# File 'lib/http/headers.rb', line 65 def get(name) name = normalize_header name.to_s @pile.select { |k, _| k == name }.map { |_, v| v } end |
#hash ⇒ Fixnum
Compute a hash-code for this headers container. Two conatiners with the same content will have the same hash code.
144 |
# File 'lib/http/headers.rb', line 144 def_delegator :@pile, :hash |
#initialize_copy(orig) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Properly clones internal key/value storage.
149 150 151 152 |
# File 'lib/http/headers.rb', line 149 def initialize_copy(orig) super @pile = to_a end |
#inspect ⇒ String
Returns human-readable representation of self
instance.
103 104 105 |
# File 'lib/http/headers.rb', line 103 def inspect "#<#{self.class} #{to_h.inspect}>" end |
#keys ⇒ Array<String>
Returns list of header names.
110 111 112 |
# File 'lib/http/headers.rb', line 110 def keys @pile.map { |k, _| k }.uniq end |
#merge(other) ⇒ Headers
Returns new instance with other
headers merged in.
166 167 168 |
# File 'lib/http/headers.rb', line 166 def merge(other) dup.tap { |dupped| dupped.merge! other } end |
#merge!(other) ⇒ void
This method returns an undefined value.
Merges other
headers into self
.
158 159 160 |
# File 'lib/http/headers.rb', line 158 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.
34 35 36 37 |
# File 'lib/http/headers.rb', line 34 def set(name, value) delete(name) add(name, value) end |
#to_a ⇒ Array<[String, String]>
Returns headers key/value pairs.
96 97 98 |
# File 'lib/http/headers.rb', line 96 def to_a @pile.map { |pair| pair.map(&:dup) } end |
#to_h ⇒ Hash Also known as: to_hash
Returns Rack-compatible headers Hash
88 89 90 |
# File 'lib/http/headers.rb', line 88 def to_h Hash[keys.map { |k| [k, self[k]] }] end |