-
-
Save abner/0768c18561620e4892d2 to your computer and use it in GitHub Desktop.
rake task for printing grape routes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = Kanban::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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :grape do | |
desc 'Print compiled grape routes' | |
task :routes => :environment do | |
API.routes.each do |route| | |
puts route | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment