如何在 rails 中使用嵌套的 JSON 编写 case 语句

How to write a case statement with nested JSON in rails

我有一个与 Easypost API 集成的应用程序(用于运送东西,真的很酷)。我正在使用他们的跟踪信息网络挂钩来触发我的应用程序中的事件。我正在使用 ruby case 语句来完成此操作。这是 Easypost 的 API 文档的 link:https://www.easypost.com/docs/api#webhooks

我想将我的 case 语句与 JSON 中的 result.status 键值相匹配。最初我只是使用描述,但我需要它更具体。看看:

{
"id": "evt_qatAiJDM",
"object": "Event",
"created_at": "2014-11-19T10:51:54Z",
"updated_at": "2014-11-19T10:51:54Z",
"description": "tracker.updated",
"mode": "test",
"previous_attributes": {
  "status": "unknown"
},
"pending_urls": [],
"completed_urls": [],
"result": {
  "id": "trk_Txyy1vaM",
  "object": "Tracker",
  "mode": "test",
  "tracking_code": "EZ4000000004",
  "status": "delivered",
  "created_at": "2014-11-18T10:51:54Z",
  "updated_at": "2014-11-18T10:51:54Z",
  "signed_by": "John Tester",
  "weight": 17.6,
  "est_delivery_date": "2014-08-27T00:00:00Z",
  "shipment_id": null,
  "carrier": "UPS",

这是我使用的代码

class HooksController < ApplicationController
# to bypass CSRF token authenticity error
skip_before_filter  :verify_authenticity_token

  def stripe
    #using easypost now as the webhook event
    case params[:description]
      # From EasyPost API https://www.easypost.com/docs/api#webhooks: 
      # tracker.updated: Fired when the status for the scan form changes
      when 'tracker.updated'
        @shipments = Spree::Shipment.where(transferred: false, state: "shipped")
        @shipments.each do |shipment|
          item_total = 0
          shipment.line_items.each do |item|
            item_total += item.product.price * item.quantity
          end  
          transfer = Stripe::Transfer.create(
            # Take 10% for ourselves from the total cost
            # of items per supplier(shipment)
            :amount => (item_total * (100 - shipment.supplier.commission_percentage)).floor,
            :currency => "usd",
            :recipient => shipment.supplier.token
          )
          shipment.transferred = true
          shipment.save!
        end
      end
  end
end

由于状态键嵌套在 JSON 中的一层,我是否需要在我的 webhook 代码中考虑到这一点?可能是 case params[:status]case params[:result :status]?

谢谢

更新

我现在意识到我想使用 EasyPost 的一些更详细的细节 API。我需要访问跟踪详细信息数组中的状态:

//Easypost API
{
"id": "evt_qatAiJDM",
"object": "Event",
"created_at": "2014-11-19T10:51:54Z",
"updated_at": "2014-11-19T10:51:54Z",
"description": "tracker.updated",
"mode": "test",
"previous_attributes": {
  "status": "unknown"
},
"pending_urls": [],
"completed_urls": [],
"result": {
  "id": "trk_Txyy1vaM",
  "object": "Tracker",
  "mode": "test",
  "tracking_code": "EZ4000000004",
  "status": "delivered",
  "created_at": "2014-11-18T10:51:54Z",
  "updated_at": "2014-11-18T10:51:54Z",
  "signed_by": "John Tester",
  "weight": 17.6,
  "est_delivery_date": "2014-08-27T00:00:00Z",
  "shipment_id": null,
  "carrier": "UPS",
  "tracking_details": [
    {
      "object": "TrackingDetail",
      "message": "BILLING INFORMATION RECEIVED",
      "status": "pre_transit",
      "datetime": "2014-08-21T14:24:00Z",
      "tracking_location": {
        "object": "TrackingLocation",
        "city": null,
        "state": null,
        "country": null,
        "zip": null
      }

所以我需要访问 result,然后是 tracking_details,然后是 status。这个案例陈述:case params[:result][:tracking_details][:status] 给了我这个错误:

    May 14 10:31:30 leemaeats app/web.1:  TypeError (no implicit conversion of Symbol into Integer): 
May 14 10:31:30 leemaeats app/web.1:    app/controllers/hooks_controller.rb:28:in `[]' 
May 14 10:31:30 leemaeats app/web.1:    app/controllers/hooks_controller.rb:28:in `stripe'

好像是因为我需要指定这个数组中的元素tracking_details。任何人都知道如何引用它? 谢谢

可能case params[:result][:status]

params[:result] returns 一个散列,其中包含您可以使用 [:status]

访问的 "status" 成员

编辑以回答更新

params[:result][:tracking_details] returns 一个数组,因此您可以尝试以下任一操作:

获取第一个元素:

  • params[:result][:tracking_details][0][:status]
  • params[:result][:tracking_details].first[:status]

获取所有元素:

  • params[:result][:tracking_details].map{|x| x[:status]}