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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With