Created
May 11, 2016 18:29
-
-
Save tiagomatos/ce23ca38ffa0e666b8364a18d6e18d64 to your computer and use it in GitHub Desktop.
API Product Count through Pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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