如何通过 Grape API 获取路由 [英] How to get routes by Grape API

查看:25
本文介绍了如何通过 Grape API 获取路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 gem、grape 作为 api.我试图通过命令 rake gear: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 gear: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 获取路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆