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"

Public Class methods

new (headers = nil)
[show source]
   # File lib/httpx/headers.rb
 8 def new(headers = nil)
 9   return headers if headers.is_a?(self)
10 
11   super
12 end
new (headers = nil)
[show source]
   # File lib/httpx/headers.rb
15 def initialize(headers = nil)
16   @headers = {}
17   return unless headers
18 
19   headers.each do |field, value|
20     array_value(value).each do |v|
21       add(downcased(field), v)
22     end
23   end
24 end

Public Instance methods

== (other)
[show source]
    # File lib/httpx/headers.rb
114 def ==(other)
115   to_hash == Headers.new(other).to_hash
116 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 ()

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
107   return enum_for(__method__) { @headers.size } unless block_given?
108 
109   @headers.each do |field, value|
110     yield(field, value.join(", ")) unless value.empty?
111   end
112 end
freeze ()

freezes the headers hash

[show source]
   # File lib/httpx/headers.rb
39 def freeze
40   @headers.freeze
41   super
42 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
145 def get(field)
146   @headers[field] || EMPTY
147 end
initialize_clone (orig)

cloned initialization

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

dupped initialization

[show source]
   # File lib/httpx/headers.rb
33 def initialize_dup(orig)
34   super
35   @headers = orig.instance_variable_get(:@headers).dup
36 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
137 def key?(downcased_key)
138   @headers.key?(downcased_key)
139 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[field] = value
61   end
62   headers
63 end
same_headers? (headers)
[show source]
   # File lib/httpx/headers.rb
44 def same_headers?(headers)
45   @headers.empty? || begin
46     headers.each do |k, v|
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
124 def to_a
125   Array(each)
126 end
to_hash ()

the headers store in Hash format

[show source]
    # File lib/httpx/headers.rb
119 def to_hash
120   Hash[to_a]
121 end
to_s ()

headers as string

[show source]
    # File lib/httpx/headers.rb
129 def to_s
130   @headers.to_s
131 end