Sha256: baf8fbf0096635d9b68085d0dba9b0253e3ce0ce814022cdb68a8f74fb8022ac

Contents?: true

Size: 881 Bytes

Versions: 20

Compression:

Stored size: 881 Bytes

Contents

# frozen_string_literal: true

module Aserto
  module Rails
    module Utils
      class << self
        # Finds the Rails route for the given path.
        # If the route is not found, returns nil.
        # If the route is found, returns a hash with the following keys:
        # :path
        # :action
        #
        # Eg:
        # route("/api/v1/users/1") => {
        #   :action => :GET,
        #   :path => "/api/v1/users/:id" }
        def route(request)
          return unless defined? ::Rails

          path = request.path_info
          route = ::Rails.application.routes.routes.to_a.find do |cr|
            cr.path.to_regexp.match?(path)
          end

          return unless route

          {
            path: route.path.spec.to_s.sub("(.:format)", ""),
            action: request.request_method.to_sym
          }
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
aserto-0.31.3 lib/aserto/rails/utils.rb
aserto-0.31.2 lib/aserto/rails/utils.rb
aserto-0.31.1 lib/aserto/rails/utils.rb
aserto-0.31.0 lib/aserto/rails/utils.rb
aserto-0.30.6 lib/aserto/rails/utils.rb
aserto-0.30.5 lib/aserto/rails/utils.rb
aserto-0.30.4 lib/aserto/rails/utils.rb
aserto-0.30.1 lib/aserto/rails/utils.rb
aserto-0.30.0 lib/aserto/rails/utils.rb
aserto-0.20.6 lib/aserto/rails/utils.rb
aserto-0.20.5 lib/aserto/rails/utils.rb
aserto-0.20.4 lib/aserto/rails/utils.rb
aserto-0.20.3 lib/aserto/rails/utils.rb
aserto-0.20.2 lib/aserto/rails/utils.rb
aserto-0.0.6 lib/aserto/rails/utils.rb
aserto-0.0.5 lib/aserto/rails/utils.rb
aserto-0.0.4 lib/aserto/rails/utils.rb
aserto-0.0.3 lib/aserto/rails/utils.rb
aserto-0.0.2 lib/aserto/rails/utils.rb
aserto-0.0.1 lib/aserto/rails/utils.rb