我有一个与 Easypost API 集成的应用程序(用于运送东西,真的很酷)。我正在使用他们的跟踪信息 webhook 在我的应用程序中触发事件。我正在使用 rubycase
语句来完成此操作。这是 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
。有人知道如何参考吗?谢谢