Browse Source

Ping API basic support

Simon Mathieu 6 years ago
parent
commit
cf5285299c

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

@@ -0,0 +1 @@
+Dir.glob("#{File.dirname(__FILE__)}/ping/*").each { |file| require(file) }

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

@@ -0,0 +1,16 @@
+module ShopifyAPI
+  module Ping
+    class Conversation < Base
+      self.prefix = "/admin/api/ping-api/v1/"
+
+      def send_message(message_attrs)
+        message = ShopifyAPI::Ping::Message.new(
+          message_attrs.merge(conversation_id: self.id)
+        )
+
+        message.save
+        message
+      end
+    end
+  end
+end

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

@@ -0,0 +1,7 @@
+module ShopifyAPI
+  module Ping
+    class Message < Base
+      self.prefix = "/admin/api/ping-api/v1/conversations/:conversation_id/"
+    end
+  end
+end

+ 1 - 1
lib/shopify_api/version.rb

@@ -1,3 +1,3 @@
 module ShopifyAPI
-  VERSION = "4.12.0"
+  VERSION = "4.13.0"
 end

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

@@ -0,0 +1 @@
+{"conversation":{"id":"d315d4f7-53bd-49ec-8808-23f6db3c641a","name":"my topic","participants":{"counts":{"total":1},"data":[{"id":"test","name":"foo","avatar":null,"group":"customer"}]},"shopify_account":{"domain":"backpackinghacks.myshopify.com"}}}

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

@@ -0,0 +1 @@
+{"message":{"id":"d0c7a2e6-8084-4e79-8483-e4a1352b81f7","sender_id":"test","sender":{"id":"test","name":"foo","avatar":null,"group":"customer"},"content":{"text":"Hello from shopify_api"},"sent_at":"2018-08-29T22:16:05.589479Z"}}

+ 37 - 0
test/ping/conversation_test.rb

@@ -0,0 +1,37 @@
+require 'test_helper'
+
+class PingConversationTest < Test::Unit::TestCase
+  def test_create_conversation
+    fake "api/ping-api/v1/conversations", :method => :post, :body => load_fixture('ping/conversation')
+
+    conversation = ShopifyAPI::Ping::Conversation.new(
+      topic: 'my topic',
+      participants: [
+        {
+          name: 'foo',
+          id: 'test',
+          group: 'customer',
+        }
+      ]
+    )
+
+    conversation.save
+
+    assert_equal "d315d4f7-53bd-49ec-8808-23f6db3c641a", conversation.id
+  end
+
+  def test_send_message
+    fake "api/ping-api/v1/conversations/123/messages", :method => :post, :body => load_fixture('ping/message')
+
+    conversation = ShopifyAPI::Ping::Conversation.new(id: '123')
+    message = conversation.send_message(
+      dedupe_key: SecureRandom.uuid,
+      content: {
+        text: "Hello from shopify_api",
+      },
+      sender_id: 'test',
+    )
+
+    assert_equal "d0c7a2e6-8084-4e79-8483-e4a1352b81f7", message.id
+  end
+end