hetre85 hetre85 - 1 month ago 6x
Ruby Question

List All Routes in a Roda app

In Rails I view all the routes I have defined via:

rake routes

Is there a built in way to do this in a Roda app?

If not, is there a common pattern Roda developers use to quickly view all the routes (maybe a Rake task of some sorts?)


Automatically is not possible. roda routes are evaluated dynamically when a request comes in, so they are not loaded before and stored in a data structure available somewhere.

As the documentation says:

The route block is called whenever a new request comes in. It is yielded an instance of a subclass of Rack::Request with some additional methods for matching routes.

A simple solution, which needs a minimal effort, is to use the roda-route_list plugin, but it needs explanatory comment on the top of each route in your app.rb file, like this:

# route: /path/to/foo
# route: GET /path/to/foo
# ...

(check the documentation for other possibilities)

Then you have to create a json file containing the routes metadata, you can do this launching a script shipped with roda-route_list plugin

roda-parse_routes -f routes.json app.rb

It creates the file routes.json in the root of your app and finally you can list the routes with:

route_list # => [{:path=>'/path/to/foo', :methods=>['GET', 'POST']}]
# (it is an Array of route metadata hashes)

You could create also a simple rake task to list all routes, something like this:

# Rakefile
require 'roda'

namespace :routes do
  task :list do |task|
    class App < Roda
      plugin :route_list
      puts route_list

maybe there is a more elegant solution than this snippet, but it works :)

Hope it helps!