Link header pagination for Rails APIs.
Ruby
Switch branches/tags
Permalink
Failed to load latest commit information.
lib Bump version to 2.1.1 Nov 18, 2015
spec Bump Rails dependency Nov 18, 2015
.gitignore Ignore Gemfile.lock Nov 18, 2015
.travis.yml Test against Ruby 2.3.0 Jan 6, 2016
Gemfile Minor cleanup Nov 18, 2015
LICENSE Update LICENSE Apr 18, 2015
README.md Use Travis CI Jan 6, 2016
Rakefile First commit Oct 1, 2013
api_pagination_headers.gemspec Bump Rails dependency Nov 18, 2015

README.md

api_pagination_headers Build Status Dependency Status

Adds pagination info to a Link response header.

This technique is considered to be a best practice for REST APIs and is currently used by GitHub. The gem also adds the total number of records into a Total-Count header.

Support for rails-api is built in.

Example

Link: <http://example.com/posts?page=2&per_page=10>; rel="next", <http://example.com/posts?page=2&per_page=10>; rel="last"
Total-Count: 11

Requirements

Install

Include in your Gemfile:

gem 'api_pagination_headers'

Usage

class PostsController < ApplicationController
  after_action only: [:index] { set_pagination_headers(:posts) }

  def index
    @posts = Post.all.paginate(per_page: params[:per_page], page: params[:page])
    respond_with @posts
  end
end

Config

ApiPaginationHeaders.configure do |config|
  # Change total count header title (default: 'Total-Count')
  config.total_count_header = 'X-Total-Count'
  
  # Force HTTPS (default: false)
  config.force_https = true
end

License

MIT © Richard Käll