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.

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

attrs[RW]

Public Class Methods

new(current_page, total_pages, per_page, base_url, params = {}) click to toggle source

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

as_json() click to toggle source

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
Also aliased as: to_h
first() click to toggle source

First page url.

# File lib/json_api_server/paginator.rb, line 40
def first
  @first ||= build_url(merge_params(1))
end
last() click to toggle source

Last page url.

# File lib/json_api_server/paginator.rb, line 45
def last
  @last ||= build_url(merge_params(@total_pages))
end
meta_info() click to toggle source

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() click to toggle source

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
prev() click to toggle source

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
self() click to toggle source

Current page url.

# File lib/json_api_server/paginator.rb, line 50
def self
  @self ||= build_url(merge_params(@current_page))
end
to_h()
Alias for: as_json

Protected Instance Methods

build_url(params) click to toggle source

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
calculate_next() click to toggle source

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
calculate_prev() click to toggle source

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
merge_params(number) click to toggle source

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