class FreckleIO::Paginator

Attributes

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