是否可以在Rails Grape资源中包含外部Params块?

问题描述 投票:0回答:1

我正在使用Ruby on Rails 4和Grape。

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

最近几天,我们一直在整合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 block转到另一个文件(例如,在helpers文件夹中的文件内部),并且可以包含params块?

我尝试使用include API::V1::Helpers::my_helper进行此操作,但我不知道如何插入params块。有人可以帮我吗?

ruby-on-rails ruby refactoring grape
1个回答
1
投票

您可能会使用共享参数

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
© www.soinside.com 2019 - 2024. All rights reserved.