Class: HTTPX::Headers
- Inherits:
-
Object
- Object
- HTTPX::Headers
- Includes:
- HeadersPatternMatchExtensions
- Defined in:
- lib/httpx/headers.rb
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(extra_headers = nil) ⇒ 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.
-
#inspect ⇒ Object
:nocov:.
-
#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
(also: #to_h)
the headers store in Hash format.
-
#to_s ⇒ Object
headers as string.
Methods included from HeadersPatternMatchExtensions
Constructor Details
#initialize(headers = nil) ⇒ Headers
Returns a new instance of Headers.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/httpx/headers.rb', line 13 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
6 7 8 9 10 |
# File 'lib/httpx/headers.rb', line 6 def new(headers = nil) return headers if headers.is_a?(self) super end |
Instance Method Details
#==(other) ⇒ Object
118 119 120 |
# File 'lib/httpx/headers.rb', line 118 def ==(other) 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(extra_headers = nil) ⇒ 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 113 114 115 116 |
# File 'lib/httpx/headers.rb', line 106 def each(extra_headers = nil) return enum_for(__method__, extra_headers) { @headers.size } unless block_given? @headers.each do |field, value| yield(field, value.join(", ")) unless value.empty? end extra_headers.each do |field, value| yield(field, value) unless value.empty? end if extra_headers 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.
156 157 158 |
# File 'lib/httpx/headers.rb', line 156 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 |
#inspect ⇒ Object
:nocov:
139 140 141 |
# File 'lib/httpx/headers.rb', line 139 def inspect to_hash.inspect 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!
148 149 150 |
# File 'lib/httpx/headers.rb', line 148 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[downcased(field)] = value end headers end |
#same_headers?(headers) ⇒ Boolean
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/httpx/headers.rb', line 42 def same_headers?(headers) @headers.empty? || begin headers.each do |k, v| next unless key?(k) return false unless v == self[k] end true end end |
#to_a ⇒ Object
the headers store in array of pairs format
129 130 131 |
# File 'lib/httpx/headers.rb', line 129 def to_a Array(each) end |
#to_hash ⇒ Object Also known as: to_h
the headers store in Hash format
123 124 125 |
# File 'lib/httpx/headers.rb', line 123 def to_hash Hash[to_a] end |
#to_s ⇒ Object
headers as string
134 135 136 |
# File 'lib/httpx/headers.rb', line 134 def to_s @headers.to_s end |