2017-06-20 85 views
1

我使用寶石,葡萄爲api。 我試圖通過命令rake grape:routes如何通過葡萄API獲得路線

namespace :grape do 
     desc "routes" 
     task :routes => :environment do 
     API::Root.routes.map { |route| puts "#{route} \n" } 
     end 
    end 

獲得API網址,但我得到了由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) 
    ... 

我的葡萄實現如下。這很好。

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 

回答

4

嘗試添加以下到您Rake文件在本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 

或者

討論嘗試以下提到here

desc "API Routes" 
task :routes do 
    API::Root.routes.each do |api| 
    method = api.route_method.ljust(10) 
    path = api.route_path 
    puts "#{method} #{path}" 
    end 
end 

和運行rake routes

還有一些夫婦被用於此目的的內置寶石(grape_on_rails_routes & grape-raketasks)的。你可能有興趣看看他們。

+0

謝謝。它運作良好! –