Parcourir la source

Added support for Discounts (Shopify Plus)

Tyler King il y a 9 ans
Parent
commit
52b4faa077

+ 11 - 0
lib/shopify_api/resources/discount.rb

@@ -0,0 +1,11 @@
+module ShopifyAPI
+  class Discount < Base
+    def disable
+      load_attributes_from_response(post(:disable))
+    end
+    
+    def enable
+      load_attributes_from_response(post(:enable))
+    end
+  end
+end

+ 52 - 0
test/discount_test.rb

@@ -0,0 +1,52 @@
+require 'test_helper'
+
+class DiscountTest < Test::Unit::TestCase
+  test 'get should get a discount' do
+    fake 'discounts/680866', method: :get, status: 200, body: load_fixture('discount')
+    
+    discount = ShopifyAPI::Discount.find(680866)
+    assert_equal 680866, discount.id
+  end
+  
+  test 'get should get all discounts' do
+    fake 'discounts', method: :get, status: 200, body: load_fixture('discounts')
+    
+    discounts = ShopifyAPI::Discount.all
+    assert_equal 'TENOFF', discounts.first.code
+  end
+  
+  test 'create should create a discount' do
+    fake 'discounts', method: :post, status: 201, body: load_fixture('discount')
+    
+    discount = ShopifyAPI::Discount.create(code: 'TENOFF', discount_type: 'percentage')
+    assert_equal 'TENOFF', discount.code
+  end
+  
+  test 'should disable discount' do
+    fake 'discounts/680866', method: :get, status: 200, body: load_fixture('discount')
+    fake 'discounts/680866/disable', method: :post, status: 201, body: load_fixture('discount_disabled')
+    
+    discount = ShopifyAPI::Discount.find(680866)
+    discount.disable
+    
+    assert_equal "disabled", discount.status
+  end
+  
+  test 'should enable discount' do
+    fake 'discounts/680866', method: :get, status: 200, body: load_fixture('discount')
+    fake 'discounts/680866/enable', method: :post, status: 201, body: load_fixture('discount')
+    
+    discount = ShopifyAPI::Discount.find(680866)
+    discount.enable
+    
+    assert_equal "enabled", discount.status
+  end
+  
+  test 'delete should delete discount' do
+    fake 'discounts/680866', method: :get, status: 200, body: load_fixture('discount')
+    fake 'discounts/680866', method: :delete, status: 200, body: 'destroyed'
+    
+    discount = ShopifyAPI::Discount.find(680866)
+    assert discount.destroy
+  end
+end

+ 17 - 0
test/fixtures/discount.json

@@ -0,0 +1,17 @@
+{
+  "discount": {
+    "id": 680866,
+    "code": "TENOFF",
+    "value": "10.0",
+    "ends_at": null,
+    "starts_at": null,
+    "status": "enabled",
+    "minimum_order_amount": "0.00",
+    "usage_limit": null,
+    "applies_to_id": null,
+    "applies_once": false,
+    "discount_type": "percentage",
+    "applies_to_resource": null,
+    "times_used": 1
+  }
+}

+ 17 - 0
test/fixtures/discount_disabled.json

@@ -0,0 +1,17 @@
+{
+  "discount": {
+    "id": 680866,
+    "code": "TENOFF",
+    "value": "10.0",
+    "ends_at": null,
+    "starts_at": null,
+    "status": "disabled",
+    "minimum_order_amount": "0.00",
+    "usage_limit": null,
+    "applies_to_id": null,
+    "applies_once": false,
+    "discount_type": "percentage",
+    "applies_to_resource": null,
+    "times_used": 1
+  }
+}

+ 34 - 0
test/fixtures/discounts.json

@@ -0,0 +1,34 @@
+{
+  "discounts": [
+    {
+      "id": 680866,
+      "code": "TENOFF",
+      "value": "10.0",
+      "ends_at": null,
+      "starts_at": null,
+      "status": "enabled",
+      "minimum_order_amount": "0.00",
+      "usage_limit": null,
+      "applies_to_id": null,
+      "applies_once": false,
+      "discount_type": "percentage",
+      "applies_to_resource": null,
+      "times_used": 1
+    },
+    {
+      "id": 949676421,
+      "code": "xyz",
+      "value": "10.00",
+      "ends_at": null,
+      "starts_at": null,
+      "status": "disabled",
+      "minimum_order_amount": "0.00",
+      "usage_limit": null,
+      "applies_to_id": null,
+      "applies_once": false,
+      "discount_type": "fixed_amount",
+      "applies_to_resource": null,
+      "times_used": 0
+    }
+  ]
+}