How do I "list the routes" in Roda?

289 views
Skip to first unread message

Enrique Garcia Cota

unread,
Mar 6, 2015, 7:18:52 AM3/6/15
to ruby...@googlegroups.com
Hi there,

I am creating an API using roda. On its root point I would like to send a JSON containing a list of the verbs and the paths they admit - in order to facilitate discovery and documentation.

It would look similar to this:

{ "GET" : ["/",  "/users", "/users/:id"], "POST": ["/users"], "PATCH": ["/users/:id"] } 

In order to generate such json, I would like to "parse the structure of the Roda tree".

I have tried reading Roda's code, and I am not able to deduce how to do so from it. I was expecting some sort of tree-related names (child, parent - you know). But it seems that either this structure either does not exist, or is implicit (via closures and such), or I just missed it.

Is there a plugin or test which does such parsing, and I can use as an example? If not, could someone please suggest an implementation (something which puts values in the console is enough).

Thanks a lot

Enrique

Jeremy Evans

unread,
Mar 6, 2015, 10:29:18 AM3/6/15
to ruby...@googlegroups.com
The routes are not stored in a datastructure (the route block is just instance_execed), and technically the list of routes would be request specific, so getting a list of routes is not possible.  This is mentioned in the presentation linked from the documentation page on the website, but not yet in the README.  It should be added to the README, I'll add that to my todo list.

My recommendation to other people who want similar functionality is to keep track of the routes in another manner.  One simple way to do this is to have a comment above each route, and write a program to collect those comments.  Unfortunately, I don't have an existing example of that for you, but maybe if I get some time soon I'll be able to put together an example.

Thanks,
Jeremy

Michel Benevento

unread,
Mar 6, 2015, 2:48:25 PM3/6/15
to ruby...@googlegroups.com
Roda’s routes are executable, so they are a (dynamic) program much more than a (static) configuration. So you can’t parse the structure like you suggest, similarly you don't have a ‘rake routes’ type overview from Roda.

But I have never seen the kind of service description that you describe, is that a thing? And what purpose does it have?

Michel


--
You received this message because you are subscribed to the Google Groups "Roda" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-roda+...@googlegroups.com.
To post to this group, send email to ruby...@googlegroups.com.
Visit this group at http://groups.google.com/group/ruby-roda.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-roda/55a7b7ad-a590-4b23-8456-35cc191b571e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Enrique Garcia Cota

unread,
Mar 6, 2015, 7:43:18 PM3/6/15
to ruby...@googlegroups.com
Thanks for answering, I guess I will just skip this part then, it is not worth the effort if it can not be done automatically (easily).

> But I have never seen the kind of service description that you describe, is that a thing? And what purpose does it have?

I had this in a Sinatra-based project and it was very practical.

You are doing these curl calls to an API endpoint and suddenly you wanted to call a different one but you didn't remember the exact endpoint name - just curl to the root and get the list. No task-switching to look for the API documentation or anything.

Jeremy Evans

unread,
Mar 8, 2015, 10:15:33 PM3/8/15
to ruby...@googlegroups.com
On Friday, March 6, 2015 at 7:29:18 AM UTC-8, Jeremy Evans wrote:
My recommendation to other people who want similar functionality is to keep track of the routes in another manner.  One simple way to do this is to have a comment above each route, and write a program to collect those comments.  Unfortunately, I don't have an existing example of that for you, but maybe if I get some time soon I'll be able to put together an example.

I created a roda-route_list external plugin today that does this:  https://github.com/jeremyevans/roda-route_list

If anyone here wants to take a look, I'd definitely like some feedback.

Thanks,
Jeremy

Michel Benevento

unread,
Mar 11, 2015, 2:00:16 PM3/11/15
to ruby...@googlegroups.com
I have been playing with Treetop to parse the code and find the routes that way. It looks kind of interesting, but I’m not sure if it’s worth the trouble. It looks like it could be quite a bit of work for a comprehensive grammar.

Just an idea, anyone has experience with this?

Thanks,
Michel


--
You received this message because you are subscribed to the Google Groups "Roda" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-roda+...@googlegroups.com.
To post to this group, send email to ruby...@googlegroups.com.
Visit this group at http://groups.google.com/group/ruby-roda.

Jeremy Evans

unread,
Mar 11, 2015, 2:06:18 PM3/11/15
to ruby...@googlegroups.com
On Wednesday, March 11, 2015 at 11:00:16 AM UTC-7, Michel Benevento wrote:
I have been playing with Treetop to parse the code and find the routes that way. It looks kind of interesting, but I’m not sure if it’s worth the trouble. It looks like it could be quite a bit of work for a comprehensive grammar.

Just an idea, anyone has experience with this?

I don't think it's possible to try to introspect the routes, as the routing tree is just ruby code.  To do so correctly in the general case would basically be equivalent to solving the halting problem. It may be possible to use Treetop and have route parsing work with specific applications that use a subset features, though.

Thanks,
Jeremy
Reply all
Reply to author
Forward
0 new messages