Paginated request in SQL cursor style
While integrating our Rails project with an Api, we found ourselves in the following situation: we wanted to iterate over all the resources in an end point, but we couldn’t fetch them all at once.
So, let’s say this endpoint was /products?page=# and the last page + 1 would return {}.
/products?page=1 returns:
/products?page=2 returns:
And finally /products?page=3 returns :
See also: Refactoring our code — The MVP design pattern for Android
We encapsulated all the Api related behavior into one class called ApiManager and we didn’t want to handle page managment outside of it so we could use it like:
How did we build this dynamic iterator which knows how to fetch the next page when the current is empty? Using Enumerable module!
See also: Customizing Components-Radio Button
Now we can iterate as shown before and given we only store one page at the time memory it’s not bloated.
Posted by Jesús Herrera (jesus.herrera@wolox.com.ar)