Rails API pagination

Recently I have been working with API's quite a lot, not just implementing them but giving some code review on the company's projects.

One thing I always see on projects is the way we handle pagination for json API's and whether is a copy/paste strategy or build it from scratch, we ended up with duplication of code among projects and in some rare cases the output from the server may be different.

So I manage to put a gem together in order to spin up the pagination implementations on Rails JSON API's following the standard on http://jsonapi.org/.

Pager

Pager API is a library to help you add meta information and the adecuate header with pagination information based on the JSON API standard for your Rails app.

We always use Kaminari to paginate records on any Rails app we build, however there are some Will Paginate lovers out there and we did not want to take them out, that's why we support both gems, and it is up to you which one to use.

The gem outputs your json collections with two things:

1. Meta information about the pagination:

 "meta": {
        "pagination": {
            "per_page": 15,
            "total_pages": 1,
            "total_objects": 15,
            "links": {
                "first": "/api/users?page=1",
                "last": "/api/users?page=1"
            }
        }
    }
# Link: <http://example.com/api/v1/users?page="2">; rel="next",
# <http://example.com/api/v1//users?page="5">; rel="last",
# <http://example.com/api/v1//users?page="1">; rel="first",
# <http://example.com/api/v1/users?page="1">; rel="prev"

You can customize this behavior on the initializer, created by the gem.


That's all for now, hope you find it useful!

Feel free to show some love or give feedback!