Skip to content

Commit

Permalink
Fix internals to parse headers on initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
garethson committed Jul 17, 2019
1 parent 8f5ae0a commit dc1d360
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ end
Page based pagination will be deprecated in the `2019-10` API version, in favor of the second method of pagination:

[Relative cursor based pagination](https://help.shopify.com/en/api/guides/paginated-rest-results)
```
```ruby
products = ShopifyAPI::Product.find(:all, params: { limit: 50 })
process_products(products)
while products.next_page?
Expand Down
31 changes: 17 additions & 14 deletions lib/shopify_api/collection_pagination.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
module ShopifyAPI
module CollectionPagination

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)
super(args)
end

def next_page?
next_url_params.present?
ensure_available
@next_url_params.present?
end

def previous_page?
previous_url_params.present?
ensure_available
@previous_url_params.present?
end

def fetch_next_page
fetch_page(next_url_params)
fetch_page(@next_url_params)
end

def fetch_previous_page
fetch_page(previous_url_params)
fetch_page(@previous_url_params)
end

private

AVAILABLE_IN_VERSION = ShopifyAPI::ApiVersion::Unstable.new

def fetch_page(url_params)
ensure_available
return [] unless url_params.present?

resource_class.where(url_params)
end

def previous_url_params
@previous_url_params ||= extract_url_params(pagination_link_headers.previous_link)
end

def next_url_params
@next_url_params ||= extract_url_params(pagination_link_headers.next_link)
end

def extract_url_params(link_header)
raise NotImplementedError unless ShopifyAPI::Base.api_version >= AVAILABLE_IN_VERSION

return nil unless link_header.present?
Rack::Utils.parse_nested_query(link_header.url.query)
end
Expand All @@ -47,5 +46,9 @@ def pagination_link_headers
ShopifyAPI::Base.connection.response["Link"]
)
end

def ensure_available
raise NotImplementedError unless ShopifyAPI::Base.api_version >= AVAILABLE_IN_VERSION
end
end
end
42 changes: 40 additions & 2 deletions test/pagination_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,9 @@ def setup
test "raises on invalid pagination links" do
link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?page_info=#{@next_page_info}>;"
fake 'orders', :method => :get, :status => 200, api_version: @version, :body => load_fixture('orders'), :link => link_header
orders = ShopifyAPI::Order.all

assert_raises ShopifyAPI::InvalidPaginationLinksError do
orders.fetch_next_page
ShopifyAPI::Order.all
end
end

Expand All @@ -142,4 +141,43 @@ def setup
orders.fetch_next_page
end
end

test "allows for multiple concurrent API collection objects" do
first_request_params = "page_info=#{@next_page_info}&limit=5"
fake(
'orders',
method: :get,
status: 200,
api_version: @version,
url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?limit=5",
body: load_fixture('orders'),
link: "<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?#{first_request_params}>; rel=\"next\""
)
orders = ShopifyAPI::Order.where(limit: 5)

second_request_params = "page_info=#{@next_page_info}&limit=5"
fake(
'orders',
method: :get,
status: 200,
api_version: @version,
url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?limit=10",
body: load_fixture('orders'),
link: "<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?#{second_request_params}>; rel=\"next\""
)

orders2 = ShopifyAPI::Order.where(limit: 10)

fake(
'orders',
method: :get,
status: 200,
api_version: @version,
url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?limit=5&page_info=#{@next_page_info}",
body: load_fixture('orders')
)
next_page = orders.fetch_next_page
assert_equal 450789469, next_page.first.id
end

end

0 comments on commit dc1d360

Please sign in to comment.