Class: SmokeDetector::JavaScriptMonitors

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

Constant Summary collapse

TARGET_TAG =
'<head>'
ACCEPTABLE_CONTENT =
/text\/html|application\/xhtml\+xml/

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ JavaScriptMonitors

Returns a new instance of JavaScriptMonitors.



7
8
9
# File 'lib/smoke_detector/middleware/javascript_monitors.rb', line 7

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  status, headers, response = @app.call(env)

  if monitor?(headers)
    body = ''
    response.each { |part| body << part }
    if index = body.rindex(TARGET_TAG)
      body.insert(index + TARGET_TAG.length + 1, monitoring_code)
      headers["Content-Length"] = body.length.to_s
      response = [body]
    end
  end

  [status, headers, response]
end