Sha256: e7701f906f93bf0bcd393feba36c34f647eb06837ab967f89ccc525faf962f9e

Contents?: true

Size: 803 Bytes

Versions: 2

Compression:

Stored size: 803 Bytes

Contents

module Clever
  module APIOperations
    # Represents a page of data
    class Page
      include Enumerable

      def initialize(uri, filters = {})
        @uri = uri
        @filters = filters

        response = Clever.request :get, uri, filters
        @list = Util.convert_to_clever_object response[:data]
        @links = {}
        response[:links].each do |link|
          @links[link[:rel].to_sym] = link[:uri]
        end
      end

      # Gets next page if one is present, nil otherwise.
      def next
        @links.key?(:next) ? Page.new(@links[:next]) : nil
      end

      def each(&blk)
        @list.each(&blk)
      end

      # TODO: remove this
      def all
        accum = []
        each do |elem|
          accum << elem
        end
        accum
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
clever-ruby-0.6.0 lib/clever-ruby/api_operations/page.rb
clever-ruby-0.5.0 lib/clever-ruby/api_operations/page.rb