Sha256: db593eea960bb8ae98e26e7815f8ab1345084a955e78c21a025d3aaeadd4a26e

Contents?: true

Size: 1.8 KB

Versions: 4

Compression:

Stored size: 1.8 KB

Contents

module Hungry
  class Collection
    module Pagination
      class NotPaginatedError < StandardError; end

      def paginate(page, options = {})
        all options.merge(page: page)
      end

      def first(n = 1)
        scope   = all(per_page: n, page: 1)
        results = scope.results

        if n == 1 && (value = results.first)
          resource = klass.new results.first
          resource.data_source = scope.data_source
          resource
        elsif n > 1
          results.first(n).map do |result|
            resource = klass.new result
            resource.data_source = scope.data_source
            resource
          end
        end
      end

      def paginated?
        pagination.present?
      end

      def per_page
        if paginated?
          pagination['per_page'].to_i
        else
          response['results'].length
        end
      end

      def total_entries
        if paginated?
          pagination['total_entries'].to_i
        else
          response['results'].length
        end
      end

      def total_pages
        if paginated?
          pagination['total_pages'].to_i
        else
          1
        end
      end

      def current_page
        if paginated?
          pagination['current_page'].to_i
        else
          1
        end
      end

      def previous_page
        current_page - 1 if paginated? && current_page > 1
      end

      def next_page
        current_page + 1 if paginated? && current_page < total_pages
      end

      def previous
        raise NotPaginatedError unless paginated?
        all page: previous_page if previous_page
      end

      def next
        raise NotPaginatedError unless paginated?
        all page: next_page if next_page
      end

      private

      def pagination
        response['pagination']
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hungry-0.2.0 lib/hungry/collection/pagination.rb
hungry-0.1.5 lib/hungry/collection/pagination.rb
hungry-0.1.4 lib/hungry/collection/pagination.rb
hungry-0.1.3 lib/hungry/collection/pagination.rb