如何在rails

时间:2015-05-14 02:25:56

标签: ruby-on-rails json webhooks easypost

我有一个与Easypost API集成的应用程序(用于运送东西,非常酷)。我正在使用他们的跟踪信息webhooks在我的应用中触发事件。我使用ruby case语句来完成此任务。这是指向Easypost的API文档的链接: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中的元素。有谁知道如何参考? 感谢

1 个答案:

答案 0 :(得分:1)

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

params[:result]会返回一个哈希,其中包含您可以使用"status"

访问的[:status]成员。

编辑以回答更新

params[:result][:tracking_details]返回一个数组,因此您可以尝试以下任何一种方法:

获取第一个元素:

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

获取所有元素:

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