Sha256: f879609e4ea18cb0e5145dc93aba790a0bb9c398eaea4d5b8cfe0027bc4d712e

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

module JsonApiClient
  module Legacy
    class Paginator
      attr_reader :meta, :result_set
      def initialize(result_set, meta)
        @meta = meta
        @result_set = result_set
      end

      def next
        raise NotImplementedError
      end

      def prev
        raise NotImplementedError
      end

      def first
        raise NotImplementedError
      end

      def last
        raise NotImplementedError
      end

      def total_pages
        meta.fetch("total_pages") do
          (1.0 * total_entries / per_page).ceil rescue 1
        end
      end

      def total_entries
        meta.fetch("total_entries") do
          result_set.length
        end
      end

      def offset
        meta.fetch("offset") do
          per_page * (current_page - 1)
        end
      end

      def per_page
        meta.fetch("per_page") do
          result_set.length
        end
      end

      def current_page
        meta.fetch("current_page") do
          meta.fetch("page", 1)
        end
      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
json_api_client-legacy-1.0.0.beta lib/json_api_client/legacy/paginator.rb