Skip to content

Instantly share code, notes, and snippets.

@sstelfox
Last active December 15, 2015 19:49
Show Gist options
  • Save sstelfox/5314342 to your computer and use it in GitHub Desktop.
Save sstelfox/5314342 to your computer and use it in GitHub Desktop.
This is my version of the Rails rake routes task for Sinatra. I've found this to be endlessly useful in debugging route ordering.
require 'sinatra_app'
# This comes from the sinatra-contrib gem
require 'sinatra/decompile'
# This will print all of the configured routes for the sinatra application in
# the order that the route was first specified. All of the methods will be
# combined together and it will format the output into neat columns. Very useful
# for debugging when an incorrect route is being hit.
desc "List all of the routes configured"
task :routes => [:environment] do
rl = Default::App.routes.each_with_object(Hash.new([])) do |verb, route_listing|
verb[1].each do |route|
route_listing[route[0].source] += [verb[0]]
end
end
longest_column = rl.values.map { |v| v.join(", ").length }.max
rl.each_pair do |route, methods|
printf("%-#{longest_column}s %s\n", methods.sort.join(", "), Sinatra::Decompile.decompile(route))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment