Browse Source

Add delivery confirmation endpoint to Ping resources. (#514)

* Add delivery confirmation endpoint to shopify_api gem.
brownianmover 6 years ago
parent
commit
8b570715b5

+ 27 - 0
lib/shopify_api/resources/ping/conversation.rb

@@ -13,6 +13,33 @@ module ShopifyAPI
         message.save
         message
       end
+
+      def successful_delivery(message_id:, delivery_timestamp:)
+        delivery_details = ShopifyAPI::Ping::DeliveryConfirmation.new(
+          conversation_id: id,
+          message_id: message_id,
+          delivery_confirmation_details: {
+            delivered: true,
+            confirmation_timestamp: delivery_timestamp,
+          }
+        )
+        delivery_details.save
+        delivery_details
+      end
+
+      def failed_delivery(message_id:, delivery_timestamp:, details:)
+        delivery_details = ShopifyAPI::Ping::DeliveryConfirmation.new(
+          conversation_id: id,
+          message_id: message_id,
+          delivery_confirmation_details: {
+            delivered: false,
+            confirmation_timestamp: delivery_timestamp,
+            details: details,
+          }
+        )
+        delivery_details.save
+        delivery_details
+      end
     end
   end
 end

+ 10 - 0
lib/shopify_api/resources/ping/delivery_confirmation.rb

@@ -0,0 +1,10 @@
+# frozen_string_literal: true
+
+module ShopifyAPI
+  module Ping
+    class DeliveryConfirmation < Base
+      self.prefix = "/admin/api/ping-api/v1/conversations/:conversation_id/messages/:message_id/"
+      self.collection_name = "delivery_confirmation"
+    end
+  end
+end

+ 0 - 1
lib/shopify_api/resources/ping/message.rb

@@ -1,5 +1,4 @@
 # frozen_string_literal: true
-
 module ShopifyAPI
   module Ping
     class Message < Base

+ 1 - 0
test/fixtures/ping/failed_delivery_confirmation.json

@@ -0,0 +1 @@
+{"delivery_confirmation_details":{"delivery_timestamp":"2018-08-29T22:16:05.589479Z","delivered":"false","details":"Integration failed to deliver message."}}

+ 1 - 0
test/fixtures/ping/successful_delivery_confirmation.json

@@ -0,0 +1 @@
+{"delivery_confirmation_details":{"delivery_timestamp":"2018-08-29T22:16:05.589479Z","delivered":"true"}}

+ 32 - 0
test/ping/conversation_test.rb

@@ -36,4 +36,36 @@ class PingConversationTest < Test::Unit::TestCase
 
     assert_equal "d0c7a2e6-8084-4e79-8483-e4a1352b81f7", message.id
   end
+
+  def test_successful_delivery
+    fake("api/ping-api/v1/conversations/123/messages/111/delivery_confirmation",
+      method: :post,
+      body: load_fixture('ping/successful_delivery_confirmation'))
+
+    conversation = ShopifyAPI::Ping::Conversation.new(id: '123')
+    delivery_confirmation = conversation.successful_delivery(
+      message_id: '111',
+      delivery_timestamp: "2018-08-29T22:16:05.589479Z"
+    )
+
+    assert_equal("true", delivery_confirmation.delivered)
+    assert_equal("2018-08-29T22:16:05.589479Z", delivery_confirmation.delivery_timestamp)
+  end
+
+  def test_failed_delivery
+    fake("api/ping-api/v1/conversations/123/messages/111/delivery_confirmation",
+      method: :post,
+      body: load_fixture('ping/failed_delivery_confirmation'))
+
+    conversation = ShopifyAPI::Ping::Conversation.new(id: '123')
+    delivery_confirmation = conversation.failed_delivery(
+      message_id: '111',
+      delivery_timestamp: Time.now.to_s,
+      details: "Integration failed to deliver message."
+    )
+
+    assert_equal("false", delivery_confirmation.delivered)
+    assert_equal("2018-08-29T22:16:05.589479Z", delivery_confirmation.delivery_timestamp)
+    assert_equal("Integration failed to deliver message.", delivery_confirmation.details)
+  end
 end

+ 23 - 0
test/ping/message_test.rb

@@ -0,0 +1,23 @@
+# frozen_string_literal: true
+
+require 'test_helper'
+
+class PingMessageTest < Test::Unit::TestCase
+  def test_create_message
+    fake("api/ping-api/v1/conversations/123/messages",
+      method: :post,
+      body: load_fixture('ping/message'))
+
+    message = ShopifyAPI::Ping::Message.new(
+      dedupe_key: SecureRandom.uuid,
+      content: {
+        text: "Hello from shopify_api",
+      },
+      sender_id: 'test',
+      conversation_id: '123',
+    )
+
+    message.save
+    assert_equal("d0c7a2e6-8084-4e79-8483-e4a1352b81f7", message.id)
+  end
+end