Skip to content

Instantly share code, notes, and snippets.

View jims3ne1's full-sized avatar

Jim jims3ne1

  • Cebu, Philippines
View GitHub Profile
@jims3ne1
jims3ne1 / gist:3921895
Last active October 11, 2015 21:28
curl api with pagination headers
curl -i http://sample.dev/api/movies?page=2
HTTP/1.1 200 OK
Link: <http://sample.dev/api/movies?page=1>;rel="first">,<http://sample.dev/api/movies?page=24>;rel="last">,<http://sample.dev/api/movies?page=3>;rel="next">,<http://sample.dev/api/movies?page=1>;rel="prev">
Content-Type: application/json; charset=utf-8
X-UA-Compatible: IE=Edge
ETag: "844a833583c5a4e4b166077c95b0bb69"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: cc557a8cbfd845cfeaac35f8e8dd0356
X-Runtime: 0.439414
@jims3ne1
jims3ne1 / gist:3921865
Created October 20, 2012 03:09
controller with set_pagination after filter 2
class Api::V2::MoviesController < Api::BaseController
after_filter :only => [:index] {set_pagination(:movies)}
def index
@movies = Movie.page params[:page]
end
def show
@movie = Movie.find params[:id]
end
@jims3ne1
jims3ne1 / gist:3921771
Last active October 11, 2015 21:27
pagination setter in the base controller
class Api::BaseController < ActionController::Base
protected
def set_pagination(name, options = {})
scope = instance_variable_get("@#{name}")
request_params = request.query_parameters
url_without_params = request.original_url.slice(0..(request.original_url.index("?")-1)) unless request_params.empty?
url_without_params ||= request.original_url
page = {}
@jims3ne1
jims3ne1 / gist:3921764
Created October 20, 2012 02:42
controller with set_pagination after filter
class Api::V2::BooksController < Api::BaseController
after_filter :only => [:index] {set_pagination(:books)}
def index
@books = Book.page params[:page]
end
end
@jims3ne1
jims3ne1 / gist:3921557
Created October 20, 2012 01:07
Pagination in the body of the response
{
"response": [{
"id": 1,
"name": "The Girl With The Dragon Tattoo"
}, {
"id": 2,
"name": "The Girl Who Played With The Fire"
},{
"id": 1,
"name": "The Girl Who Kicked the Hornet's Nest"