Class: Rack::Metadata

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

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Metadata

Returns a new instance of Metadata.



4
5
6
# File 'lib/rack/metadata.rb', line 4

def initialize(app, options = {})
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rack/metadata.rb', line 8

def call(env)
  env['rack.metadata'] = {}
  status, headers, response = @app.call(env)

  if headers['Content-Type'].to_s.include?('text/html') # Only update HTML bodies
    if env['rack.metadata']
      body = ""
      response.each { |part| body << part }
      index = body.rindex("</head>")
      if index
        body.insert(index, env['rack.metadata'].map{|k,v| message(k,v)}.join(""))
        headers["Content-Length"] = body.length.to_s
        response = [body]
      end
    end
  end

  [status, headers, response]
end