Skip to content

Commit 991cc6e

Browse files
author
unabris
committed
Add support for paginated:false on products call
1 parent c695fdc commit 991cc6e

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

lib/beyond_api/resources/products.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Products < Base
3232
#
3333
# @beyond_api.scopes +prod:r+
3434
#
35+
# @option params [Boolean] :paginated
3536
# @option params [Integer] :size the page size
3637
# @option params [Integer] :page the page number
3738
#
@@ -41,9 +42,19 @@ class Products < Base
4142
# @products = session.products.all(size: 100, page: 0)
4243
#
4344
def all(params = {})
44-
response, status = BeyondApi::Request.get(@session, "/products", params)
45+
if params[:paginated] == false
46+
result = all_paginated(page: 0, size: 1000)
4547

46-
handle_response(response, status)
48+
(1..result[:page][:total_pages] - 1).each do |page|
49+
result[:embedded][:products].concat(all_paginated(page: page, size: 1000)[:embedded][:products])
50+
end
51+
52+
result.is_a?(Hash) ? result.delete(:page) : result.delete_field(:page)
53+
54+
result
55+
else
56+
all_paginated(params)
57+
end
4758
end
4859

4960
#
@@ -282,5 +293,13 @@ def assign_variation_images_differentiator(product_id, differentiator)
282293
alias_method :create_variation, :create
283294
alias_method :find_variation, :find
284295
alias_method :update_variation, :update
296+
297+
private
298+
299+
def all_paginated(params = {})
300+
response, status = BeyondApi::Request.get(@session, "/products", params)
301+
302+
handle_response(response, status)
303+
end
285304
end
286305
end

0 commit comments

Comments
 (0)