This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Api::V2::BooksController < Api::BaseController | |
after_filter :only => [:index] {set_pagination(:books)} | |
def index | |
@books = Book.page params[:page] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" |