Browse Source

Merge pull request #323 from Shopify/add_product_listings

Add product/collection listings to ShopifyAPI gem
Matt Scriven 8 years ago
parent
commit
72bad45e6f

+ 9 - 0
lib/shopify_api/resources/collection_listing.rb

@@ -0,0 +1,9 @@
+module ShopifyAPI
+  class CollectionListing < Base
+    init_prefix :application
+
+    def product_ids(options = {})
+      get("#{collection_id}/product_ids", options[:params])
+    end
+  end
+end

+ 9 - 0
lib/shopify_api/resources/product_listing.rb

@@ -0,0 +1,9 @@
+module ShopifyAPI
+  class ProductListing < Base
+    init_prefix :application
+
+    def self.product_ids(options = {})
+      get(:product_ids, options[:params])
+    end
+  end
+end

+ 25 - 0
test/collection_listing_test.rb

@@ -0,0 +1,25 @@
+require 'test_helper'
+
+class CollectionListingTest < Test::Unit::TestCase
+
+  def test_get_collection_listings
+    fake "applications/999/collection_listings", method: :get, status: 201, body: load_fixture('collection_listings')
+
+    collection_listings = ShopifyAPI::CollectionListing.find(:all, params: { application_id: 999 })
+
+    assert_equal 1, collection_listings.count
+    assert_equal 1, collection_listings.first.collection_id
+    assert_equal 'Home page', collection_listings.first.title
+  end
+
+  def test_get_collection_listing_for_collection_id
+    fake "applications/999/collection_listings/1", method: :get, status: 201, body: load_fixture('collection_listing')
+    fake "applications/999/collection_listings//1/product_ids", method: :get, status: 201, body: load_fixture('collection_listing_product_ids')
+
+    collection_listing = ShopifyAPI::CollectionListing.find(1, params: { application_id: 999 })
+
+    assert_equal 1, collection_listing.collection_id
+    assert_equal 'Home page', collection_listing.title
+    assert_equal [1, 2], collection_listing.product_ids
+  end
+end

+ 11 - 0
test/fixtures/collection_listing.json

@@ -0,0 +1,11 @@
+{
+  "collection_id": 1,
+  "updated_at": "2017-01-09T13:59:09-05:00",
+  "body_html": null,
+  "default_product_image": null,
+  "handle": "frontpage",
+  "image": null,
+  "title": "Home page",
+  "sort_order": "alpha-asc",
+  "published_at": "2017-01-09T13:59:09-05:00"
+}

+ 1 - 0
test/fixtures/collection_listing_product_ids.json

@@ -0,0 +1 @@
+[1, 2]

+ 13 - 0
test/fixtures/collection_listings.json

@@ -0,0 +1,13 @@
+[
+  {
+    "collection_id": 1,
+    "updated_at": "2017-01-09T13:59:09-05:00",
+    "body_html": null,
+    "default_product_image": null,
+    "handle": "frontpage",
+    "image": null,
+    "title": "Home page",
+    "sort_order": "alpha-asc",
+    "published_at": "2017-01-09T13:59:09-05:00"
+  }
+]

+ 86 - 0
test/fixtures/product_listing.json

@@ -0,0 +1,86 @@
+{
+  "product_id": 2,
+  "created_at": "2017-01-06T14:52:56-05:00",
+  "updated_at": "2017-01-06T14:52:56-05:00",
+  "body_html": null,
+  "handle": "synergistic-silk-chair",
+  "product_type": "morph magnetic solutions",
+  "title": "Synergistic Silk Chair",
+  "vendor": "O'Hara, Fritsch and Hudson",
+  "available": true,
+  "tags": "",
+  "published_at": "2017-01-06T14:52:53-05:00",
+  "images": [
+
+  ],
+  "options": [
+    {
+      "id": 2,
+      "name": "Color or something",
+      "product_id": 2,
+      "position": 1
+    }
+  ],
+  "variants": [
+    {
+      "id": 3,
+      "title": "Aerodynamic Copper Clock",
+      "option_values": [
+        {
+          "option_id": 2,
+          "name": "Color or something",
+          "value": "Aerodynamic Copper Clock"
+        }
+      ],
+      "price": "179.99",
+      "formatted_price": "$179.99",
+      "compare_at_price": null,
+      "grams": 8400,
+      "requires_shipping": true,
+      "sku": "",
+      "barcode": null,
+      "taxable": true,
+      "position": 1,
+      "available": true,
+      "inventory_policy": "deny",
+      "inventory_quantity": 810,
+      "inventory_management": "shopify",
+      "fulfillment_service": "manual",
+      "weight": 8.4,
+      "weight_unit": "kg",
+      "image_id": null,
+      "created_at": "2017-01-04T17:07:47-05:00",
+      "updated_at": "2017-01-04T17:07:47-05:00"
+    },
+    {
+      "id": 4,
+      "title": "Awesome Concrete Knife",
+      "option_values": [
+        {
+          "option_id": 2,
+          "name": "Color or something",
+          "value": "Awesome Concrete Knife"
+        }
+      ],
+      "price": "179.99",
+      "formatted_price": "$179.99",
+      "compare_at_price": null,
+      "grams": 8400,
+      "requires_shipping": true,
+      "sku": "",
+      "barcode": null,
+      "taxable": true,
+      "position": 2,
+      "available": true,
+      "inventory_policy": "deny",
+      "inventory_quantity": 1,
+      "inventory_management": null,
+      "fulfillment_service": "manual",
+      "weight": 8.4,
+      "weight_unit": "kg",
+      "image_id": null,
+      "created_at": "2017-01-04T17:07:47-05:00",
+      "updated_at": "2017-01-04T17:07:47-05:00"
+    }
+  ]
+}

+ 1 - 0
test/fixtures/product_listing_product_ids.json

@@ -0,0 +1 @@
+[2, 1]

+ 174 - 0
test/fixtures/product_listings.json

@@ -0,0 +1,174 @@
+[
+  {
+    "product_id": 2,
+    "created_at": "2017-01-06T14:52:56-05:00",
+    "updated_at": "2017-01-06T14:52:56-05:00",
+    "body_html": null,
+    "handle": "synergistic-silk-chair",
+    "product_type": "morph magnetic solutions",
+    "title": "Synergistic Silk Chair",
+    "vendor": "O'Hara, Fritsch and Hudson",
+    "available": true,
+    "tags": "",
+    "published_at": "2017-01-06T14:52:53-05:00",
+    "images": [
+
+    ],
+    "options": [
+      {
+        "id": 2,
+        "name": "Color or something",
+        "product_id": 2,
+        "position": 1
+      }
+    ],
+    "variants": [
+      {
+        "id": 3,
+        "title": "Aerodynamic Copper Clock",
+        "option_values": [
+          {
+            "option_id": 2,
+            "name": "Color or something",
+            "value": "Aerodynamic Copper Clock"
+          }
+        ],
+        "price": "179.99",
+        "formatted_price": "$179.99",
+        "compare_at_price": null,
+        "grams": 8400,
+        "requires_shipping": true,
+        "sku": "",
+        "barcode": null,
+        "taxable": true,
+        "position": 1,
+        "available": true,
+        "inventory_policy": "deny",
+        "inventory_quantity": 810,
+        "inventory_management": "shopify",
+        "fulfillment_service": "manual",
+        "weight": 8.4,
+        "weight_unit": "kg",
+        "image_id": null,
+        "created_at": "2017-01-04T17:07:47-05:00",
+        "updated_at": "2017-01-04T17:07:47-05:00"
+      },
+      {
+        "id": 4,
+        "title": "Awesome Concrete Knife",
+        "option_values": [
+          {
+            "option_id": 2,
+            "name": "Color or something",
+            "value": "Awesome Concrete Knife"
+          }
+        ],
+        "price": "179.99",
+        "formatted_price": "$179.99",
+        "compare_at_price": null,
+        "grams": 8400,
+        "requires_shipping": true,
+        "sku": "",
+        "barcode": null,
+        "taxable": true,
+        "position": 2,
+        "available": true,
+        "inventory_policy": "deny",
+        "inventory_quantity": 1,
+        "inventory_management": null,
+        "fulfillment_service": "manual",
+        "weight": 8.4,
+        "weight_unit": "kg",
+        "image_id": null,
+        "created_at": "2017-01-04T17:07:47-05:00",
+        "updated_at": "2017-01-04T17:07:47-05:00"
+      }
+    ]
+  },
+  {
+    "product_id": 1,
+    "created_at": "2017-01-06T14:52:54-05:00",
+    "updated_at": "2017-01-06T14:52:54-05:00",
+    "body_html": null,
+    "handle": "rustic-copper-bottle",
+    "product_type": "maximize viral channels",
+    "title": "Rustic Copper Bottle",
+    "vendor": "Kuphal and Sons",
+    "available": true,
+    "tags": "",
+    "published_at": "2017-01-06T14:52:52-05:00",
+    "images": [
+
+    ],
+    "options": [
+      {
+        "id": 1,
+        "name": "Color or something",
+        "product_id": 1,
+        "position": 1
+      }
+    ],
+    "variants": [
+      {
+        "id": 1,
+        "title": "Awesome Bronze Hat",
+        "option_values": [
+          {
+            "option_id": 1,
+            "name": "Color or something",
+            "value": "Awesome Bronze Hat"
+          }
+        ],
+        "price": "111.99",
+        "formatted_price": "$111.99",
+        "compare_at_price": null,
+        "grams": 1800,
+        "requires_shipping": true,
+        "sku": "",
+        "barcode": null,
+        "taxable": true,
+        "position": 1,
+        "available": true,
+        "inventory_policy": "deny",
+        "inventory_quantity": 65,
+        "inventory_management": "shopify",
+        "fulfillment_service": "manual",
+        "weight": 1.8,
+        "weight_unit": "kg",
+        "image_id": null,
+        "created_at": "2017-01-04T17:07:07-05:00",
+        "updated_at": "2017-01-04T17:07:07-05:00"
+      },
+      {
+        "id": 2,
+        "title": "Rustic Marble Bottle",
+        "option_values": [
+          {
+            "option_id": 1,
+            "name": "Color or something",
+            "value": "Rustic Marble Bottle"
+          }
+        ],
+        "price": "111.99",
+        "formatted_price": "$111.99",
+        "compare_at_price": null,
+        "grams": 1800,
+        "requires_shipping": true,
+        "sku": "",
+        "barcode": null,
+        "taxable": true,
+        "position": 2,
+        "available": true,
+        "inventory_policy": "deny",
+        "inventory_quantity": 1,
+        "inventory_management": null,
+        "fulfillment_service": "manual",
+        "weight": 1.8,
+        "weight_unit": "kg",
+        "image_id": null,
+        "created_at": "2017-01-04T17:07:07-05:00",
+        "updated_at": "2017-01-04T17:07:07-05:00"
+      }
+    ]
+  }
+]

+ 31 - 0
test/product_listing_test.rb

@@ -0,0 +1,31 @@
+require 'test_helper'
+
+class ProductListingTest < Test::Unit::TestCase
+
+  def test_get_product_listings
+    fake "applications/999/product_listings", method: :get, status: 201, body: load_fixture('product_listings')
+
+    product_listings = ShopifyAPI::ProductListing.find(:all, params: { application_id: 999 })
+    assert_equal 2, product_listings.count
+    assert_equal 2, product_listings.first.product_id
+    assert_equal 1, product_listings.last.product_id
+    assert_equal 'Synergistic Silk Chair', product_listings.first.title
+    assert_equal 'Rustic Copper Bottle', product_listings.last.title
+  end
+
+  def test_get_product_listing_for_product_id
+    fake "applications/999/product_listings/2", method: :get, status: 201, body: load_fixture('product_listing')
+
+    product_listing = ShopifyAPI::ProductListing.find(2, params: { application_id: 999 })
+    assert_equal 'Synergistic Silk Chair', product_listing.title
+  end
+
+  def test_get_product_listing_product_ids
+    fake "applications/999/product_listings/product_ids", method: :get, status: 201, body: load_fixture('product_listing_product_ids')
+
+    product_ids = ShopifyAPI::ProductListing.product_ids(params: { application_id: 999 })
+    assert_equal 2, product_ids.count
+    assert_equal 2, product_ids.first
+    assert_equal 1, product_ids.last
+  end
+end