Class: AddHttpHeader

Inherits:
Object
  • Object
show all
Defined in:
lib/add_http_header.rb,
lib/add_http_header/version.rb

Overview

Rack middleware to add arbitrary HTTP headers to responses.

Constant Summary collapse

VERSION =
'1.0.4'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, headers) ⇒ AddHttpHeader

Initialize with the headers to add. Headers should be a hash. The headers are added after the request is processed. The values of the headers hash can be Proc objects in which case they are called with the request environment, response status, and response headers at runtime. In this way, you can add dynamic values as headers.



10
11
12
13
# File 'lib/add_http_header.rb', line 10

def initialize (app, headers)
  @app = app
  @headers = headers
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/add_http_header.rb', line 15

def call (env)
  status, headers, body = @app.call(env)
  @headers.each_pair do |name, value|
    value = value.call(env, status, headers) if value.is_a?(Proc)
    headers[name.to_s] = value.to_s unless value.blank?
  end
  return [status, headers, body]
end