rails 如何在控制器中重构接收 webhook
how to refactor receive webhooks in controller in rails
我正在开发一个控制器来接收 webhooks 我想了解如何在控制器中重构这个创建方法。参数几乎总是重复的。我可以创建一个私人 moto 吗?
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
我会从:
开始
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
我正在开发一个控制器来接收 webhooks 我想了解如何在控制器中重构这个创建方法。参数几乎总是重复的。我可以创建一个私人 moto 吗?
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
我会从:
开始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