Accessing compiled routes in Grape / Rack::Mount::Route
Asked Answered
V

6

11

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?

Vd answered 11/6, 2011 at 19:34 Comment(0)
G
7

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

Gastrolith answered 2/4, 2013 at 23:18 Comment(2)
that link is down, and it's to a .xxx domain? HmmmMalcommalcontent
Yes is down @sardaukar, but you can acess here hermes-vertigem.herokuapp.com/2013/04/02/rake-routes-no-grape I fix on answer, ThankxGastrolith
T
3

If you are using grape with rails, check out the grape_on_rails_routes gem.

You can run rake grape:routes and see a nicely formatted list all current API's and their details (url, description, version).

Traumatism answered 21/3, 2016 at 10:21 Comment(3)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewCreamery
@Creamery thanks for the feedback;just updated the answer!Traumatism
This method is better because it does not base on the project name, but searches through ObjectSpace for all the inheriting from Grape::API.Jettiejettison
A
2

Just to add another variation to the answers here. I use the following two rake tasks.

task :all_routes => [:routes, :api_routes]

task :api_routes => :environment do
  longest_uri  = MyAPI.routes.map{|api|api.route_path.length}.max
  longest_desc = MyAPI.routes.map{|api|api.route_description.length}.max
  pattern = "%10s %-#{longest_uri}s %-#{longest_desc}s\n"

  # print column headers
  printf(
    pattern,
    "Verb", "URI Pattern", "Description"
  )

  # print each column
  MyAPI.routes.each do |api|
    printf(
      pattern,
      api.route_method, api.route_path, api.route_description
    )
  end
end
Antabuse answered 15/12, 2014 at 1:54 Comment(0)
M
1

How I did it:

desc "Print out routes"
task :routes => :environment do
  StudyTube::API::V1::Base.routes.each do |route|
    info = route.instance_variable_get :@options
    description = "%-40s..." % info[:description][0..39]
    method = "%-7s" % info[:method]
    puts "#{description}  #{method}#{info[:path]}"
  end
end

Prints them out nicely. I was futzing around with the truncation, but you can get rid of that if you want of course.

Malcommalcontent answered 26/2, 2014 at 8:27 Comment(0)
P
0

You can't do this. Once the route has been compiled it becomes something much less inspectable. Instead of reverse engineering it, see https://github.com/intridea/grape/pull/48/.

Papism answered 29/7, 2011 at 13:44 Comment(0)
L
0

I'd go with one of the custom rake tasks if not running Rails. If you are in fact using Rails, take a look at the grape-rails-routes gem.

It gives you a similar rake task rake routes_with_grape.

It's added value is however the html table view in the rails info section at http://localhost:3000/rails/info/routes_with_grape

Lampion answered 9/9, 2015 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.