Class: SimpleAnalyticsRails::Middleware::JavascriptInjection

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

Overview

This receives the request payload and injects the JavaScript snippet to the HTML. It’s injected just before the </head> element.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ JavascriptInjection

Returns a new instance of JavascriptInjection.



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

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/simple_analytics_rails/middleware/javascript_injection.rb', line 9

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

  if headers && headers["Content-Type"]&.include?("text/html")
    response = inject_javascript_to_response(response)
  end

  [status, headers, response]
end