Skip to content

Commit

Permalink
Use all next/previous link params
Browse files Browse the repository at this point in the history
  • Loading branch information
garethson committed Jul 17, 2019
1 parent f2c05be commit ef49dad
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
28 changes: 14 additions & 14 deletions lib/shopify_api/collection_pagination.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
21 changes: 10 additions & 11 deletions test/pagination_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ def setup
@next_page_info = "eyJkaXJlY3Rpb24iOiJuZXh0IiwibGFzdF9pZCI6NDQwMDg5NDIzLCJsYXN0X3ZhbHVlIjoiNDQwMDg5NDIzIn0%3D"
@previous_page_info = "eyJsYXN0X2lkIjoxMDg4MjgzMDksImxhc3RfdmFsdWUiOiIxMDg4MjgzMDkiLCJkaXJlY3Rpb24iOiJuZXh0In0%3D"

@next_link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?page_info=#{@next_page_info}>; rel=\"next\""
@previous_link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?page_info=#{@previous_page_info}>; rel=\"previous\""
@next_link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?page_info=#{@next_page_info}>; rel=\"next\">"
@previous_link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?page_info=#{@previous_page_info}>; rel=\"previous\">"
end

test "navigates using next and previous link headers" do
link_header =
"<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?page_info=#{@previous_page_info}>; rel=\"previous\",\
<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?page_info=#{@next_page_info}>; 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
Expand 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

Expand All @@ -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 = "<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?#{params}>; 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
Expand Down

0 comments on commit ef49dad

Please sign in to comment.