Class: Rack::Header::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/header/context.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, headers = {}) ⇒ Context

Returns a new instance of Context.



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/rack/header/context.rb', line 3

def initialize(app, headers = {})
  @app = app

  # Map HTTP_* environment variables to response headers
  @headers = Hash[*ENV.select {|k,v| k.start_with? 'HTTP_'}
                      .collect {|k,v| [k.sub(/\AHTTP_/, ''), v]}
                      .collect {|k,v| [k.split('_').collect(&:capitalize).join('-'), v]}
                      .flatten]

  @headers = headers.merge(@headers)
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/rack/header/context.rb', line 15

def call(env)
  status, headers, response = @app.call(env)

  @headers.each do |k,v|
    headers[k] = v
  end
  headers.delete_if { |k,v| v.nil? }

  [status, headers, response]
end