Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tiagomatos/ce23ca38ffa0e666b8364a18d6e18d64 to your computer and use it in GitHub Desktop.
Save tiagomatos/ce23ca38ffa0e666b8364a18d6e18d64 to your computer and use it in GitHub Desktop.
API Product Count through Pagination
require 'httparty'
LOGIN = 'my-store-code'
TOKEN = 'XXXXX'
response = HTTParty.get("https://api.jumpseller.com/v1/products/count.json?login=#{LOGIN}&authtoken=#{TOKEN}")
products_count = response.parsed_response["count"]
pages = products_count / 200 # the number of pages used for pagination.
last_pagination_count = products_count - pages * 200 # calculates the number of products present at the last pagination.
1.upto(10) do |page| # so we repeat this process X times.
1.upto(pages+1) do |page|
p "page: #{page}"
response = HTTParty.get("https://api.jumpseller.com/v1/products.json?login=#{LOGIN}&authtoken=#{TOKEN}&limit=200&page=#{page}")
p "page products count: #{response.size}"
if page == pages + 1
# last page.
exit unless response.size == last_pagination_count # error!
else
exit unless response.size == 200 # error!
end
sleep 1 # do not attack the api (max of 60 requests per minute)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment