class FreckleIO::Paginator
Attributes
raw_links[R]
Public Class Methods
new(raw_links)
click to toggle source
# File lib/freckle_io/paginator.rb, line 8 def initialize(raw_links) @raw_links = raw_links end
Public Instance Methods
first()
click to toggle source
# File lib/freckle_io/paginator.rb, line 20 def first find("first")&.dig(:url) end
last()
click to toggle source
# File lib/freckle_io/paginator.rb, line 24 def last find("last")&.dig(:url) end
next()
click to toggle source
# File lib/freckle_io/paginator.rb, line 12 def next find("next")&.dig(:url) end
prev()
click to toggle source
# File lib/freckle_io/paginator.rb, line 16 def prev find("prev")&.dig(:url) end
total_pages()
click to toggle source
# File lib/freckle_io/paginator.rb, line 28 def total_pages find("last")&.dig(:number_page) end
Private Instance Methods
find(rel)
click to toggle source
# File lib/freckle_io/paginator.rb, line 34 def find(rel) pages.find { |page| page[:rel] == rel } end
pages()
click to toggle source
# File lib/freckle_io/paginator.rb, line 38 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.letsfreckle.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/freckle_io/paginator.rb, line 59 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