Sha256: cd3b643928e1ea3bc47ef47fb069c26e2e77147bdf79fd919bda667123b0db12

Contents?: true

Size: 1.23 KB

Versions: 20

Compression:

Stored size: 1.23 KB

Contents

module Airbrake
  module Rack
    # Adds route slugs to context/route.
    # @since v7.5.0
    class RouteFilter
      attr_reader :weight

      def initialize
        @weight = 100
      end

      def call(notice)
        return unless (request = notice.stash[:rack_request])

        notice[:context][:route] =
          if defined?(ActionDispatch::Request) &&
             request.instance_of?(ActionDispatch::Request)
            rails_route(request)
          elsif defined?(Sinatra::Request) &&
                request.instance_of?(Sinatra::Request)
            sinatra_route(request)
          end
      end

      private

      def rails_route(request)
        ::Rails.application.routes.router.recognize(request) do |route, _parameters|
          # Rails can recognize multiple routes for the given request. For
          # example, if we visit /users/2/edit, then Rails sees these routes:
          #   * "/users/:id/edit(.:format)"
          #   *  "/"
          #
          # We return the first route as, what it seems, the most optimal
          # approach.
          return route.path.spec.to_s
        end
      end

      def sinatra_route(request)
        request.env['sinatra.route'].split(' ').drop(1).join(' ')
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
airbrake-8.3.2 lib/airbrake/rack/route_filter.rb
airbrake-8.3.1 lib/airbrake/rack/route_filter.rb
airbrake-8.3.0 lib/airbrake/rack/route_filter.rb
airbrake-8.2.1 lib/airbrake/rack/route_filter.rb
airbrake-8.2.0 lib/airbrake/rack/route_filter.rb
airbrake-8.1.4 lib/airbrake/rack/route_filter.rb
airbrake-8.1.3 lib/airbrake/rack/route_filter.rb
airbrake-8.1.2 lib/airbrake/rack/route_filter.rb
airbrake-8.1.1 lib/airbrake/rack/route_filter.rb
airbrake-8.1.0 lib/airbrake/rack/route_filter.rb
airbrake-8.0.1 lib/airbrake/rack/route_filter.rb
airbrake-8.0.0 lib/airbrake/rack/route_filter.rb
airbrake-8.0.0.rc.9 lib/airbrake/rack/route_filter.rb
airbrake-8.0.0.rc.8 lib/airbrake/rack/route_filter.rb
airbrake-8.0.0.rc.7 lib/airbrake/rack/route_filter.rb
airbrake-8.0.0.rc.6 lib/airbrake/rack/route_filter.rb
airbrake-8.0.0.rc.5 lib/airbrake/rack/route_filter.rb
airbrake-8.0.0.rc.3 lib/airbrake/rack/route_filter.rb
airbrake-8.0.0.rc.2 lib/airbrake/rack/route_filter.rb
airbrake-7.5.0.pre.1 lib/airbrake/rack/route_filter.rb