Class: Polychrest::Cure

Inherits:
Object
  • Object
show all
Defined in:
lib/polychrest/cure.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Cure

Returns a new instance of Cure.



7
8
9
# File 'lib/polychrest/cure.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
26
27
28
29
30
31
# File 'lib/polychrest/cure.rb', line 11

def call(env)
  status, headers, response = @app.call(env)
  
  if headers["Content-Type"] =~ /text\/html|application\/xhtml\+xml/ && ENV.has_key?('TRACK_JS_ERRORS')
    body = ""
    response.each { |part| body << part }
    doc = Nokogiri::HTML(body)
    
    head = doc.at_css("head") 
    script = Nokogiri::XML::Node.new('script', doc)
    script.content = Polychrest::Ingredients.active
    head.add_child(script)
    
    body = doc.to_html
    
    headers["Content-Length"] = body.length.to_s
    response = [body]
  end
  
  [status, headers, response]
end