Skip to content

Commit 7e411e9

Browse files
committed
Fix internals to intitalize on colllection creation.
1 parent 8f5ae0a commit 7e411e9

File tree

3 files changed

+58
-17
lines changed

3 files changed

+58
-17
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ end
384384
Page based pagination will be deprecated in the `2019-10` API version, in favor of the second method of pagination:
385385

386386
[Relative cursor based pagination](https://help.shopify.com/en/api/guides/paginated-rest-results)
387-
```
387+
```ruby
388388
products = ShopifyAPI::Product.find(:all, params: { limit: 50 })
389389
process_products(products)
390390
while products.next_page?
+17-14
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,42 @@
11
module ShopifyAPI
22
module CollectionPagination
33

4+
def initialize(args)
5+
@previous_url_params = extract_url_params(pagination_link_headers.previous_link)
6+
@next_url_params = extract_url_params(pagination_link_headers.next_link)
7+
super(args)
8+
end
9+
410
def next_page?
5-
next_url_params.present?
11+
ensure_available
12+
@next_url_params.present?
613
end
714

815
def previous_page?
9-
previous_url_params.present?
16+
ensure_available
17+
@previous_url_params.present?
1018
end
1119

1220
def fetch_next_page
13-
fetch_page(next_url_params)
21+
fetch_page(@next_url_params)
1422
end
1523

1624
def fetch_previous_page
17-
fetch_page(previous_url_params)
25+
fetch_page(@previous_url_params)
1826
end
1927

2028
private
2129

2230
AVAILABLE_IN_VERSION = ShopifyAPI::ApiVersion::Unstable.new
2331

2432
def fetch_page(url_params)
33+
ensure_available
2534
return [] unless url_params.present?
2635

2736
resource_class.where(url_params)
2837
end
2938

30-
def previous_url_params
31-
@previous_url_params ||= extract_url_params(pagination_link_headers.previous_link)
32-
end
33-
34-
def next_url_params
35-
@next_url_params ||= extract_url_params(pagination_link_headers.next_link)
36-
end
37-
3839
def extract_url_params(link_header)
39-
raise NotImplementedError unless ShopifyAPI::Base.api_version >= AVAILABLE_IN_VERSION
40-
4140
return nil unless link_header.present?
4241
Rack::Utils.parse_nested_query(link_header.url.query)
4342
end
@@ -47,5 +46,9 @@ def pagination_link_headers
4746
ShopifyAPI::Base.connection.response["Link"]
4847
)
4948
end
49+
50+
def ensure_available
51+
raise NotImplementedError unless ShopifyAPI::Base.api_version >= AVAILABLE_IN_VERSION
52+
end
5053
end
5154
end

test/pagination_test.rb

+40-2
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,9 @@ def setup
124124
test "raises on invalid pagination links" do
125125
link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?page_info=#{@next_page_info}>;"
126126
fake 'orders', :method => :get, :status => 200, api_version: @version, :body => load_fixture('orders'), :link => link_header
127-
orders = ShopifyAPI::Order.all
128127

129128
assert_raises ShopifyAPI::InvalidPaginationLinksError do
130-
orders.fetch_next_page
129+
ShopifyAPI::Order.all
131130
end
132131
end
133132

@@ -142,4 +141,43 @@ def setup
142141
orders.fetch_next_page
143142
end
144143
end
144+
145+
test "allows for multiple concurrent API collection objects" do
146+
first_request_params = "page_info=#{@next_page_info}&limit=5"
147+
fake(
148+
'orders',
149+
method: :get,
150+
status: 200,
151+
api_version: @version,
152+
url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?limit=5",
153+
body: load_fixture('orders'),
154+
link: "<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?#{first_request_params}>; rel=\"next\""
155+
)
156+
orders = ShopifyAPI::Order.where(limit: 5)
157+
158+
second_request_params = "page_info=#{@next_page_info}&limit=5"
159+
fake(
160+
'orders',
161+
method: :get,
162+
status: 200,
163+
api_version: @version,
164+
url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?limit=10",
165+
body: load_fixture('orders'),
166+
link: "<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?#{second_request_params}>; rel=\"next\""
167+
)
168+
169+
orders2 = ShopifyAPI::Order.where(limit: 10)
170+
171+
fake(
172+
'orders',
173+
method: :get,
174+
status: 200,
175+
api_version: @version,
176+
url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?limit=5&page_info=#{@next_page_info}",
177+
body: load_fixture('orders')
178+
)
179+
next_page = orders.fetch_next_page
180+
assert_equal 450789469, next_page.first.id
181+
end
182+
145183
end

0 commit comments

Comments
 (0)