class Google::Apis::Core::PagedResults

Helper class for enumerating over a result set requiring multiple fetches

Attributes

last_result[R]

Public Class Methods

new(service, max: nil, items: :items, cache: true, &block) click to toggle source

@param [BaseService] service

Current service instance

@param [Fixnum] max

Maximum number of items to iterate over. Nil if no limit

@param [Boolean] cache

True (default) if results should be cached so multiple iterations can be used.

@param [Symbol] items

Name of the field in the result containing the items. Defaults to :items
# File lib/google/apis/core/base_service.rb, line 45
def initialize(service, max: nil, items: :items, cache: true, &block)
  @service = service
  @block = block
  @max = max
  @items_field = items
  if cache
    @result_cache = Hash.new do |h, k|
      h[k] = @block.call(k, @service)
    end
    @fetch_proc = Proc.new { |token| @result_cache[token] }
  else
    @fetch_proc = Proc.new { |token| @block.call(token, @service) }
  end
end

Public Instance Methods

each() { |item| ... } click to toggle source

Iterates over result set, fetching additional pages as needed

# File lib/google/apis/core/base_service.rb, line 61
def each
  page_token = nil
  item_count = 0
  loop do
    @last_result = @fetch_proc.call(page_token)
    for item in @last_result.send(@items_field)
      item_count = item_count + 1
      break if @max && item_count > @max
      yield item
    end
    break if @max && item_count >= @max
    break if @last_result.next_page_token.nil? || @last_result.next_page_token == page_token
    page_token = @last_result.next_page_token
  end
end