Skip to content

Instantly share code, notes, and snippets.

@awesome
Forked from bantic/rails_route_recognizer.rb
Created December 10, 2016 05:33
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save awesome/488b0a9d62c427b982af9454c09245bc to your computer and use it in GitHub Desktop.
Programmatically list all routes (/paths) from within a rails app.
class RouteRecognizer
attr_reader :paths
# To use this inside your app, call:
# `RouteRecognizer.new.initial_path_segments`
# This returns an array, e.g.: ['assets','blog','team','faq','users']
INITIAL_SEGMENT_REGEX = %r{^\/([^\/\(:]+)}
def initialize
routes = Rails.application.routes.routes
@paths = routes.collect {|r| r.path.spec.to_s }
end
def initial_path_segments
@initial_path_segments ||= begin
paths.collect {|path| match_initial_path_segment(path)}.compact.uniq
end
end
def match_initial_path_segment(path)
if match = INITIAL_SEGMENT_REGEX.match(path)
match[1]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment