簡體   English   中英

如何在rails的控制器中重構接收webhook

[英]how to refactor receive webhooks in controller in rails

我正在開發一個控制器來接收一個 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

我會開始:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM