Class: JsonApiServer::Paginator
- Inherits:
-
Object
- Object
- JsonApiServer::Paginator
- Defined in:
- lib/json_api_server/paginator.rb
Overview
Creates JSON API pagination entries per http://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": "page=3&page=5"">http://example.com/articles?page=3&page=5", "first": "page=1&page=5"">http://example.com/articles?page=1&page=5", "prev": "page=2&page=5"">http://example.com/articles?page=2&page=5", "next": "page=4&page=5"">http://example.com/articles?page=4&page=5", "last": "page=13&page=5"">http://example.com/articles?page=13&page=5" }
Class Attribute Summary collapse
-
.attrs ⇒ Object
Returns the value of attribute attrs.
Instance Method Summary collapse
-
#as_json ⇒ Object
(also: #to_h)
Returns hash: # i.e., { self: "page=3&page=5"">http://example.com/articles?page=3&page=5", first: "page=1&page=5"">http://example.com/articles?page=1&page=5", prev: "page=2&page=5"">http://example.com/articles?page=2&page=5", next: "page=4&page=5"">http://example.com/articles?page=4&page=5", last: "page=13&page=5"">http://example.com/articles?page=13&page=5" }.
-
#first ⇒ Object
First page url.
-
#initialize(current_page, total_pages, per_page, base_url, params = {}) ⇒ Paginator
constructor
Params: - current_page (Integer) - total_pages (Integer) - per_page (Integer) - base_url (String) - Base url for resource, i.e., http://example.com/articles.
-
#last ⇒ Object
Last page url.
-
#meta_info ⇒ Object
Hash with pagination meta information.
-
#next ⇒ Object
Next page url.
-
#prev ⇒ Object
Previous page url.
-
#self ⇒ Object
Current page url.
Constructor Details
#initialize(current_page, total_pages, per_page, base_url, params = {}) ⇒ Paginator
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.
27 28 29 30 31 32 33 |
# 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 |
Class Attribute Details
.attrs ⇒ Object
Returns the value of attribute attrs.
36 37 38 |
# File 'lib/json_api_server/paginator.rb', line 36 def attrs @attrs end |
Instance Method Details
#as_json ⇒ Object Also known as: to_h
Returns hash:
i.e.,
{ self: "page=3&page=5"">http://example.com/articles?page=3&page=5", first: "page=1&page=5"">http://example.com/articles?page=1&page=5", prev: "page=2&page=5"">http://example.com/articles?page=2&page=5", next: "page=4&page=5"">http://example.com/articles?page=4&page=5", last: "page=13&page=5"">http://example.com/articles?page=13&page=5" }
79 80 81 |
# 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 ⇒ Object
First page url.
40 41 42 |
# File 'lib/json_api_server/paginator.rb', line 40 def first @first ||= build_url(merge_params(1)) end |
#last ⇒ Object
Last page url.
45 46 47 |
# File 'lib/json_api_server/paginator.rb', line 45 def last @last ||= build_url(merge_params(@total_pages)) end |
#meta_info ⇒ Object
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 } }
96 97 98 99 100 101 102 103 104 |
# File 'lib/json_api_server/paginator.rb', line 96 def { 'links' => { 'current_page' => @current_page, 'total_pages' => @total_pages, 'per_page' => @per_page } } end |
#next ⇒ Object
Next page url.
55 56 57 58 59 60 |
# 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 |
#prev ⇒ Object
Previous page url.
63 64 65 66 67 68 |
# 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 |
#self ⇒ Object
Current page url.
50 51 52 |
# File 'lib/json_api_server/paginator.rb', line 50 def self @self ||= build_url(merge_params(@current_page)) end |