class Nokotime::Paginator
Attributes
raw_links[R]
Public Class Methods
new(raw_links)
click to toggle source
# File lib/nokotime/paginator.rb, line 5 def initialize(raw_links) @raw_links = raw_links end
Public Instance Methods
first()
click to toggle source
# File lib/nokotime/paginator.rb, line 17 def first find("first")&.dig(:url) end
last()
click to toggle source
# File lib/nokotime/paginator.rb, line 21 def last find("last")&.dig(:url) end
next()
click to toggle source
# File lib/nokotime/paginator.rb, line 9 def next find("next")&.dig(:url) end
prev()
click to toggle source
# File lib/nokotime/paginator.rb, line 13 def prev find("prev")&.dig(:url) end
total_pages()
click to toggle source
# File lib/nokotime/paginator.rb, line 25 def total_pages find("last")&.dig(:number_page) end
Private Instance Methods
find(rel)
click to toggle source
# File lib/nokotime/paginator.rb, line 31 def find(rel) pages.find { |page| page[:rel] == rel } end
pages()
click to toggle source
# File lib/nokotime/paginator.rb, line 35 def pages return {} if raw_links.empty? @pages ||= raw_links.split(",").map do |link| url, rel, number_page = split_and_clean_link(link) { url: "#{url.path}?#{url.query}", rel: rel, number_page: number_page } end end
split_and_clean_link(link)
click to toggle source
Example of link response headers: β<api.nokotime.com/v2/users?page=2>; rel="last"β
The first element is an URI. βRelβ means the relative position of the current page, in this case is last page.
# File lib/nokotime/paginator.rb, line 56 def split_and_clean_link(link) raw_url, rel = link.split(";").map(&:strip) raw_url = raw_url.gsub(/<|>/, "") rel = rel.gsub(/(rel=)?"/, "") url = URI.parse(raw_url) page = CGI.parse(url.query)["page"].first [url, rel, page] end