Class: HTTPX::Headers

Inherits:
Object
  • Object
show all
Defined in:
lib/httpx/headers.rb

Constant Summary collapse

EMPTY =

:nodoc:

[].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers = nil) ⇒ Headers

Returns a new instance of Headers.



15
16
17
18
19
20
21
22
23
24
# File 'lib/httpx/headers.rb', line 15

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



8
9
10
11
12
# File 'lib/httpx/headers.rb', line 8

def new(headers = nil)
  return headers if headers.is_a?(self)

  super
end

Instance Method Details

#==(other) ⇒ Object



105
106
107
# File 'lib/httpx/headers.rb', line 105

def ==(other)
  to_hash == Headers.new(other).to_hash
end

#[](field) ⇒ Object

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



59
60
61
62
# File 'lib/httpx/headers.rb', line 59

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.



66
67
68
69
70
# File 'lib/httpx/headers.rb', line 66

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.



81
82
83
# File 'lib/httpx/headers.rb', line 81

def add(field, value)
  (@headers[downcased(field)] ||= []) << String(value)
end

#delete(field) ⇒ Object

deletes all values associated with field header.



74
75
76
77
# File 'lib/httpx/headers.rb', line 74

def delete(field)
  canonical = downcased(field)
  @headers.delete(canonical) if @headers.key?(canonical)
end

#eachObject

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



97
98
99
100
101
102
103
# File 'lib/httpx/headers.rb', line 97

def each
  return enum_for(__method__) { @headers.size } unless block_given?

  @headers.each do |field, value|
    yield(field, value.join(", ")) unless value.empty?
  end
end

#freezeObject

freezes the headers hash



39
40
41
42
# File 'lib/httpx/headers.rb', line 39

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.



136
137
138
# File 'lib/httpx/headers.rb', line 136

def get(field)
  @headers[field] || EMPTY
end

#initialize_clone(orig) ⇒ Object

cloned initialization



27
28
29
30
# File 'lib/httpx/headers.rb', line 27

def initialize_clone(orig)
  super
  @headers = orig.instance_variable_get(:@headers).clone
end

#initialize_dup(orig) ⇒ Object

dupped initialization



33
34
35
36
# File 'lib/httpx/headers.rb', line 33

def initialize_dup(orig)
  super
  @headers = orig.instance_variable_get(:@headers).dup
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!

Returns:

  • (Boolean)


128
129
130
# File 'lib/httpx/headers.rb', line 128

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



48
49
50
51
52
53
54
# File 'lib/httpx/headers.rb', line 48

def merge(other)
  headers = dup
  other.each do |field, value|
    headers[field] = value
  end
  headers
end

#to_aObject

the headers store in array of pairs format



115
116
117
# File 'lib/httpx/headers.rb', line 115

def to_a
  Array(each)
end

#to_hashObject

the headers store in Hash format



110
111
112
# File 'lib/httpx/headers.rb', line 110

def to_hash
  Hash[to_a]
end

#to_sObject

headers as string



120
121
122
# File 'lib/httpx/headers.rb', line 120

def to_s
  @headers.to_s
end