Prechádzať zdrojové kódy

Use raw URLs from link

Drew Martin 5 rokov pred
rodič
commit
7dd24809a7

+ 9 - 18
lib/shopify_api/paginated_collection.rb

@@ -3,30 +3,28 @@
 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)
+        @next_url = pagination_link_headers.next_link&.url&.to_s
+        @previous_url = pagination_link_headers.previous_link&.url&.to_s
         super(args)
       end
 
       def next_page?
         ensure_available
-        @next_url_params.present?
+        @next_url.present?
       end
 
       def previous_page?
         ensure_available
-        @previous_url_params.present?
+        @previous_url.present?
       end
 
       def fetch_next_page
-        fetch_page(@next_url_params)
+        fetch_page(@next_url)
       end
 
       def fetch_previous_page
-        fetch_page(@previous_url_params)
+        fetch_page(@previous_url)
       end
 
       private
@@ -34,18 +32,11 @@ module ShopifyAPI
       AVAILABLE_IN_VERSION = ShopifyAPI::ApiVersion::Release.new('2019-10')
       AVAILABLE_IN_VERSION_EARLY = ShopifyAPI::ApiVersion::Release.new('2019-07')
 
-      def fetch_page(url_params)
+      def fetch_page(url)
         ensure_available
-        return [] unless url_params.present?
-
-        params = url_params
-        params = params.reverse_merge(prefix_options) if prefix_options.present?
-        resource_class.where(params)
-      end
+        return [] unless url.present?
 
-      def extract_url_params(link_header)
-        return nil unless link_header.present?
-        Rack::Utils.parse_nested_query(link_header.url.query)
+        resource_class.all(from: url)
       end
 
       def pagination_link_headers

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

@@ -137,10 +137,6 @@ 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?

+ 21 - 0
test/product_listing_test.rb

@@ -73,4 +73,25 @@ class ProductListingTest < Test::Unit::TestCase
     next_page = product_ids.fetch_next_page
     assert_equal [2, 1], next_page
   end
+
+  def test_get_product_listing_product_ids_multi_page_with_cursor_fails_on_older_api_version
+    version = ShopifyAPI::ApiVersion::Release.new('2019-07')
+    ShopifyAPI::Base.api_version = version.to_s
+
+    url = "https://this-is-my-test-shop.myshopify.com/admin/api/2019-07/product_listings/product_ids.json"
+
+    fake(
+      "product_listings/product_ids",
+      method: :get,
+      status: 201,
+      url: url,
+      body: load_fixture('product_listing_product_ids'),
+    )
+
+    product_ids = ShopifyAPI::ProductListing.product_ids
+    assert_equal [4, 3], product_ids
+    assert_raises NotImplementedError do
+      product_ids.next_page?
+    end
+  end
 end