Class: JsonApiServer::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/json_api_server/paginator.rb

Overview

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"
}

Class Attribute Summary collapse

Instance Method Summary collapse

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

.attrsObject

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_jsonObject Also known as: to_h

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"
}


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

#firstObject

First page url.



40
41
42
# File 'lib/json_api_server/paginator.rb', line 40

def first
  @first ||= build_url(merge_params(1))
end

#lastObject

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_infoObject

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 meta_info
  {
    'links' => {
      'current_page' => @current_page,
      'total_pages' => @total_pages,
      'per_page' => @per_page
    }
  }
end

#nextObject

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

#prevObject

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

#selfObject

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