module Roda::RodaPlugins::RouteList

The route_list plugin reads route information from a json file, and then makes the route metadata available for introspection. This provides a workaround to the general issue of routing trees being unable to introspect the routes.

This plugin assumes that a json file containing the routes metadata has already been created. The recommended way to create one is to add comments above each route in the Roda app, in one of the following formats:

# route: /path/to/foo
# route: GET /path/to/foo
# route: GET|POST /path/to/foo/:foo_id
# route[route_name]: /path/to/foo
# route[route_name]: GET /path/to/foo
# route[foo]: GET|POST /path/to/foo/:foo_id

As you can see, the general style is a comment followed by the word route. If you want to name the route, you can put the name in brackets. Then you have a colon. Optionally after that you can have the method for the route, or multiple methods separated by pipes if the path works with multiple methods. The end is the path for the route.

Assuming you have added the appropriate comments as explained above, you can create the json file using the roda-route_parser executable that came with the roda-route_list gem:

roda-route_parser -f routes.json app.rb

Assuming you have the necessary json file created, you can then get route information:

plugin :route_list

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

# path for the route with the given name
listed_route(:route_name) # => '/path/to/foo'

# path for the route with the given name, supplying hash for placeholders
listed_route(:foo, :foo_id=>3) # => '/path/to/foo/3'

# path for the route with the given name, supplying array for placeholders
listed_route(:foo, [3]) # => '/path/to/foo/3'

The listed_route method is also available at the instance level to make it easier to use inside the route block.

Public Class Methods

configure(app, opts={}) click to toggle source

Set the file to load the routes metadata from. Options:

:file

The JSON file containing the routes metadata (default: 'routes.json')

   # File lib/roda/plugins/route_list.rb
57 def self.configure(app, opts={})
58   file = File.expand_path(opts.fetch(:file, 'routes.json'), app.opts[:root])
59   app.send(:load_routes, file)
60 end