Class: Lowfidelity::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/lowfidelity/middleware.rb

Constant Summary collapse

INJECT_RE =
%r{</body>}i

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



5
6
7
# File 'lib/lowfidelity/middleware.rb', line 5

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/lowfidelity/middleware.rb', line 9

def call(env)
  status, headers, body = @app.call(env)
  return [status, headers, body] unless inject?(env, status, headers)

  html = +""
  body.each { |chunk| html << chunk.to_s }
  body.close if body.respond_to?(:close)

  if html.match?(INJECT_RE)
    html = html.sub(INJECT_RE, Payload.html + "</body>")
    headers = headers.dup
    headers["Content-Length"] = html.bytesize.to_s
  end

  [status, headers, [html]]
end