Methods
Public Class
Public Instance
Public Instance Aliases
Public Class methods
# File lib/httpx/headers.rb 6 def new(headers = nil) 7 return headers if headers.is_a?(self) 8 9 super 10 end
# 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
returns the comma-separated values of the header field identified by field
, or nil otherwise.
# File lib/httpx/headers.rb 69 def [](field) 70 a = @headers[downcased(field)] || return 71 a.join(", ") 72 end
sets value
(if not nil) as single value for the field
header.
# File lib/httpx/headers.rb 76 def []=(field, value) 77 return unless value 78 79 @headers[downcased(field)] = array_value(value) 80 end
adds additional value
to the existing, for header field
.
# File lib/httpx/headers.rb 91 def add(field, value) 92 (@headers[downcased(field)] ||= []) << String(value) 93 end
deletes all values associated with field
header.
# File lib/httpx/headers.rb 84 def delete(field) 85 canonical = downcased(field) 86 @headers.delete(canonical) if @headers.key?(canonical) 87 end
returns the enumerable headers store in pairs of header field + the values in the comma-separated string format
# 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
freezes the headers hash
# File lib/httpx/headers.rb 49 def freeze 50 @headers.freeze 51 super 52 end
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.
# File lib/httpx/headers.rb 162 def get(field) 163 @headers[field] || EMPTY 164 end
cloned initialization
# File lib/httpx/headers.rb 37 def initialize_clone(orig, **kwargs) 38 super 39 @headers = orig.instance_variable_get(:@headers).clone(**kwargs) 40 end
dupped initialization
# File lib/httpx/headers.rb 43 def initialize_dup(orig) 44 super 45 @headers = orig.instance_variable_get(:@headers).dup 46 end
:nocov:
# File lib/httpx/headers.rb 144 def inspect 145 "#<#{self.class}:#{object_id} " \ 146 "#{to_hash.inspect}>" 147 end
this is internal API and doesn’t abide to other public API guarantees, like downcasing strings. Please do not use this outside of core!
# File lib/httpx/headers.rb 154 def key?(downcased_key) 155 @headers.key?(downcased_key) 156 end
merges headers with another header-quack. the merge rule is, if the header already exists, ignore what the other
headers has. Otherwise, set
# 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
the headers store in array of pairs format
# File lib/httpx/headers.rb 134 def to_a 135 Array(each) 136 end
the headers store in Hash format
# File lib/httpx/headers.rb 128 def to_hash 129 Hash[to_a] 130 end
headers as string
# File lib/httpx/headers.rb 139 def to_s 140 @headers.to_s 141 end