Sha256: ab3e3ff4ddf0ed40ff2d11758526d53d898ab4cf5445cafba6868c342abd7b1a

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module Hungry
  class Collection
    module Pagination
      class NotPaginatedError < StandardError; end
      
      def paginate(page, options = {})
        all options.merge(page: page)
      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

1 entries across 1 versions & 1 rubygems

Version Path
hungry-0.0.1 lib/hungry/collection/pagination.rb