本文介绍了如何通过Grape API获取路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用宝石,葡萄作原料药.我试图通过命令rake grape:routes
I use gem, grape for api.I tried to get api urls by the command rake grape:routes
namespace :grape do
desc "routes"
task :routes => :environment do
API::Root.routes.map { |route| puts "#{route} \n" }
end
end
但是我被rake grape:routes
#<Grape::Router::Route:0x007f9040d13878>
#<Grape::Router::Route:0x007f9040d13878>
#<Grape::Router::Route:0x007f9040d13878>
#<Grape::Router::Route:0x007f9040d13878>
...
我想要这样的东西.
version=v1, method=GET, path=/services(.:format)
version=v1, method=GET, path=/services/:id(.:format)
...
下面是我的葡萄实现.效果很好.
My grape implementation is below. This works well.
module API
class Root < Grape::API
version 'v1', using: :path
format :json
helpers Devise::Controllers::Helpers
mount API::Admin::Services
end
end
module API
class Services < Grape::API
resources :services do
resource ':service_id' do
...
end
end
end
end
推荐答案
尝试将以下内容添加到您的 Rakefile 中,如本 建议
Try adding the the below to your Rakefile as discussed in this proposal
desc "Print out routes"
task :routes => :environment do
API::Root.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
或
尝试以下提到的方法 此处
Try the below as mentioned here
desc "API Routes"
task :routes do
API::Root.routes.each do |api|
method = api.request_method.ljust(10)
path = api.path
puts "#{method} #{path}"
end
end
然后运行rake routes
还有一些宝石( grape_on_rails_routes & grape-raketasks ).您可能有兴趣看看它们.
Also there are couple of gems(grape_on_rails_routes & grape-raketasks) which are built for this purpose. You might be interested to have a look at them.
这篇关于如何通过Grape API获取路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!