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?

dB.
  • 4,700
  • 2
  • 46
  • 51
Josh
  • 621
  • 6
  • 10

6 Answers6

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

Duke
  • 3,226
  • 1
  • 18
  • 23
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).

Syed Usamah
  • 101
  • 4
  • 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 Review](/review/low-quality-posts/11712383) – RickyA Mar 21 '16 at 10:48
  • @RickyA thanks for the feedback;just updated the answer! – Syed Usamah Mar 21 '16 at 11:55
  • This method is better because it does not base on the project name, but searches through ObjectSpace for all the inheriting from Grape::API. – Aurel Branzeanu Mar 29 '16 at 11:28
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
operand
  • 433
  • 4
  • 11
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.

Bruno Antunes
  • 2,241
  • 4
  • 21
  • 35
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

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/.

dB.
  • 4,700
  • 2
  • 46
  • 51