How to write a case statement with nested JSON in rails -


i have app integrated easypost api (for shipping stuff, cool). i'm using tracking information webhooks fire events in app. i'm using ruby case statements accomplish this. here's link easypost's api doc: https://www.easypost.com/docs/api#webhooks

i match case statement result.status key value in json. using description, need more specific. take look:

{ "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", 

here's code using

class hookscontroller < applicationcontroller # bypass csrf token authenticity error skip_before_filter  :verify_authenticity_token    def stripe     #using easypost webhook event     case params[:description]       # easypost api https://www.easypost.com/docs/api#webhooks:        # tracker.updated: fired when status scan form changes       when 'tracker.updated'         @shipments = spree::shipment.where(transferred: false, state: "shipped")         @shipments.each |shipment|           item_total = 0           shipment.line_items.each |item|             item_total += item.product.price * item.quantity           end             transfer = stripe::transfer.create(             # take 10% ourselves 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 

since status key nested 1 level in json need account in webhook code? might case params[:status] or case params[:result :status]?

thanks

update

i realize want use more granular details of easypost api. need access status within array of tracking details:

//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       } 

so need access result , tracking_details , status. case statement: case params[:result][:tracking_details][:status] gave me error:

    may 14 10:31:30 leemaeats app/web.1:  typeerror (no implicit conversion of symbol 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' 

seems it's because need specify element in array tracking_details. know how reference that? thanks

probably case params[:result][:status]

params[:result] returns hash contains, among other things, "status" member can access [:status]

edit answer update

params[:result][:tracking_details] returns array, try of following:

to first element:

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

to elements:

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

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -