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   @headers = {}
15   return unless headers
16 
17   headers.each do |field, value|
18     array_value(value).each do |v|
19       add(downcased(field), v)
20     end
21   end
22 end

Public Instance methods

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

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

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

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

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

adds additional value to the existing, for header field.

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

deletes all values associated with field header.

[show source]
   # File lib/httpx/headers.rb
83 def delete(field)
84   canonical = downcased(field)
85   @headers.delete(canonical) if @headers.key?(canonical)
86 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
106 def each(extra_headers = nil)
107   return enum_for(__method__, extra_headers) { @headers.size } unless block_given?
108 
109   @headers.each do |field, value|
110     yield(field, value.join(", ")) unless value.empty?
111   end
112 
113   extra_headers.each do |field, value|
114     yield(field, value) unless value.empty?
115   end if extra_headers
116 end
freeze()

freezes the headers hash

[show source]
   # File lib/httpx/headers.rb
37 def freeze
38   @headers.freeze
39   super
40 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
156 def get(field)
157   @headers[field] || EMPTY
158 end
initialize_clone(orig)

cloned initialization

[show source]
   # File lib/httpx/headers.rb
25 def initialize_clone(orig)
26   super
27   @headers = orig.instance_variable_get(:@headers).clone
28 end
initialize_dup(orig)

dupped initialization

[show source]
   # File lib/httpx/headers.rb
31 def initialize_dup(orig)
32   super
33   @headers = orig.instance_variable_get(:@headers).dup
34 end
inspect()

:nocov:

[show source]
    # File lib/httpx/headers.rb
139 def inspect
140   to_hash.inspect
141 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
148 def key?(downcased_key)
149   @headers.key?(downcased_key)
150 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
57 def merge(other)
58   headers = dup
59   other.each do |field, value|
60     headers[downcased(field)] = value
61   end
62   headers
63 end
same_headers?(headers)
[show source]
   # File lib/httpx/headers.rb
42 def same_headers?(headers)
43   @headers.empty? || begin
44     headers.each do |k, v|
45       next unless key?(k)
46 
47       return false unless v == self[k]
48     end
49     true
50   end
51 end
to_a()

the headers store in array of pairs format

[show source]
    # File lib/httpx/headers.rb
129 def to_a
130   Array(each)
131 end
to_hash()

the headers store in Hash format

[show source]
    # File lib/httpx/headers.rb
123 def to_hash
124   Hash[to_a]
125 end
to_s()

headers as string

[show source]
    # File lib/httpx/headers.rb
134 def to_s
135   @headers.to_s
136 end