Class: HTTPX::Headers

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HeadersPatternMatchExtensions

#deconstruct

Constructor Details

#initialize(headers = nil) ⇒ Headers

Returns a new instance of Headers.

[View source]

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

.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

#==(other) ⇒ Object

[View source]

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

def ==(other)
  other == to_hash
end

#[](field) ⇒ Object

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

[View source]

69
70
71
72
# File 'lib/httpx/headers.rb', line 69

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.

[View source]

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

#add(field, value) ⇒ Object Also known as: add_header

adds additional value to the existing, for header field.

[View source]

91
92
93
# File 'lib/httpx/headers.rb', line 91

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

#delete(field) ⇒ Object

deletes all values associated with field header.

[View source]

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

#each(extra_headers = nil) ⇒ Object

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

[View source]

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

#empty?Boolean

Returns:

  • (Boolean)
[View source]

123
124
125
# File 'lib/httpx/headers.rb', line 123

def empty?
  @headers.empty?
end

#freezeObject

freezes the headers hash

[View source]

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

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.

[View source]

162
163
164
# File 'lib/httpx/headers.rb', line 162

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

#initialize_clone(orig, **kwargs) ⇒ Object

cloned initialization

[View source]

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

#initialize_dup(orig) ⇒ Object

dupped initialization

[View source]

43
44
45
46
# File 'lib/httpx/headers.rb', line 43

def initialize_dup(orig)
  super
  @headers = orig.instance_variable_get(:@headers).dup
end

#inspectObject

:nocov:

[View source]

144
145
146
147
# File 'lib/httpx/headers.rb', line 144

def inspect
  "#<#{self.class}:#{object_id} " \
    "#{to_hash.inspect}>"
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)
[View source]

154
155
156
# File 'lib/httpx/headers.rb', line 154

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

[View source]

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

#to_aObject

the headers store in array of pairs format

[View source]

134
135
136
# File 'lib/httpx/headers.rb', line 134

def to_a
  Array(each)
end

#to_hashObject Also known as: to_h

the headers store in Hash format

[View source]

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

def to_hash
  Hash[to_a]
end

#to_sObject

headers as string

[View source]

139
140
141
# File 'lib/httpx/headers.rb', line 139

def to_s
  @headers.to_s
end