lib/http/protocol/headers.rb in http-protocol-0.8.1 vs lib/http/protocol/headers.rb in http-protocol-0.9.0
- old
+ new
@@ -97,21 +97,41 @@
end
keys.each do |key|
@indexed.delete(key)
end
+
+ return self
end
+ def slice(keys)
+ self.dup.slice!(keys)
+ end
+
def add(key, value)
self[key] = value
end
+ def merge!(headers)
+ headers.each do |key, value|
+ self[key] = value
+ end
+
+ return self
+ end
+
+ def merge(headers)
+ self.dup.merge!(headers)
+ end
+
+ # Append the value to the given key. Some values can be appended multiple times, others can only be set once.
+ # @param key [String] The header key.
+ # @param value The header value.
def []= key, value
- @fields << [key, value]
+ merge_into(@indexed, key.downcase, value)
- # It would be good to do some kind of validation here.
- merge(@indexed, key.downcase, value)
+ @fields << [key, value]
end
MERGE_POLICY = {
# Headers which may only be specified once.
'content-type' => false,
@@ -147,20 +167,18 @@
end
return @indexed.delete(key)
end
- def merge(hash, key, value)
+ protected def merge_into(hash, key, value)
if policy = MERGE_POLICY[key]
if current_value = hash[key]
current_value << value
else
hash[key] = policy.new(value)
end
else
- raise ArgumentError, "Header #{key} can only be set once!" if hash.include?(key)
-
# We can't merge these, we only expose the last one set.
hash[key] = value
end
end
@@ -168,10 +186,10 @@
@indexed[key]
end
def to_h
@fields.inject({}) do |hash, (key, value)|
- merge(hash, key.downcase, value)
+ merge_into(hash, key.downcase, value)
hash
end
end