class ApiPack::PaginationMetaGenerator
Attributes
hash[RW]
page[R]
per_page[R]
total_pages[R]
url[RW]
Public Class Methods
new(request:, total_pages:)
click to toggle source
# File lib/api_pack/pagination_meta_generator.rb, line 7 def initialize(request:, total_pages:) @url = "#{request.base_url}#{request.path}" @page = request.params[:page].to_i @per_page = request.params[:per_page].to_i @total_pages = total_pages end
Public Instance Methods
call()
click to toggle source
# File lib/api_pack/pagination_meta_generator.rb, line 14 def call { links: links, meta: { current_page: current_page, total_pages: @total_pages } } end
Private Instance Methods
current_page()
click to toggle source
# File lib/api_pack/pagination_meta_generator.rb, line 69 def current_page return 1 if page.zero? page end
generate_url(number_page: 0)
click to toggle source
# File lib/api_pack/pagination_meta_generator.rb, line 43 def generate_url(number_page: 0) return url if number_page.zero? [url, url_params(number_page)].join("?") end
include_per_page()
click to toggle source
# File lib/api_pack/pagination_meta_generator.rb, line 57 def include_per_page return ApiPack.default_per_page if per_page.zero? per_page end
links()
click to toggle source
# File lib/api_pack/pagination_meta_generator.rb, line 30 def links links = { self: generate_url, first: generate_url(number_page: 1), last: generate_url(number_page: total_pages) } links[:next] = generate_url(number_page: next_number(page)) if current_page != total_pages links[:prev] = generate_url(number_page: page - 1) if page > 1 links end
next_number(number_page)
click to toggle source
# File lib/api_pack/pagination_meta_generator.rb, line 63 def next_number(number_page) return number_page + 2 if number_page.zero? number_page + 1 end
url_params(number_page)
click to toggle source
# File lib/api_pack/pagination_meta_generator.rb, line 49 def url_params(number_page) url_params = {} url_params[:per_page] = include_per_page url_params[:page] = number_page to_query_api(url_params) end