我正在開發一個控制器來接收一個 webhook 我想了解如何在控制器中重構這個 create 方法。引數幾乎總是重復的。我可以創建一個私人摩托車嗎?
module Webhooks
module Payments
class ConfirmationsController < Webhooks::BaseController
def create
confirmation_payment = {
customer_code: params[:event][:data][:bill][:customer][:code],
customer_name: params[:event][:data][:bill][:customer][:name],
customer_email: params[:event][:data][:bill][:customer][:email],
payment_company_id: params[:event][:data][:bill][:payment_profile [:payment_company][:id],
payment_company_name: params[:event][:data][:bill][:payment_profile][:payment_company][:name],
payment_profile_card_number_last_four: params[:event][:data][:bill][:payment_profile][:card_number_last_four],
payment_time: params[:event][:data][:bill][:updated_at]
}
end
end
end
end
uj5u.com熱心網友回復:
我會開始:
module Webhooks
module Payments
class ConfirmationsController < Webhooks::BaseController
def create
bill = params.dig(:event, :data, :bill)
payment = bill.fetch(:payment_profile)
customer = bill.fetch(:customer)
company = payment.fetch(:payment_company)
confirmation_payment = {
customer_code: customer[:code],
customer_name: customer[:name],
customer_email: customer[:email],
payment_company_id: company[:id],
payment_company_name: company[:name],
payment_profile_card_number_last_four: payment[:card_number_last_four],
payment_time: bill[:updated_at]
}
end
end
end
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/483821.html
