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.
-
#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.
14 15 16 17 18 19 20 21 22 |
# File 'lib/httpx/headers.rb', line 14 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 |
# 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
102 103 104 |
# File 'lib/httpx/headers.rb', line 102 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.
58 59 60 61 |
# File 'lib/httpx/headers.rb', line 58 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.
65 66 67 68 |
# File 'lib/httpx/headers.rb', line 65 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
.
79 80 81 |
# File 'lib/httpx/headers.rb', line 79 def add(field, value) (@headers[downcased(field)] ||= []) << String(value) end |
#delete(field) ⇒ Object
deletes all values associated with field
header.
72 73 74 75 |
# File 'lib/httpx/headers.rb', line 72 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
95 96 97 98 99 100 |
# File 'lib/httpx/headers.rb', line 95 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
37 38 39 40 |
# File 'lib/httpx/headers.rb', line 37 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.
133 134 135 |
# File 'lib/httpx/headers.rb', line 133 def get(field) @headers[field] || EMPTY end |
#initialize_clone(orig) ⇒ Object
cloned initialization
25 26 27 28 |
# File 'lib/httpx/headers.rb', line 25 def initialize_clone(orig) super @headers = orig.instance_variable_get(:@headers).clone end |
#initialize_dup(orig) ⇒ Object
dupped initialization
31 32 33 34 |
# File 'lib/httpx/headers.rb', line 31 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!
125 126 127 |
# File 'lib/httpx/headers.rb', line 125 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
46 47 48 49 50 51 52 53 |
# File 'lib/httpx/headers.rb', line 46 def merge(other) # TODO: deep-copy headers = dup other.each do |field, value| headers[field] = value end headers end |
#to_a ⇒ Object
the headers store in array of pairs format
112 113 114 |
# File 'lib/httpx/headers.rb', line 112 def to_a Array(each) end |
#to_hash ⇒ Object
the headers store in Hash format
107 108 109 |
# File 'lib/httpx/headers.rb', line 107 def to_hash Hash[to_a] end |
#to_s ⇒ Object
headers as string
117 118 119 |
# File 'lib/httpx/headers.rb', line 117 def to_s @headers.to_s end |