Class: NPlusInsight::Middleware

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



3
# File 'lib/n_plus_insight/middleware.rb', line 3

def initialize(app) = @app = app

Instance Method Details

#call(env) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/n_plus_insight/middleware.rb', line 5

def call(env)
  previous = Current.collector
  config = NPlusInsight.configuration
  return @app.call(env) unless config.enabled
  return @app.call(env) if env["PATH_INFO"].to_s.start_with?(config.mount_path)
  return @app.call(env) if config.ignore_paths.any? { |pattern| pattern.match?(env["PATH_INFO"].to_s) }

  queries = []
  Current.collector = queries
  status, headers, body = @app.call(env)

  if injectable_html?(headers, config)
    chunks = []
    body.each { |chunk| chunks << chunk }
    body.close if body.respond_to?(:close)
    detections = analyze(env, queries, config)
    html = chunks.join
    html = inject(html, Overlay.render(detections))
    headers.delete("Content-Length")
    headers.delete("content-length")
    headers["Content-Length"] = html.bytesize.to_s
    [status, headers, [html]]
  else
    analyze(env, queries, config)
    [status, headers, body]
  end
ensure
  Current.collector = previous
end