소스 검색

Merge pull request #463 from Shopify/ping-api

Ping API basic support
Simon Mathieu 6 년 전
부모
커밋
7c82c73eed

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

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

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

@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+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: id)
+        )
+
+        message.save
+        message
+      end
+    end
+  end
+end

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

@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+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"}}

+ 39 - 0
test/ping/conversation_test.rb

@@ -0,0 +1,39 @@
+# frozen_string_literal: true
+
+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

+ 7 - 5
test/test_helper.rb

@@ -30,11 +30,13 @@ class Test::Unit::TestCase < Minitest::Unit::TestCase
 
   def setup
     ActiveResource::Base.format = :json
-    ShopifyAPI.constants.each do |const|
-      begin
-        const = "ShopifyAPI::#{const}".constantize
-        const.format = :json if const.respond_to?(:format=)
-      rescue NameError
+    [ShopifyAPI, ShopifyAPI::Ping].each do |mod|
+      mod.constants.each do |const|
+        begin
+          const = mod.const_get(const)
+          const.format = :json if const.respond_to?(:format=)
+        rescue NameError
+        end
       end
     end