Class: Webmachine::Headers
- Inherits:
-
Hash
- Object
- Hash
- Webmachine::Headers
- Defined in:
- lib/webmachine/headers.rb
Overview
Case-insensitive Hash of Request headers
Constant Summary collapse
- CGI_HTTP_MATCH =
/^HTTP_(\w+)$/.freeze
- CONTENT_TYPE_LENGTH_MATCH =
/^(CONTENT_(?:TYPE|LENGTH))$/.freeze
Class Method Summary collapse
-
.[](*args) ⇒ Webmachine::Headers
Creates a new headers object populated with the given objects.
-
.from_cgi(env) ⇒ Webmachine::Headers
Convert CGI-style Hash into Request headers.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Fetch a header.
-
#[]=(key, value) ⇒ Object
Set a header.
-
#delete(key) ⇒ Object
Delete a header.
-
#fetch(*args, &block) ⇒ Object
Returns the value for the given key.
-
#grep(pattern) ⇒ Object
Select matching headers.
Class Method Details
.[](key, value, ...) ⇒ Webmachine::Headers .[](array) ⇒ Webmachine::Headers .[](object) ⇒ Webmachine::Headers
Creates a new headers object populated with the given objects. It supports the same forms as Hash.[].
35 36 37 |
# File 'lib/webmachine/headers.rb', line 35 def self.[](*args) super(super(*args).map {|k, v| [k.to_s.downcase, v]}) end |
.from_cgi(env) ⇒ Webmachine::Headers
Convert CGI-style Hash into Request headers
12 13 14 15 16 17 18 19 |
# File 'lib/webmachine/headers.rb', line 12 def self.from_cgi(env) env.inject(new) do |h,(k,v)| if k =~ CGI_HTTP_MATCH || k =~ CONTENT_TYPE_LENGTH_MATCH h[$1.tr(UNDERSCORE, DASH)] = v end h end end |
Instance Method Details
#[](key) ⇒ Object
Fetch a header
40 41 42 |
# File 'lib/webmachine/headers.rb', line 40 def [](key) super transform_key(key) end |
#[]=(key, value) ⇒ Object
Set a header
45 46 47 |
# File 'lib/webmachine/headers.rb', line 45 def []=(key,value) super transform_key(key), value end |
#delete(key) ⇒ Object
Delete a header
73 74 75 |
# File 'lib/webmachine/headers.rb', line 73 def delete(key) super transform_key(key) end |
#fetch(key) ⇒ Object #fetch(key, default) ⇒ Object #fetch(key) {|key| ... } ⇒ Object
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.
68 69 70 |
# File 'lib/webmachine/headers.rb', line 68 def fetch(*args, &block) super(transform_key(args.shift), *args, &block) end |
#grep(pattern) ⇒ Object
Select matching headers
78 79 80 |
# File 'lib/webmachine/headers.rb', line 78 def grep(pattern) self.class[select { |k,_| pattern === k }] end |