Class: HTTP::Protocol::Headers
- Inherits:
-
Object
- Object
- HTTP::Protocol::Headers
show all
- Defined in:
- lib/http/protocol/headers.rb
Defined Under Namespace
Classes: Merged, Multiple, Split
Constant Summary
collapse
- MERGE_POLICY =
{
'content-type' => false,
'content-disposition' => false,
'content-length' => false,
'user-agent' => false,
'referer' => false,
'host' => false,
'authorization' => false,
'proxy-authorization' => false,
'if-modified-since' => false,
'if-unmodified-since' => false,
'from' => false,
'location' => false,
'max-forwards' => false,
'connection' => Split,
'via' => Split,
'x-forwarded-for' => Split,
'set-cookie' => Multiple,
'www-authenticate' => Multiple,
'proxy-authenticate' => Multiple
}.tap{|hash| hash.default = Split}
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(fields = []) ⇒ Headers
Returns a new instance of Headers.
67
68
69
70
|
# File 'lib/http/protocol/headers.rb', line 67
def initialize(fields = [])
@fields = fields
@indexed = to_h
end
|
Instance Attribute Details
#fields ⇒ Object
Returns the value of attribute fields.
72
73
74
|
# File 'lib/http/protocol/headers.rb', line 72
def fields
@fields
end
|
Class Method Details
.[](hash) ⇒ Object
63
64
65
|
# File 'lib/http/protocol/headers.rb', line 63
def self.[] hash
self.new(hash.to_a)
end
|
Instance Method Details
#==(other) ⇒ Object
199
200
201
202
203
204
205
|
# File 'lib/http/protocol/headers.rb', line 199
def == other
if other.is_a? Hash
to_h == other
else
@fields == other.fields
end
end
|
#[](key) ⇒ Object
187
188
189
|
# File 'lib/http/protocol/headers.rb', line 187
def [] key
@indexed[key]
end
|
#[]=(key, value) ⇒ Object
Append the value to the given key. Some values can be appended multiple times, others can only be set once.
129
130
131
132
133
|
# File 'lib/http/protocol/headers.rb', line 129
def []= key, value
merge_into(@indexed, key.downcase, value)
@fields << [key, value]
end
|
#add(key, value) ⇒ Object
110
111
112
|
# File 'lib/http/protocol/headers.rb', line 110
def add(key, value)
self[key] = value
end
|
#delete(key) ⇒ Object
Delete all headers with the given key, and return the merged value.
164
165
166
167
168
169
170
|
# File 'lib/http/protocol/headers.rb', line 164
def delete(key)
values, @fields = @fields.partition do |field|
field.first.downcase == key
end
return @indexed.delete(key)
end
|
#each(&block) ⇒ Object
86
87
88
|
# File 'lib/http/protocol/headers.rb', line 86
def each(&block)
@fields.each(&block)
end
|
#empty? ⇒ Boolean
82
83
84
|
# File 'lib/http/protocol/headers.rb', line 82
def empty?
@fields.empty?
end
|
#freeze ⇒ Object
74
75
76
77
78
79
80
|
# File 'lib/http/protocol/headers.rb', line 74
def freeze
return if frozen?
@indexed = to_h
super
end
|
#include?(key) ⇒ Boolean
90
91
92
|
# File 'lib/http/protocol/headers.rb', line 90
def include? key
self[key] != nil
end
|
#merge(headers) ⇒ Object
122
123
124
|
# File 'lib/http/protocol/headers.rb', line 122
def merge()
self.dup.merge!()
end
|
#merge!(headers) ⇒ Object
114
115
116
117
118
119
120
|
# File 'lib/http/protocol/headers.rb', line 114
def merge!()
.each do |key, value|
self[key] = value
end
return self
end
|
#slice(keys) ⇒ Object
106
107
108
|
# File 'lib/http/protocol/headers.rb', line 106
def slice(keys)
self.dup.slice!(keys)
end
|
#slice!(keys) ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/http/protocol/headers.rb', line 94
def slice!(keys)
values, @fields = @fields.partition do |field|
keys.include?(field.first.downcase)
end
keys.each do |key|
@indexed.delete(key)
end
return self
end
|
#to_h ⇒ Object
191
192
193
194
195
196
197
|
# File 'lib/http/protocol/headers.rb', line 191
def to_h
@fields.inject({}) do |hash, (key, value)|
merge_into(hash, key.downcase, value)
hash
end
end
|