Sha256: 87caa3443f99978887fec0f1d7e17037a9a043c465df610ef4c14f94a9326daf

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

module Raygun::Middleware
  class JavascriptExceptionTracking
    def initialize(app)
      @app = app
    end

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

      # It's a html file, inject our JS
      if headers['Content-Type'] && headers['Content-Type'].include?('text/html')
        response = inject_javascript_to_response(response)
      end

      [status, headers, response]
    end

    def inject_javascript_to_response(response)
      if Raygun.configuration.js_api_key.present?
        if response.respond_to?('[]')
          response[0].gsub!('</head>', "#{js_tracker.head_html}</head>")
          response[0].gsub!('</body>', "#{js_tracker.body_html}</body>")
        end

        if response.respond_to?(:body) # Rack::BodyProxy
          body = response.body
          body.gsub!('</head>', "#{js_tracker.head_html}</head>")
          body.gsub!('</body>', "#{js_tracker.body_html}</body>")
        end
      end

      response
    end

    private
    def js_tracker
      @js_tracker = Raygun::JavaScriptTracker.new
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
raygun4ruby-4.0.2 lib/raygun/middleware/javascript_exception_tracking.rb
raygun4ruby-4.0.1 lib/raygun/middleware/javascript_exception_tracking.rb
raygun4ruby-4.0.0.pre lib/raygun/middleware/javascript_exception_tracking.rb