Class: Revelio::Middleware

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



8
9
10
# File 'lib/revelio/middleware.rb', line 8

def initialize(app)
  @app = app
end

Class Method Details

.devtools_scriptObject



107
108
109
110
# File 'lib/revelio/middleware.rb', line 107

def self.devtools_script
  js = File.read(File.expand_path("overlay.js", __dir__))
  "<script id=\"revelio\">\n#{js}\n</script>"
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/revelio/middleware.rb', line 12

def call(env)
  ensure_installed!

  status = nil
  headers = nil
  response = nil

  metrics = collect_metrics do
    status, headers, response = @app.call(env)
  end

  return [status, headers, response] unless injectable?(status, headers)

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

  if body.include?("</body>")
    body.sub!("</body>", "#{devtools_injection(metrics)}\n</body>")
    headers["content-length"] = body.bytesize.to_s if headers["content-length"]
  end

  [status, headers, [body]]
end