diff --git a/lib/shopify_api/collection_pagination.rb b/lib/shopify_api/collection_pagination.rb index e45ebe6c5..3714b6c22 100644 --- a/lib/shopify_api/collection_pagination.rb +++ b/lib/shopify_api/collection_pagination.rb @@ -1,46 +1,46 @@ +require 'pry' module ShopifyAPI module CollectionPagination def next_page? - next_page_info.present? + next_url_params.present? end def previous_page? - previous_page_info.present? + previous_url_params.present? end def fetch_next_page - fetch_page(next_page_info) + fetch_page(next_url_params) end def fetch_previous_page - fetch_page(previous_page_info) + fetch_page(previous_url_params) end private AVAILABLE_IN_VERSION = ShopifyAPI::ApiVersion::Unstable.new - def fetch_page(page_info) - return [] unless page_info + def fetch_page(url_params) + return [] unless url_params.present? - resource_class.where(original_params.merge(page_info: page_info)) + resource_class.where(url_params) end - def previous_page_info - @previous_page_info ||= extract_page_info(pagination_link_headers.previous_link) + def previous_url_params + @previous_url_params ||= extract_url_params(pagination_link_headers.previous_link) end - def next_page_info - @next_page_info ||= extract_page_info(pagination_link_headers.next_link) + def next_url_params + @next_url_params ||= extract_url_params(pagination_link_headers.next_link) end - def extract_page_info(link_header) + def extract_url_params(link_header) raise NotImplementedError unless ShopifyAPI::Base.api_version >= AVAILABLE_IN_VERSION return nil unless link_header.present? - - CGI.parse(link_header.url.query).dig("page_info", 0) + Rack::Utils.parse_nested_query(link_header.url.query) end def pagination_link_headers diff --git a/test/pagination_test.rb b/test/pagination_test.rb index 0d4451c9c..01da68c0a 100644 --- a/test/pagination_test.rb +++ b/test/pagination_test.rb @@ -9,14 +9,12 @@ def setup @next_page_info = "eyJkaXJlY3Rpb24iOiJuZXh0IiwibGFzdF9pZCI6NDQwMDg5NDIzLCJsYXN0X3ZhbHVlIjoiNDQwMDg5NDIzIn0%3D" @previous_page_info = "eyJsYXN0X2lkIjoxMDg4MjgzMDksImxhc3RfdmFsdWUiOiIxMDg4MjgzMDkiLCJkaXJlY3Rpb24iOiJuZXh0In0%3D" - @next_link_header = "; rel=\"next\"" - @previous_link_header = "; rel=\"previous\"" + @next_link_header = "; rel=\"next\">" + @previous_link_header = "; rel=\"previous\">" end - test "navigates using next and previous link headers" do - link_header = - "; rel=\"previous\",\ - ; rel=\"next\"" + test "navigates using next and previous link headers with no original params" do + link_header ="#{@previous_link_header}, #{@next_link_header}" fake 'orders', :method => :get, :status => 200, api_version: @version, :body => load_fixture('orders'), :link => link_header orders = ShopifyAPI::Order.all @@ -28,7 +26,6 @@ def setup status: 200, body: load_fixture('orders') ) - next_page = orders.fetch_next_page assert_equal 450789469, next_page.first.id @@ -44,24 +41,26 @@ def setup assert_equal 1122334455, previous_page.first.id end - test "retains previous querystring parameters" do + test "uses all passed in querystring parameters" do + params = "page_info=#{@next_page_info}&limit=50&fields=#{CGI.escape('id,created_at')}" + @next_link_header = "; rel=\"next\">" fake( 'orders', method: :get, status: 200, api_version: @version, - url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?fields=id%2Cupdated_at", + url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?fields=id%2Cupdated_at&limit=100", body: load_fixture('orders'), link: @next_link_header ) - orders = ShopifyAPI::Order.where(fields: 'id,updated_at') + orders = ShopifyAPI::Order.where(fields: 'id,updated_at', limit: 100) fake( 'orders', method: :get, status: 200, api_version: @version, - url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?fields=id%2Cupdated_at&page_info=#{@next_page_info}", + url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?fields=id%2Ccreated_at&limit=50&page_info=#{@next_page_info}", body: load_fixture('orders') ) next_page = orders.fetch_next_page