I'm using Ruby on Rails 4 and Grape.
I'd like my Grape Resources to take up a little space so that they are more readable by other developers.
In the last few days we have been integrating the Stripe API (as an example) and in the params do section of the Resources there are code blocks like this:
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
How can I make that params block go to another file (for example inside a file in the helpers folder) and the params block can be included?
I tried to do this with include API::V1::Helpers::my_helper
but I don't know how to insert the params block. Can someone help me, please?
You might use shared 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