class JsonApiServer::Paginator
Creates JSON API pagination entries per jsonapi.org/examples/#pagination.
JsonApiServer::Paginator#as_json
generates a hash like the following which can be added to JsonApiServer::BaseSerializer#links
section.
-
'next' is nil when self is the last page.
-
'prev' is nil when self is the first page.
Example:¶ ↑
"links": { "self": "http://example.com/articles?page[number]=3&page[limit]=5", "first": "http://example.com/articles?page[number]=1&page[limit]=5", "prev": "http://example.com/articles?page[number]=2&page[limit]=5", "next": "http://example.com/articles?page[number]=4&page[limit]=5", "last": "http://example.com/articles?page[number]=13&page[limit]=5" }
Attributes
Public Class Methods
Params:
-
current_page
(Integer) -
total_pages
(Integer) -
per_page
(Integer) -
base_url
(String) - Base url for resource, i.e.,http://example.com/articles
. -
params
(Hash) - Request parameters.Pagination
params are merged into these.
# File lib/json_api_server/paginator.rb, line 27 def initialize(current_page, total_pages, per_page, base_url, params = {}) @current_page = current_page @total_pages = total_pages @per_page = per_page @base_url = base_url @params = params end
Public Instance Methods
Returns hash:
# i.e., { self: "http://example.com/articles?page[number]=3&page[limit]=5", first: "http://example.com/articles?page[number]=1&page[limit]=5", prev: "http://example.com/articles?page[number]=2&page[limit]=5", next: "http://example.com/articles?page[number]=4&page[limit]=5", last: "http://example.com/articles?page[number]=13&page[limit]=5" }
# File lib/json_api_server/paginator.rb, line 79 def as_json self.class.attrs.each_with_object({}) { |attr, acc| acc[attr] = send(attr); } end
First page url.
# File lib/json_api_server/paginator.rb, line 40 def first @first ||= build_url(merge_params(1)) end
Last page url.
# File lib/json_api_server/paginator.rb, line 45 def last @last ||= build_url(merge_params(@total_pages)) end
Hash with pagination meta information. Useful for user interfaces, i.e., 'page #{current_page} of #{total_pages}'.
#i.e., { links: { current_page: 2, total_pages: 13, per_page: 5 } }
# File lib/json_api_server/paginator.rb, line 96 def meta_info { 'links' => { 'current_page' => @current_page, 'total_pages' => @total_pages, 'per_page' => @per_page } } end
Next page url.
# File lib/json_api_server/paginator.rb, line 55 def next @next ||= begin n = calculate_next n.nil? ? nil : build_url(merge_params(n)) end end
Previous page url.
# File lib/json_api_server/paginator.rb, line 63 def prev @prev ||= begin p = calculate_prev p.nil? ? nil : build_url(merge_params(p)) end end
Current page url.
# File lib/json_api_server/paginator.rb, line 50 def self @self ||= build_url(merge_params(@current_page)) end
Protected Instance Methods
Merges base_url with modified params. Params are Url encoded, i.e., page%5Blimit%5D=5&page%5Bnumber%5D=1
# File lib/json_api_server/paginator.rb, line 116 def build_url(params) "#{@base_url}?#{params.to_query}" end
Calculates next page. Returns nil when value is invalid, i.e., exceeds total_pages.
# File lib/json_api_server/paginator.rb, line 122 def calculate_next n = @current_page + 1 n > @total_pages || n <= 0 ? nil : n end
Calculates previous page. Returns nil if value is invalid, i.e., less than or equal to 0.
# File lib/json_api_server/paginator.rb, line 129 def calculate_prev p = @current_page - 1 p <= 0 || p > @total_pages ? nil : p end
Merges pagination params with request params. Pagination
params look like this to page=x&page=y.
# File lib/json_api_server/paginator.rb, line 110 def merge_params(number) @params.merge(page: { number: number, limit: @per_page }) end