Class: Webmachine::Headers

Inherits:
Hash
  • Object
show all
Defined in:
lib/webmachine/headers.rb

Overview

Case-insensitive Hash of Request headers

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Webmachine::Headers) [](key, value, ...) + (Webmachine::Headers) [](array) + (Webmachine::Headers) [](object)

Creates a new headers object populated with the given objects. It supports the same forms as Hash.[].

Overloads:

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

    Pairs of keys and values

    Parameters:

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

    Array of key-value pairs

    Parameters:

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

    Object convertible to a hash

    Parameters:

    • (Object)

Returns:



30
31
32
# File 'lib/webmachine/headers.rb', line 30

def self.[](*args)
  super(super(*args).map {|k, v| [k.to_s.downcase, v]})
end

+ (Webmachine::Headers) from_cgi(env)

Convert CGI-style Hash into Request headers

Parameters:

  • env (Hash)

    a hash of CGI-style env/headers

Returns:



7
8
9
10
11
12
13
14
# File 'lib/webmachine/headers.rb', line 7

def self.from_cgi(env)
  env.inject(new) do |h,(k,v)|
    if k =~ /^HTTP_(\w+)$/ || k =~ /^(CONTENT_(?:TYPE|LENGTH))$/
      h[$1.tr("_", "-")] = v
    end
    h
  end
end

Instance Method Details

- (Object) [](key)

Fetch a header



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

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

- (Object) []=(key, value)

Set a header



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

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

- (Object) delete(key)

Delete a header



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

def delete(key)
  super transform_key(key)
end

- (Object) fetch(key) - (Object) fetch(key, default) - (Object) fetch(key) {|key| ... }

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 exception; 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:

  • - (Object) fetch(key)

    A key

    Parameters:

    • key (Object)
  • - (Object) fetch(key, default)

    A key and a default value

    Parameters:

    • key (Object)
    • default (Object)
  • - (Object) fetch(key) {|key| ... }

    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



63
64
65
# File 'lib/webmachine/headers.rb', line 63

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

- (Object) grep(pattern)

Select matching headers



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

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