class Google::Apis::Core::PagedResults
Helper class for enumerating over a result set requiring multiple fetches
Attributes
Public Class Methods
Source
# File lib/google/apis/core/base_service.rb, line 46 def initialize(service, max: nil, items: :items, cache: true, response_page_token: :next_page_token, &block) @service = service @block = block @max = max @items_field = items @response_page_token_field = response_page_token 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
@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
Public Instance Methods
Source
# File lib/google/apis/core/base_service.rb, line 63 def each page_token = nil item_count = 0 loop do @last_result = @fetch_proc.call(page_token) items = @last_result.send(@items_field) if items.kind_of?(Array) for item in items item_count = item_count + 1 break if @max && item_count > @max yield item end elsif items.kind_of?(Hash) items.each do |key, val| item_count = item_count + 1 break if @max && item_count > @max yield key, val end elsif items # yield singular non-nil items (for genomics API) yield items end break if @max && item_count >= @max next_page_token = @last_result.send(@response_page_token_field) break if next_page_token.to_s.empty? || next_page_token == page_token page_token = next_page_token end end
Iterates over result set, fetching additional pages as needed