class Rack::Metadata

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/rack/metadata.rb, line 4
def initialize(app, options = {})
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# 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.bytesize.to_s
        response = [body]
      end
    end
  end

  [status, headers, response]
end

Private Instance Methods

message(name, content) click to toggle source
# File lib/rack/metadata.rb, line 30
def message(name, content)
  %{<meta name="#{name}" content="#{content}" />}
end