Class: Webmachine::Headers

Inherits:
Hash
  • Object
show all
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

Instance Method Summary collapse

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.[].

Overloads:

  • .[](key, value, ...) ⇒ Webmachine::Headers

    Pairs of keys and values

    Parameters:

    • key (Object)
    • value (Object)
  • .[](array) ⇒ Webmachine::Headers

    Array of key-value pairs

    Parameters:

    • (Array<Object, Object>, ...)
  • .[](object) ⇒ Webmachine::Headers

    Object convertible to a hash

    Parameters:

    • (Object)

Returns:



34
35
36
# File 'lib/webmachine/headers.rb', line 34

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

Parameters:

  • env (Hash)

    a hash of CGI-style env/headers

Returns:



12
13
14
15
16
17
18
# 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

Instance Method Details

#[](key) ⇒ Object

Fetch a header



39
40
41
# File 'lib/webmachine/headers.rb', line 39

def [](key)
  super(transform_key(key))
end

#[]=(key, value) ⇒ Object

Set a header



44
45
46
# File 'lib/webmachine/headers.rb', line 44

def []=(key, value)
  super(transform_key(key), value)
end

#delete(key) ⇒ Object

Delete a header



72
73
74
# File 'lib/webmachine/headers.rb', line 72

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.

Overloads:

  • #fetch(key) ⇒ Object

    A key

    Parameters:

    • key (Object)
  • #fetch(key, default) ⇒ Object

    A key and a default value

    Parameters:

    • key (Object)
    • default (Object)
  • #fetch(key) {|key| ... } ⇒ Object

    A key and a code block

    Parameters:

    • (Object)

    Yields:

    • (key)

      Passes the key to the block

Returns:

  • (Object)

    the value for the key or the default



67
68
69
# File 'lib/webmachine/headers.rb', line 67

def fetch(*args, &block)
  super(transform_key(args.shift), *args, &block)
end

#grep(pattern) ⇒ Object

Select matching headers



77
78
79
# File 'lib/webmachine/headers.rb', line 77

def grep(pattern)
  self.class[select { |k, _| pattern === k }]
end