Procházet zdrojové kódy

Allow cursor pagination in 2019-07 release for select resources

Drew Martin před 5 roky
rodič
revize
2d5d4f3089

+ 4 - 1
lib/shopify_api/paginated_collection.rb

@@ -30,6 +30,7 @@ module ShopifyAPI
       private
 
       AVAILABLE_IN_VERSION = ShopifyAPI::ApiVersion::Release.new('2019-10')
+      AVAILABLE_IN_VERSION_EARLY = ShopifyAPI::ApiVersion::Release.new('2019-07')
 
       def fetch_page(url_params)
         ensure_available
@@ -50,7 +51,9 @@ module ShopifyAPI
       end
 
       def ensure_available
-        raise NotImplementedError unless ShopifyAPI::Base.api_version >= AVAILABLE_IN_VERSION
+        return if ShopifyAPI::Base.api_version >= AVAILABLE_IN_VERSION
+        return if ShopifyAPI::Base.api_version >= AVAILABLE_IN_VERSION_EARLY && resource_class.early_july_pagination?
+        raise NotImplementedError
       end
     end
 

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

@@ -125,6 +125,18 @@ module ShopifyAPI
           @prefix_options[resource_id]
         end
       end
+
+      def early_july_pagination?
+        !!early_july_pagination
+      end
+
+      private
+
+      attr_accessor :early_july_pagination
+
+      def early_july_pagination_release!
+        self.early_july_pagination = true
+      end
     end
 
     def persisted?

+ 1 - 0
lib/shopify_api/resources/collect.rb

@@ -1,5 +1,6 @@
 module ShopifyAPI
   # For adding/removing products from custom collections
   class Collect < Base
+    early_july_pagination_release!
   end
 end

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

@@ -2,6 +2,8 @@ module ShopifyAPI
   class CollectionListing < Base
     self.primary_key = :collection_id
 
+    early_july_pagination_release!
+
     def product_ids
       get(:product_ids)
     end

+ 2 - 0
lib/shopify_api/resources/customer_saved_search.rb

@@ -2,6 +2,8 @@ require 'shopify_api/resources/customer'
 
 module ShopifyAPI
   class CustomerSavedSearch < Base
+    early_july_pagination_release!
+
     def customers(params = {})
       Customer.search(params.merge({:customer_saved_search_id => self.id}))
     end

+ 1 - 0
lib/shopify_api/resources/event.rb

@@ -3,5 +3,6 @@ module ShopifyAPI
     include DisablePrefixCheck
 
     conditional_prefix :resource, true
+    early_july_pagination_release!
   end
 end

+ 1 - 0
lib/shopify_api/resources/metafield.rb

@@ -3,6 +3,7 @@ module ShopifyAPI
     include DisablePrefixCheck
 
     conditional_prefix :resource, true
+    early_july_pagination_release!
 
     def value
       return if attributes["value"].nil?

+ 2 - 0
lib/shopify_api/resources/product.rb

@@ -3,6 +3,8 @@ module ShopifyAPI
     include Events
     include Metafields
 
+    early_july_pagination_release!
+
     # compute the price range
     def price_range
       prices = variants.collect(&:price).collect(&:to_f)

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

@@ -2,6 +2,8 @@ module ShopifyAPI
   class ProductListing < Base
     self.primary_key = :product_id
 
+    early_july_pagination_release!
+
     def self.product_ids
       get(:product_ids)
     end

+ 24 - 1
test/pagination_test.rb

@@ -130,7 +130,7 @@ class PaginationTest < Test::Unit::TestCase
     end
   end
 
-  test "raises on an invalid API version" do
+  test "raises on an older API version" do
     version = ShopifyAPI::ApiVersion::Release.new('2019-04')
     ShopifyAPI::Base.api_version = version.to_s
 
@@ -142,6 +142,29 @@ class PaginationTest < Test::Unit::TestCase
     end
   end
 
+  test "raises on 2019-07 API version for models that don't support new pagination yet" do
+    version = ShopifyAPI::ApiVersion::Release.new('2019-07')
+    ShopifyAPI::Base.api_version = version.to_s
+
+    fake 'orders', :method => :get, :status => 200, api_version: version, :body => load_fixture('orders')
+    orders = ShopifyAPI::Order.all
+
+    assert_raises NotImplementedError do
+      orders.fetch_next_page
+    end
+  end
+
+  test "new pagination works on 2019-07 API version for select models" do
+    version = ShopifyAPI::ApiVersion::Release.new('2019-07')
+    ShopifyAPI::Base.api_version = version.to_s
+
+    fake 'events', :method => :get, :status => 200, api_version: version, :body => load_fixture('events')
+    events = ShopifyAPI::Event.all
+
+    assert_empty events.fetch_next_page
+    assert_empty events.fetch_previous_page
+  end
+
   test "does not raise on the unstable version" do
     version = ShopifyAPI::ApiVersion::Unstable.new
     ShopifyAPI::Base.api_version = version.to_s