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 @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
returns the comma-separated values of the header field identified by field
, or nil otherwise.
# File lib/httpx/headers.rb 68 def [](field) 69 a = @headers[downcased(field)] || return 70 a.join(", ") 71 end
sets value
(if not nil) as single value for the field
header.
# File lib/httpx/headers.rb 75 def []=(field, value) 76 return unless value 77 78 @headers[downcased(field)] = array_value(value) 79 end
adds additional value
to the existing, for header field
.
# File lib/httpx/headers.rb 90 def add(field, value) 91 (@headers[downcased(field)] ||= []) << String(value) 92 end
deletes all values associated with field
header.
# File lib/httpx/headers.rb 83 def delete(field) 84 canonical = downcased(field) 85 @headers.delete(canonical) if @headers.key?(canonical) 86 end
returns the enumerable headers store in pairs of header field + the values in the comma-separated string format
# 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
freezes the headers hash
# File lib/httpx/headers.rb 37 def freeze 38 @headers.freeze 39 super 40 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 156 def get(field) 157 @headers[field] || EMPTY 158 end
cloned initialization
# File lib/httpx/headers.rb 25 def initialize_clone(orig) 26 super 27 @headers = orig.instance_variable_get(:@headers).clone 28 end
dupped initialization
# File lib/httpx/headers.rb 31 def initialize_dup(orig) 32 super 33 @headers = orig.instance_variable_get(:@headers).dup 34 end
:nocov:
# File lib/httpx/headers.rb 139 def inspect 140 to_hash.inspect 141 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 148 def key?(downcased_key) 149 @headers.key?(downcased_key) 150 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 57 def merge(other) 58 headers = dup 59 other.each do |field, value| 60 headers[downcased(field)] = value 61 end 62 headers 63 end
# 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
the headers store in array of pairs format
# File lib/httpx/headers.rb 129 def to_a 130 Array(each) 131 end
the headers store in Hash format
# File lib/httpx/headers.rb 123 def to_hash 124 Hash[to_a] 125 end
headers as string
# File lib/httpx/headers.rb 134 def to_s 135 @headers.to_s 136 end