2015-11-08 46 views
2

这是一个忙碌的一周。我正在开发一个Rails项目,并且包含Grape以实现API葡萄API(swagger doc)'desc'的全局配置

API有两个部分

  • 无需身份验证(无头)
  • 验证需要

我安装的应用程序与所有工作...

  • 葡萄
  • 葡萄Sw 012
  • 葡萄扬鞭的Rails

对于声称头需要我用一些像这样的事情......

class ProfilesApi < Grape::API 

    resource :profiles do 

    desc 'List all profiles' do 
     headers Authorization: { 
       description: 'Validates identity through JWT provided in auth/login', 
       required: true 
       } 
    end 
    get do 
     present User.all, with: Presenters::ProfilePresenter 
    end 
    end 
end 

现在的问题是,我这描述了很多类似的安装API类的。

有没有一种方法可以使这种常见的(种类继承),所以我不需要用每个葡萄方法定义它。

desc 'List all profiles' do 
     headers Authorization: { 
       description: 'Validates identity through JWT provided in auth/login', 
       required: true 
       } 
    end 

在此先感谢,并希望你们享受周末。

回答

5

是的,有一种方法。我通过在class API中定义一个方法来实现这一点,以便它可以从API继承的所有内容中访问。喜欢的东西:

module Myapp 
    class API < Grape::API 
    def self.auth_headers 
     { Authorization: { description: 'Validates identity through JWT provided in auth/login',required: true}} 
    end 
    end 
end 

而且你访问它这样的:

desc "List all profiles", { 
    headers: Myapp::API.auth_headers 
} 

当然,还有就是更加的方式,但他们依赖于您的实现。

0

我想这可能是葡萄API事情的更新版本,我已经得到这个有工作:

desc 'My description text' do 
     headers Authorization: {description: "pass the access token as Bearer", required: true } 
    end