Repository is archived
No commit activity in last 3 years
No release in over 3 years
Add pagination info to the response headers, GitHub style.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 4.2.0
 Project Readme

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