Skip to content

Commit 8905f90

Browse files
Merge pull request #609 from Shopify/collection-products
Add support Collection#products endpoint
2 parents 7c5a458 + 8b94824 commit 8905f90

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
module ShopifyAPI
4+
class Collection < Base
5+
include Events
6+
include Metafields
7+
8+
def products(options = {})
9+
available_in_version = ShopifyAPI::ApiVersion.find_version(:unstable)
10+
raise NotImplementedError unless ShopifyAPI::Base.api_version >= available_in_version
11+
Product.find(:all, from: "#{self.class.prefix}collections/#{id}/products.json", params: options)
12+
end
13+
end
14+
end

test/collection_test.rb

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'test_helper'
2+
3+
class CollectionTest < Test::Unit::TestCase
4+
test "Collection get products gets all products in a collection on unstable version" do
5+
unstable_version = ShopifyAPI::Session.new(domain: 'shop2.myshopify.com', token: 'token2', api_version: :unstable)
6+
ShopifyAPI::Base.activate_session(unstable_version)
7+
8+
fake(
9+
'collections',
10+
url: 'https://shop2.myshopify.com/admin/api/unstable/collections/1.json',
11+
method: :get,
12+
status: 200,
13+
body: load_fixture('collection'),
14+
extension: false
15+
)
16+
17+
collection = ShopifyAPI::Collection.find(1)
18+
19+
fake(
20+
'products',
21+
url: 'https://shop2.myshopify.com/admin/api/unstable/collections/1/products.json',
22+
method: :get,
23+
status: 200,
24+
body: load_fixture('collection_products'),
25+
extension: false
26+
)
27+
assert_equal [632910392, 921728736], collection.products.map(&:id)
28+
end
29+
30+
test "Collection get products fails on older api version" do
31+
unstable_version = ShopifyAPI::Session.new(domain: 'shop2.myshopify.com', token: 'token2', api_version: '2019-07')
32+
ShopifyAPI::Base.activate_session(unstable_version)
33+
34+
fake(
35+
'collections',
36+
url: 'https://shop2.myshopify.com/admin/api/2019-07/collections/1.json',
37+
method: :get,
38+
status: 200,
39+
body: load_fixture('collection'),
40+
extension: false
41+
)
42+
43+
collection = ShopifyAPI::Collection.find(1)
44+
45+
assert_raises NotImplementedError do
46+
collection.products
47+
end
48+
end
49+
end

test/fixtures/collection.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"collection": {
3+
"id": 1,
4+
"handle": "test-collection",
5+
"title": "test-collection",
6+
"updated_at": "2016-03-17T16:58:37-04:00",
7+
"body_html": null,
8+
"published_at": "2016-03-17T16:58:37-04:00",
9+
"sort_order": "alpha-asc",
10+
"template_suffix": null,
11+
"published_scope": "global",
12+
"image": {
13+
"created_at": "2016-03-17T16:58:37-04:00",
14+
"src": "https:\/\/cdn.shopify.com\/s\/files\/1\/0006\/9093\/3842\/collections\/fd43f2c8883f6e9b680e3295fd990d2c.gif?v=1458248317"
15+
}
16+
}
17+
}
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"products": [
3+
{
4+
"product_type": "Cult Products",
5+
"handle": "ipod-nano",
6+
"created_at": "2011-10-20T14:05:13-04:00",
7+
"body_html": "<p>It's the small iPod with one very big idea: Video. Now the world's most popular music player, available in 4GB and 8GB models, lets you enjoy TV shows, movies, video podcasts, and more. The larger, brighter display means amazing picture quality. In six eye-catching colors, iPod nano is stunning all around. And with models starting at just $149, little speaks volumes.</p>",
8+
"title": "IPod Nano - 8GB",
9+
"template_suffix": null,
10+
"updated_at": "2011-10-20T14:05:13-04:00",
11+
"id": 632910392,
12+
"tags": "Emotive, Flash Memory, MP3, Music",
13+
"images": [
14+
{
15+
"position": 1,
16+
"created_at": "2011-10-20T14:05:13-04:00",
17+
"product_id": 632910392,
18+
"updated_at": "2011-10-20T14:05:13-04:00",
19+
"src": "http://static.shopify.com/s/files/1/6909/3384/products/ipod-nano.png?0",
20+
"id": 850703190
21+
}
22+
],
23+
"vendor": "Apple",
24+
"published_at": "2007-12-31T19:00:00-05:00",
25+
"manually_sorted": true,
26+
"options": [
27+
{
28+
"name": "Title"
29+
}
30+
]
31+
},
32+
{
33+
"product_type": "Cult Products",
34+
"handle": "ipod-touch",
35+
"created_at": "2018-09-26T14:05:13-04:00",
36+
"body_html": "<p>The iPod Touch has the iPhone's multi-touch interface, with a physical home button off the touch screen. The home screen has a list of buttons for the available applications.</p>",
37+
"title": "IPod Touch 8GB",
38+
"template_suffix": null,
39+
"updated_at": "2018-09-26T14:05:13-04:00",
40+
"id": 921728736,
41+
"tags": null,
42+
"vendor": "Apple",
43+
"published_at": "2018-09-26T14:05:13-04:00",
44+
"manually_sorted": true
45+
}
46+
]
47+
}

0 commit comments

Comments
 (0)