Skip to content

Instantly share code, notes, and snippets.

@oivoodoo
Created March 5, 2013 10:06
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save oivoodoo/5089237 to your computer and use it in GitHub Desktop.
Save oivoodoo/5089237 to your computer and use it in GitHub Desktop.
rake task for printing grape routes.
namespace :grape do
desc 'Print compiled grape routes'
task :routes => :environment do
API.routes.each do |route|
puts route
end
end
end
@pboling
Copy link

pboling commented Jul 9, 2014

In the style of rails routes... (nicely formatted)

namespace :grape do
  desc "Condensed API Routes"
  task :routes => :environment do
    mapped_prefix = '/whatevs' # where mounted in routes.rb
    format = "%46s  %3s %7s %50s %12s:  %s"
    API.routes.each do |grape_route|
      info = grape_route.instance_variable_get :@options
      puts format % [
        info[:description] ? info[:description][0..45] : '',
        info[:version],
        info[:method],
        mapped_prefix + info[:path],
        '# params: ' + info[:params].length.to_s,
        info[:params].first.inspect
      ]
      if info[:params].length > 1
        info[:params].each_with_index do |param_info, index|
          next if index == 0
          puts format % ['','','','','',param_info.inspect]
        end
      end
    end
  end
end

@lpil
Copy link

lpil commented Apr 17, 2015

Remove the env task dependency :)

namespace :grape do
  desc 'Print compiled grape routes'
  task :routes do
    API.routes.each do |route|
      puts route
    end
  end
end

@alexebird
Copy link

With dynamic column widths:

namespace :grape do
  desc "Grape API Routes"
  task :routes => :environment do
    mapped_prefix = '/api' # where mounted API in routes.rb
    params_str = ' params:'
    desc_limit = 45
    route_info = API.routes.map {|r| [r, r.instance_variable_get(:@options)] }
    max_desc_size     = route_info.map{|_,info| (info[:description] || '')[0..desc_limit].size }.max
    max_method_size   = route_info.map{|_,info| info[:method].size }.max
    max_version_size  = route_info.map{|_,info| info[:version].size }.max
    max_path_size     = route_info.map{|_,info| info[:path].sub(':version', info[:version]).size }.max
    max_params_digits = route_info.map{|_,info| info[:params].size.to_s.size }.max
    format_str = format(
      '%%%ds  %%%ds %%%ds %%%ds%%-%ds | %%%ds%%%ds  %%s',
      max_desc_size + 1,
      max_version_size,
      max_method_size,
      mapped_prefix.size,
      max_path_size,
      max_params_digits,
      params_str.size)

    route_info.each do |_,info|
      fields = [
        info[:description] ? info[:description][0..desc_limit] : '',
        info[:version],
        info[:method],
        mapped_prefix,
        info[:path].sub(':version', info[:version]),
        info[:params].size.to_s,
        params_str,
        info[:params].first.inspect,
      ]
      puts format(format_str, *fields)

      info[:params].drop(1).each do |param|
        puts format(format_str, *([''] * (fields.size-1)) + [param.inspect])
      end
    end
  end
end

@urkle
Copy link

urkle commented Dec 29, 2015

Scan through the routes table to find the mounted APIs.

namespace :grape do
  desc "Condensed API Routes"
  task :routes => :environment do
    format = "%46s  %3s %7s %50s %12s:  %s"

    Rails.application.routes.routes.map do |r|
      ActionDispatch::Routing::RouteWrapper.new(r)
    end.reject do |r|
      r.internal?
    end.select do |r|
      r.rack_app < Grape::API rescue false
    end.each do |r|
      mapped_prefix = r.path
      api_class = r.rack_app

      api_class.routes.each do |grape_route|
        info = grape_route.instance_variable_get :@options
        puts format % [
            info[:description] ? info[:description][0..45] : '',
            info[:version],
            info[:method],
            mapped_prefix + info[:path],
            '# params: ' + info[:params].length.to_s,
            info[:params].first.inspect
        ]
        if info[:params].length > 1
          info[:params].each_with_index do |param_info, index|
            next if index == 0
            puts format % ['','','','','',param_info.inspect]
          end
        end
      end
    end
  end
end

@syedmusamah
Copy link

Guys, I just published a gem to display grape routes in a rails-like way: https://github.com/syedmusamah/grape_on_rails_routes. Any feedback would be greatly appreciated!

@theunixbeard
Copy link

Great gem @syedmusamah. The above gists no longer work with the latest version of Grape but your gem is flawless. Seeing as this is the first google search result for pretty print grape routes I figured should chime in to save other searchers some time.

@ndeto
Copy link

ndeto commented Aug 22, 2020

Thanks for the gem @syedmusamah! Confirmation that it still works in 2020!

@syedmusamah
Copy link

Amazing! Glad its still helping people :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment