lib/protocol/http/headers.rb in protocol-http-0.3.0 vs lib/protocol/http/headers.rb in protocol-http-0.4.0
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+#
# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
@@ -20,28 +22,28 @@
module Protocol
module HTTP
# Headers are an array of key-value pairs. Some header keys represent multiple values.
class Headers
- # Split by commas.
+ # Header value which is split by commas.
class Split < Array
COMMA = /\s*,\s*/
def initialize(value)
super(value.split(COMMA))
end
def << value
- super value.split(COMMA)
+ self.push(*value.split(COMMA))
end
def to_s
join(", ")
end
end
- # Split by newline charaters.
+ # Header value which is split by newline charaters (e.g. cookies).
class Multiple < Array
def initialize(value)
super()
self << value
@@ -78,13 +80,16 @@
attr :fields
def freeze
return if frozen?
- # Generate @indexed
+ # Ensure @indexed is generated:
self.to_h
+ @fields.freeze
+ @indexed.freeze
+
super
end
def empty?
@fields.empty?
@@ -220,24 +225,31 @@
def inspect
"#<#{self.class} #{@fields.inspect}>"
end
def == other
- if other.is_a? Hash
+ case other
+ when Hash
to_h == other
- else
+ when Headers
@fields == other.fields
+ else
+ @fields == other
end
end
# Used for merging objects into a sequential list of headers. Normalizes header keys and values.
class Merged
+ include Enumerable
+
def initialize(*all)
@all = all
end
def << headers
@all << headers
+
+ return self
end
# @yield [String, String] header key (lower case) and value (as string).
def each(&block)
@all.each do |headers|