Sha256: 700bf5995996c54c3e6f68b1f407e2419acf0cc8b9cea4c498f1fee1379636e4

Contents?: true

Size: 956 Bytes

Versions: 1

Compression:

Stored size: 956 Bytes

Contents

module SimpleAnalyticsRails::Middleware
  # This receives the request payload and injects the JavaScript snippet to the HTML.
  # It's injected just before the </head> element.
  class JavascriptInjection
    def initialize(app)
      @app = app
    end

    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

    private

    def inject_javascript_to_response(response)
      if SimpleAnalyticsRails.configuration.enabled? && response.respond_to?("[]")
        response[0]&.gsub!("</head>", "#{javascript_script.head_html}</head>")
        response[0]&.gsub!("</body>", "#{javascript_script.body_html}</body>")
      end

      response
    end

    def javascript_script
      @javascript_script ||= SimpleAnalyticsRails::JavascriptScript.new
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_analytics_rails-0.3.1 lib/simple_analytics_rails/middleware/javascript_injection.rb