有没有办法在 Rails Grape 资源中包含外部参数块?

Is there a way to include an external Params block in a Rails Grape resource?

我在 Rails 4 和 Grape 上使用 Ruby。

我希望我的 Grape Resources 占用一点空间 space 以便其他开发人员更容易阅读它们。

最近几天我们一直在集成 Stripe API(作为示例) 在资源的 params do 部分中,有这样的代码块:

desc 'Add bank account' do
    headers API::V1::Defaults.xxxxxxx
    success API::V1::Entities::xxxxxxx
end

params do
  requires :external_account, type: Hash, allow_blank: false, desc: 'Bank account nested data' do
    requires :bank_account, type: Hash, allow_blank: false, desc: 'Bank account nested data' do
      requires :id, type: String, desc: 'Stripe token for bank account'
      requires :account_holder_name, type: String, desc: 'Bank account holder name'
      requires :account_holder_type, type: String, desc: 'Bank account holder type [individual or company]'
      optional :bank_name, type: String, desc: 'Bank name'
      requires :country, type: String, desc: 'Bank account country'
      optional :currency, type: String, desc: 'Bank account currency'
      requires :routing_number, type: String, desc: 'Bank account routing number'
      requires :name, type: String, desc: 'Bank account holders name'
      requires :status, type: String, desc: 'Bank account status'
      requires :last4, type: Integer,
                  desc: 'Account holder ID number.'
    end
    requires :client_ip, type: String, desc: 'IP address of user for Stripe service agreement'
  end
  requires :email, type: String, desc: 'Users email'
  requires :business_type, type: String, desc: 'Individual or Company'
  requires :tos_acceptance, type: Hash, allow_blank: false, desc: 'Type of Service' do
    requires :date, type: Integer, desc: 'ToS [date]'
    requires :ip, type: String, desc: 'ToS [ip]'
  end
  optional :individual, type: Hash do
    requires :first_name, type: String, desc: 'Individuals [first name]'
    requires :last_name, type: String, desc: 'Individuals [last name]'
    requires :ssn_last_4, type: String, desc: 'Individuals SSN number'
    optional :dob, type: Hash do
      requires :day, type: String, desc: 'Individuals date of birth [day]'
      requires :month, type: String, desc: 'Individuals date of birth [month]'
      requires :year, type: String, desc: 'Individuals date of birth [year]'
    end
  end
  optional :company, type: Hash do
    requires :name, type: String, desc: 'Company [first name]'
    requires :email, type: String, desc: 'Company [email]'
    requires :phone, type: String, desc: 'Company [phone]'
  end
end

# ...
oauth2
post '/' do
   # ...
end

如何使 params 块 转到另一个文件(例如在 helpers 文件夹中的文件中)并包含 params 块?

我尝试用 include API::V1::Helpers::my_helper 来做到这一点,但我不知道如何插入 params 块。有人可以帮帮我吗?

您可以使用共享参数

module SharedParams
  extend Grape::API::Helpers

  params :pagination do
    optional :page, type: Integer
    optional :per_page, type: Integer
  end
end

class API < Grape::API
  helpers SharedParams

  desc 'Get collection.'
  params do
    use :pagination
  end

  get do
    # your logic here
  end
end