Skip to content

Instantly share code, notes, and snippets.

@assembler
Last active June 9, 2016 12:43
Show Gist options
  • Save assembler/99b399ec4d62a3e175f97660e34f5e2d to your computer and use it in GitHub Desktop.
Save assembler/99b399ec4d62a3e175f97660e34f5e2d to your computer and use it in GitHub Desktop.
def paginate
return enum_for(:paginate) unless block_given?
page = 0
page_count = nil
partners = []
loop do
response = fetch_all(page)
page_count ||= (response['partners_count'].to_f / PARTNERS_PER_PAGE)
yield response["partners"]
break if page >= page_count
page += 1
end
end
client.paginate.lazy.take(3).flatten # takes first 3 pages of partners
client.paginate.each { |parnters| ... } # takes all partners
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment