class Webmachine::Headers
Case-insensitive Hash of Request
headers
Constants
- CGI_HTTP_MATCH
- CONTENT_TYPE_LENGTH_MATCH
Public Class Methods
Creates a new headers object populated with the given objects. It supports the same forms as {Hash.[]}.
@overload [](key, value, …)
Pairs of keys and values @param [Object] key @param [Object] value
@overload [](array)
Array of key-value pairs @param [Array<Object, Object>, ...]
@overload [](object)
Object convertible to a hash @param [Object]
@return [Webmachine::Headers]
# File lib/webmachine/headers.rb, line 34 def self.[](*args) super(super(*args).map { |k, v| [k.to_s.downcase, v] }) end
Convert CGI-style Hash into Request
headers @param [Hash] env a hash of CGI-style env/headers @return [Webmachine::Headers]
# File lib/webmachine/headers.rb, line 12 def self.from_cgi(env) env.each_with_object(new) do |(k, v), h| if k =~ CGI_HTTP_MATCH || k =~ CONTENT_TYPE_LENGTH_MATCH h[$1.tr(UNDERSCORE, DASH)] = v end end end
Public Instance Methods
Fetch a header
# File lib/webmachine/headers.rb, line 39 def [](key) super transform_key(key) end
Set a header
# File lib/webmachine/headers.rb, line 44 def []=(key, value) super transform_key(key), value end
Delete a header
# File lib/webmachine/headers.rb, line 72 def delete(key) super transform_key(key) end
Returns the value for the given key. If the key can’t be found, there are several options: With no other arguments, it will raise a KeyError error; if default is given, then that will be returned; if the optional code block is specified, then that will be run and its result returned.
@overload fetch(key)
A key @param [Object] key
@overload fetch(key, default)
A key and a default value @param [Object] key @param [Object] default
@overload fetch(key) {|key| block }
A key and a code block @param [Object] @yield [key] Passes the key to the block
@return [Object] the value for the key or the default
# File lib/webmachine/headers.rb, line 67 def fetch(*args, &block) super(transform_key(args.shift), *args, &block) end
Select matching headers
# File lib/webmachine/headers.rb, line 77 def grep(pattern) self.class[select { |k, _| pattern === k }] end
Private Instance Methods
# File lib/webmachine/headers.rb, line 83 def transform_key(key) key.to_s.downcase end