Sha256: efe3cc2f177db7e8f38a774372e8de7626bec8d0475f03c8fd3a3c39b6c416ba
Contents?: true
Size: 1.61 KB
Versions: 9
Compression:
Stored size: 1.61 KB
Contents
module JsonApiClient module Paginating class Paginator attr_reader :params, :result_set, :links def initialize(result_set, data) @params = params_for_uri(result_set.uri) @result_set = result_set @links = data['links'] end def next result_set.links.fetch_link("next") end def prev result_set.links.fetch_link("next") end def first result_set.links.fetch_link("first") end def last result_set.links.fetch_link("last") end def total_pages if links["last"] last_params = params_for_uri(links["last"]) last_params.fetch("page") do current_page end.to_i else current_page end end # this number may be off def total_entries per_page * total_pages end alias_method :total_count, :total_entries def offset per_page * (current_page - 1) end def per_page params.fetch("per_page") do result_set.length end.to_i end def current_page params.fetch("page", 1).to_i end def out_of_bounds? current_page > total_pages end def previous_page current_page > 1 ? (current_page - 1) : nil end def next_page current_page < total_pages ? (current_page + 1) : nil end alias limit_value per_page protected def params_for_uri(uri) return {} unless uri uri = Addressable::URI.parse(uri) uri.query_values || {} end end end end
Version data entries
9 entries across 9 versions & 1 rubygems