class HTTPX::Headers

  1. lib/httpx/headers.rb
Superclass: Object

Public Instance Aliases

add_header -> add

helper to be used when adding an header field as a value to another field

h2_headers.add_header("vary", "accept-encoding")
h2_headers["vary"] #=> "accept-encoding"
h1_headers.add_header("vary", "accept-encoding")
h1_headers["vary"] #=> "Accept-Encoding"
to_h -> to_hash

Public Class methods

new(headers = nil)
[show source]
   # File lib/httpx/headers.rb
 6 def new(headers = nil)
 7   return headers if headers.is_a?(self)
 8 
 9   super
10 end
new(headers = nil)
[show source]
   # File lib/httpx/headers.rb
13 def initialize(headers = nil)
14   if headers.nil? || headers.empty?
15     @headers = headers.to_h
16     return
17   end
18 
19   @headers = {}
20 
21   headers.each do |field, value|
22     field = downcased(field)
23 
24     value = array_value(value)
25 
26     current = @headers[field]
27 
28     if current.nil?
29       @headers[field] = value
30     else
31       current.concat(value)
32     end
33   end
34 end

Public Instance methods

==(other)
[show source]
    # File lib/httpx/headers.rb
119 def ==(other)
120   other == to_hash
121 end
[](field)

returns the comma-separated values of the header field identified by field, or nil otherwise.

[show source]
   # File lib/httpx/headers.rb
69 def [](field)
70   a = @headers[downcased(field)] || return
71   a.join(", ")
72 end
[]=(field, value)

sets value (if not nil) as single value for the field header.

[show source]
   # File lib/httpx/headers.rb
76 def []=(field, value)
77   return unless value
78 
79   @headers[downcased(field)] = array_value(value)
80 end
add(field, value)

adds additional value to the existing, for header field.

[show source]
   # File lib/httpx/headers.rb
91 def add(field, value)
92   (@headers[downcased(field)] ||= []) << String(value)
93 end
delete(field)

deletes all values associated with field header.

[show source]
   # File lib/httpx/headers.rb
84 def delete(field)
85   canonical = downcased(field)
86   @headers.delete(canonical) if @headers.key?(canonical)
87 end
each(extra_headers = nil)

returns the enumerable headers store in pairs of header field + the values in the comma-separated string format

[show source]
    # File lib/httpx/headers.rb
107 def each(extra_headers = nil)
108   return enum_for(__method__, extra_headers) { @headers.size } unless block_given?
109 
110   @headers.each do |field, value|
111     yield(field, value.join(", ")) unless value.empty?
112   end
113 
114   extra_headers.each do |field, value|
115     yield(field, value) unless value.empty?
116   end if extra_headers
117 end
empty?()
[show source]
    # File lib/httpx/headers.rb
123 def empty?
124   @headers.empty?
125 end
freeze()

freezes the headers hash

[show source]
   # File lib/httpx/headers.rb
49 def freeze
50   @headers.freeze
51   super
52 end
get(field)

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.

[show source]
    # File lib/httpx/headers.rb
162 def get(field)
163   @headers[field] || EMPTY
164 end
initialize_clone(orig, **kwargs)

cloned initialization

[show source]
   # File lib/httpx/headers.rb
37 def initialize_clone(orig, **kwargs)
38   super
39   @headers = orig.instance_variable_get(:@headers).clone(**kwargs)
40 end
initialize_dup(orig)

dupped initialization

[show source]
   # File lib/httpx/headers.rb
43 def initialize_dup(orig)
44   super
45   @headers = orig.instance_variable_get(:@headers).dup
46 end
inspect()

:nocov:

[show source]
    # File lib/httpx/headers.rb
144 def inspect
145   "#<#{self.class}:#{object_id} " \
146     "#{to_hash.inspect}>"
147 end
key?(downcased_key)

this is internal API and doesn’t abide to other public API guarantees, like downcasing strings. Please do not use this outside of core!

[show source]
    # File lib/httpx/headers.rb
154 def key?(downcased_key)
155   @headers.key?(downcased_key)
156 end
merge(other)

merges headers with another header-quack. the merge rule is, if the header already exists, ignore what the other headers has. Otherwise, set

[show source]
   # File lib/httpx/headers.rb
58 def merge(other)
59   headers = dup
60   other.each do |field, value|
61     headers[downcased(field)] = value
62   end
63   headers
64 end
to_a()

the headers store in array of pairs format

[show source]
    # File lib/httpx/headers.rb
134 def to_a
135   Array(each)
136 end
to_hash()

the headers store in Hash format

[show source]
    # File lib/httpx/headers.rb
128 def to_hash
129   Hash[to_a]
130 end
to_s()

headers as string

[show source]
    # File lib/httpx/headers.rb
139 def to_s
140   @headers.to_s
141 end