Jelajahi Sumber

Paginate collection listing product ids and product listing product ids with cursor

Drew Martin 5 tahun lalu
induk
melakukan
102169c14b

+ 5 - 1
lib/shopify_api/paginated_collection.rb

@@ -3,6 +3,8 @@
 module ShopifyAPI
   class PaginatedCollection < ActiveResource::Collection
     module CollectionPagination
+      attr_accessor :prefix_options
+
       def initialize(args)
         @previous_url_params = extract_url_params(pagination_link_headers.previous_link)
         @next_url_params = extract_url_params(pagination_link_headers.next_link)
@@ -36,7 +38,9 @@ module ShopifyAPI
         ensure_available
         return [] unless url_params.present?
 
-        resource_class.where(url_params)
+        params = url_params
+        params = params.reverse_merge(prefix_options) if prefix_options.present?
+        resource_class.where(params)
       end
 
       def extract_url_params(link_header)

+ 2 - 1
lib/shopify_api/resources.rb

@@ -1,2 +1,3 @@
 require 'shopify_api/resources/base'
-Dir.glob("#{File.dirname(__FILE__)}/resources/*").each { |file| require(file) } 
+require 'shopify_api/resources/array_base'
+Dir.glob("#{File.dirname(__FILE__)}/resources/*").each { |file| require(file) }

+ 13 - 0
lib/shopify_api/resources/array_base.rb

@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+module ShopifyAPI
+  class ArrayBase < Base
+    class << self
+      private
+
+      def instantiate_record(record, *)
+        record
+      end
+    end
+  end
+end

+ 4 - 0
lib/shopify_api/resources/base.rb

@@ -137,6 +137,10 @@ module ShopifyAPI
       def early_july_pagination_release!
         self.early_july_pagination = true
       end
+
+      def instantiate_collection(_collection, _original_params = {}, prefix_options = {})
+        super.tap { |collection| collection.prefix_options = prefix_options }
+      end
     end
 
     def persisted?

+ 8 - 1
lib/shopify_api/resources/collection_listing.rb

@@ -5,7 +5,14 @@ module ShopifyAPI
     early_july_pagination_release!
 
     def product_ids
-      get(:product_ids)
+      ProductId.all(params: { collection_id: collection_id })
     end
+
+    class ProductId < ArrayBase
+      self.resource_prefix = 'collection_listings/:collection_id/'
+
+      early_july_pagination_release!
+    end
+    private_constant :ProductId
   end
 end

+ 6 - 1
lib/shopify_api/resources/product_listing.rb

@@ -5,7 +5,12 @@ module ShopifyAPI
     early_july_pagination_release!
 
     def self.product_ids
-      get(:product_ids)
+      ProductId.all
     end
+
+    class ProductId < ArrayBase
+      self.resource_prefix = 'product_listings/'
+    end
+    private_constant :ProductId
   end
 end

+ 38 - 0
test/collection_listing_test.rb

@@ -38,4 +38,42 @@ class CollectionListingTest < Test::Unit::TestCase
 
     assert_equal [1, 2], collection_listing.product_ids
   end
+
+  def test_get_collection_listing_product_ids_multi_page_with_cursor
+    version = ShopifyAPI::ApiVersion::Release.new('2019-07')
+    ShopifyAPI::Base.api_version = version.to_s
+
+    collection_listing = ShopifyAPI::CollectionListing.new(collection_id: 1)
+
+    url = "https://this-is-my-test-shop.myshopify.com/admin/api/2019-07/collection_listings/1/product_ids.json"
+
+    next_page_info = "notarealpageinfobutthatsokay"
+    next_page_url = "#{url}?page_info=#{next_page_info}"
+    link_header = "<#{next_page_url}>; rel=\"next\""
+
+    fake(
+      "collection_listings/1/product_ids",
+      method: :get,
+      status: 201,
+      url: url,
+      body: load_fixture('collection_listing_product_ids'),
+      link: link_header,
+    )
+
+    product_ids = collection_listing.product_ids
+    assert_equal [1, 2], product_ids
+    assert product_ids.next_page?
+
+    fake(
+      "collection_listings/1/product_ids",
+      method: :get,
+      status: 201,
+      url: next_page_url,
+      body: load_fixture('collection_listing_product_ids2'),
+      link: link_header,
+    )
+
+    next_page = product_ids.fetch_next_page
+    assert_equal [3, 4], next_page
+  end
 end

+ 1 - 0
test/fixtures/collection_listing_product_ids2.json

@@ -0,0 +1 @@
+[3, 4]

+ 1 - 1
test/fixtures/product_listing_product_ids.json

@@ -1 +1 @@
-[2, 1]
+[4, 3]

+ 1 - 0
test/fixtures/product_listing_product_ids2.json

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

+ 38 - 2
test/product_listing_test.rb

@@ -34,7 +34,43 @@ class ProductListingTest < Test::Unit::TestCase
 
     product_ids = ShopifyAPI::ProductListing.product_ids
     assert_equal 2, product_ids.count
-    assert_equal 2, product_ids.first
-    assert_equal 1, product_ids.last
+    assert_equal 4, product_ids.first
+    assert_equal 3, product_ids.last
+  end
+
+  def test_get_product_listing_product_ids_multi_page_with_cursor
+    version = ShopifyAPI::ApiVersion::Release.new('2019-10')
+    ShopifyAPI::Base.api_version = version.to_s
+
+    url = "https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/product_listings/product_ids.json"
+
+    next_page_info = "notarealpageinfobutthatsokay"
+    next_page_url = "#{url}?page_info=#{next_page_info}"
+    link_header = "<#{next_page_url}>; rel=\"next\""
+
+    fake(
+      "product_listings/product_ids",
+      method: :get,
+      status: 201,
+      url: url,
+      body: load_fixture('product_listing_product_ids'),
+      link: link_header,
+    )
+
+    product_ids = ShopifyAPI::ProductListing.product_ids
+    assert_equal [4, 3], product_ids
+    assert product_ids.next_page?
+
+    fake(
+      "product_listings/product_ids",
+      method: :get,
+      status: 201,
+      url: next_page_url,
+      body: load_fixture('product_listing_product_ids2'),
+      link: link_header,
+    )
+
+    next_page = product_ids.fetch_next_page
+    assert_equal [2, 1], next_page
   end
 end