Class: Rack::Utils::EnvironmentHeaders

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rack/utils/environment_headers.rb

Overview

A facade over a Rack Environment Hash that gives access to headers using their normal RFC 2616 names.

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ EnvironmentHeaders

Create the facade over the given Rack Environment Hash.



11
12
13
# File 'lib/rack/utils/environment_headers.rb', line 11

def initialize(env)
  @env = env
end

Instance Method Details

#[](header_name) ⇒ Object

Return the value of the specified header. The header_name should be as specified by RFC 2616 (e.g., “Content-Type”, “Accept”, etc.)



17
18
19
# File 'lib/rack/utils/environment_headers.rb', line 17

def [](header_name)
  @env[env_name(header_name)]
end

#[]=(header_name, value) ⇒ Object

Set the value of the specified header. The header_name should be as specified by RFC 2616 (e.g., “Content-Type”, “Accept”, etc.)



23
24
25
# File 'lib/rack/utils/environment_headers.rb', line 23

def []=(header_name, value)
  @env[env_name(header_name)] = value
end

#delete(header_name) ⇒ Object

Delete the entry in the underlying Rack Environment that corresponds to the given RFC 2616 header name.



45
46
47
# File 'lib/rack/utils/environment_headers.rb', line 45

def delete(header_name)
  @env.delete(env_name(header_name))
end

#eachObject

Iterate over all headers yielding a (name, value) tuple to the block. Rack Environment keys that do not map to an header are not included.



36
37
38
39
40
41
# File 'lib/rack/utils/environment_headers.rb', line 36

def each
  @env.each do |key,value|
    next unless key =~ /^(HTTP_|CONTENT_)/
    yield header_name(key), value
  end
end

#include?(header_name) ⇒ Boolean

Determine if the underlying Rack Environment includes a header of the given name.

Returns:

  • (Boolean)


29
30
31
# File 'lib/rack/utils/environment_headers.rb', line 29

def include?(header_name)
  @env.include?(env_name(header_name))
end

#to_envObject Also known as: to_hash

Return the underlying Rack Environment Hash.



50
51
52
# File 'lib/rack/utils/environment_headers.rb', line 50

def to_env
  @env
end