Class: HTTPX::Headers
- Inherits:
-
Object
- Object
- HTTPX::Headers
- Defined in:
- lib/httpx/headers.rb
Constant Summary collapse
- EMPTY =
:nodoc:
[].freeze
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#[](field) ⇒ Object
returns the comma-separated values of the header field identified by
field
, or nil otherwise. -
#[]=(field, value) ⇒ Object
sets
value
(if not nil) as single value for thefield
header. -
#add(field, value) ⇒ Object
(also: #add_header)
adds additional
value
to the existing, for headerfield
. -
#delete(field) ⇒ Object
deletes all values associated with
field
header. -
#each ⇒ Object
returns the enumerable headers store in pairs of header field + the values in the comma-separated string format.
-
#freeze ⇒ Object
freezes the headers hash.
-
#get(field) ⇒ Object
returns the values for the
field
header in array format. -
#initialize(headers = nil) ⇒ Headers
constructor
A new instance of Headers.
-
#initialize_clone(orig) ⇒ Object
cloned initialization.
-
#initialize_dup(orig) ⇒ Object
dupped initialization.
-
#key?(downcased_key) ⇒ Boolean
this is internal API and doesn’t abide to other public API guarantees, like downcasing strings.
-
#merge(other) ⇒ Object
merges headers with another header-quack.
- #same_headers?(headers) ⇒ Boolean
-
#to_a ⇒ Object
the headers store in array of pairs format.
-
#to_hash ⇒ Object
the headers store in Hash format.
-
#to_s ⇒ Object
headers as string.
Constructor Details
#initialize(headers = nil) ⇒ Headers
Returns a new instance of Headers.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/httpx/headers.rb', line 15 def initialize(headers = nil) @headers = {} return unless headers headers.each do |field, value| array_value(value).each do |v| add(downcased(field), v) end end end |
Class Method Details
.new(headers = nil) ⇒ Object
8 9 10 11 12 |
# File 'lib/httpx/headers.rb', line 8 def new(headers = nil) return headers if headers.is_a?(self) super end |
Instance Method Details
#==(other) ⇒ Object
114 115 116 |
# File 'lib/httpx/headers.rb', line 114 def ==(other) to_hash == Headers.new(other).to_hash end |
#[](field) ⇒ Object
returns the comma-separated values of the header field identified by field
, or nil otherwise.
68 69 70 71 |
# File 'lib/httpx/headers.rb', line 68 def [](field) a = @headers[downcased(field)] || return a.join(",") end |
#[]=(field, value) ⇒ Object
sets value
(if not nil) as single value for the field
header.
75 76 77 78 79 |
# File 'lib/httpx/headers.rb', line 75 def []=(field, value) return unless value @headers[downcased(field)] = array_value(value) end |
#add(field, value) ⇒ Object Also known as: add_header
adds additional value
to the existing, for header field
.
90 91 92 |
# File 'lib/httpx/headers.rb', line 90 def add(field, value) (@headers[downcased(field)] ||= []) << String(value) end |
#delete(field) ⇒ Object
deletes all values associated with field
header.
83 84 85 86 |
# File 'lib/httpx/headers.rb', line 83 def delete(field) canonical = downcased(field) @headers.delete(canonical) if @headers.key?(canonical) end |
#each ⇒ Object
returns the enumerable headers store in pairs of header field + the values in the comma-separated string format
106 107 108 109 110 111 112 |
# File 'lib/httpx/headers.rb', line 106 def each return enum_for(__method__) { @headers.size } unless block_given? @headers.each do |field, value| yield(field, value.join(", ")) unless value.empty? end end |
#freeze ⇒ Object
freezes the headers hash
39 40 41 42 |
# File 'lib/httpx/headers.rb', line 39 def freeze @headers.freeze super end |
#get(field) ⇒ Object
returns the values for the field
header in array format. This method is more internal, and for this reason doesn’t try to “correct” the user input, i.e. it doesn’t downcase the key.
145 146 147 |
# File 'lib/httpx/headers.rb', line 145 def get(field) @headers[field] || EMPTY end |
#initialize_clone(orig) ⇒ Object
cloned initialization
27 28 29 30 |
# File 'lib/httpx/headers.rb', line 27 def initialize_clone(orig) super @headers = orig.instance_variable_get(:@headers).clone end |
#initialize_dup(orig) ⇒ Object
dupped initialization
33 34 35 36 |
# File 'lib/httpx/headers.rb', line 33 def initialize_dup(orig) super @headers = orig.instance_variable_get(:@headers).dup end |
#key?(downcased_key) ⇒ Boolean
this is internal API and doesn’t abide to other public API guarantees, like downcasing strings. Please do not use this outside of core!
137 138 139 |
# File 'lib/httpx/headers.rb', line 137 def key?(downcased_key) @headers.key?(downcased_key) end |
#merge(other) ⇒ Object
merges headers with another header-quack. the merge rule is, if the header already exists, ignore what the other
headers has. Otherwise, set
57 58 59 60 61 62 63 |
# File 'lib/httpx/headers.rb', line 57 def merge(other) headers = dup other.each do |field, value| headers[field] = value end headers end |
#same_headers?(headers) ⇒ Boolean
44 45 46 47 48 49 50 51 |
# File 'lib/httpx/headers.rb', line 44 def same_headers?(headers) @headers.empty? || begin headers.each do |k, v| return false unless v == self[k] end true end end |
#to_a ⇒ Object
the headers store in array of pairs format
124 125 126 |
# File 'lib/httpx/headers.rb', line 124 def to_a Array(each) end |
#to_hash ⇒ Object
the headers store in Hash format
119 120 121 |
# File 'lib/httpx/headers.rb', line 119 def to_hash Hash[to_a] end |
#to_s ⇒ Object
headers as string
129 130 131 |
# File 'lib/httpx/headers.rb', line 129 def to_s @headers.to_s end |