Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing compiled routes in Grape / Rack::Mount::Route

I'm trying to generate a list of all routes generated by my subclass of Grape::API (MyApi).

I can get close by calling:

MyApi.send(:route_set).instance_variable_get(:@routes)

which gives me an array of Rack::Mount::Route objects.

The only attribute of the Route object that is useful is :conditions which returns a Hash like this:

 :path_info => (?-mix:\\A\\/api\\/(?<version>v1)\\/token(?:\\.(?<format>[^\\/]+))?\\Z)", "k: request_method, v: (?-mix:\\AGET\\Z)

As you can see the value of the hash is a regexp for matching the route's path. I can also use :named_captures to get all the named captures from the regexp:

{:path_info=>{:version=>0, :format=>1}, :request_method=>{}}

Ultimately what I'm trying to do is generate a list of all routes created through Grape::API, their full path, etc. It doesn't make sense to me to try and deconstruct the regexp in conditions. Is there another way of accessing/generating a human readable path for Rack::Mount::Route?

like image 799
Josh Avatar asked Jun 11 '11 19:06

Josh


1 Answers

See this post rake routes with grape

basicaly you can get routes with:

MyApi.routes

UPDATE:

Article in English: rake routes command on grape gem

like image 169
Duke Avatar answered Sep 28 '22 00:09

Duke