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.
- #empty? ⇒ Boolean
-
#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, **kwargs) ⇒ 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.
-
#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
permalink #initialize(headers = nil) ⇒ Headers
Returns a new instance of Headers.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/httpx/headers.rb', line 13 def initialize(headers = nil) if headers.nil? || headers.empty? @headers = headers.to_h return end @headers = {} headers.each do |field, value| field = downcased(field) value = array_value(value) current = @headers[field] if current.nil? @headers[field] = value else current.concat(value) end end end |
Class Method Details
permalink .new(headers = nil) ⇒ Object
[View source]
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
permalink #==(other) ⇒ Object
[View source]
119 120 121 |
# File 'lib/httpx/headers.rb', line 119 def ==(other) other == to_hash end |
permalink #[](field) ⇒ Object
returns the comma-separated values of the header field identified by field
, or nil otherwise.
69 70 71 72 |
# File 'lib/httpx/headers.rb', line 69 def [](field) a = @headers[downcased(field)] || return a.join(", ") end |
permalink #[]=(field, value) ⇒ Object
sets value
(if not nil) as single value for the field
header.
76 77 78 79 80 |
# File 'lib/httpx/headers.rb', line 76 def []=(field, value) return unless value @headers[downcased(field)] = array_value(value) end |
permalink #add(field, value) ⇒ Object Also known as: add_header
adds additional value
to the existing, for header field
.
91 92 93 |
# File 'lib/httpx/headers.rb', line 91 def add(field, value) (@headers[downcased(field)] ||= []) << String(value) end |
permalink #delete(field) ⇒ Object
deletes all values associated with field
header.
84 85 86 87 |
# File 'lib/httpx/headers.rb', line 84 def delete(field) canonical = downcased(field) @headers.delete(canonical) if @headers.key?(canonical) end |
permalink #each(extra_headers = nil) ⇒ Object
returns the enumerable headers store in pairs of header field + the values in the comma-separated string format
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/httpx/headers.rb', line 107 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 |
permalink #empty? ⇒ Boolean
123 124 125 |
# File 'lib/httpx/headers.rb', line 123 def empty? @headers.empty? end |
permalink #freeze ⇒ Object
freezes the headers hash
49 50 51 52 |
# File 'lib/httpx/headers.rb', line 49 def freeze @headers.freeze super end |
permalink #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.
162 163 164 |
# File 'lib/httpx/headers.rb', line 162 def get(field) @headers[field] || EMPTY end |
permalink #initialize_clone(orig, **kwargs) ⇒ Object
cloned initialization
37 38 39 40 |
# File 'lib/httpx/headers.rb', line 37 def initialize_clone(orig, **kwargs) super @headers = orig.instance_variable_get(:@headers).clone(**kwargs) end |
permalink #initialize_dup(orig) ⇒ Object
dupped initialization
43 44 45 46 |
# File 'lib/httpx/headers.rb', line 43 def initialize_dup(orig) super @headers = orig.instance_variable_get(:@headers).dup end |
permalink #inspect ⇒ Object
:nocov:
144 145 146 147 |
# File 'lib/httpx/headers.rb', line 144 def inspect "#<#{self.class}:#{object_id} " \ "#{to_hash.inspect}>" end |
permalink #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!
154 155 156 |
# File 'lib/httpx/headers.rb', line 154 def key?(downcased_key) @headers.key?(downcased_key) end |
permalink #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
58 59 60 61 62 63 64 |
# File 'lib/httpx/headers.rb', line 58 def merge(other) headers = dup other.each do |field, value| headers[downcased(field)] = value end headers end |
permalink #to_a ⇒ Object
the headers store in array of pairs format
134 135 136 |
# File 'lib/httpx/headers.rb', line 134 def to_a Array(each) end |
permalink #to_hash ⇒ Object Also known as: to_h
the headers store in Hash format
128 129 130 |
# File 'lib/httpx/headers.rb', line 128 def to_hash Hash[to_a] end |
permalink #to_s ⇒ Object
headers as string
139 140 141 |
# File 'lib/httpx/headers.rb', line 139 def to_s @headers.to_s end |