Class: Google::Apis::Core::PagedResults
- Inherits:
-
Object
- Object
- Google::Apis::Core::PagedResults
- Includes:
- Enumerable
- Defined in:
- lib/google/apis/core/base_service.rb
Overview
Helper class for enumerating over a result set requiring multiple fetches
Instance Attribute Summary collapse
-
#last_result ⇒ Object
readonly
Returns the value of attribute last_result.
Instance Method Summary collapse
-
#each ⇒ Object
Iterates over result set, fetching additional pages as needed.
-
#initialize(service, max: nil, items: :items, cache: true, &block) ⇒ PagedResults
constructor
A new instance of PagedResults.
Constructor Details
#initialize(service, max: nil, items: :items, cache: true, &block) ⇒ PagedResults
Returns a new instance of PagedResults.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# 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 |
Instance Attribute Details
#last_result ⇒ Object (readonly)
Returns the value of attribute last_result.
35 36 37 |
# File 'lib/google/apis/core/base_service.rb', line 35 def last_result @last_result end |
Instance Method Details
#each ⇒ Object
Iterates over result set, fetching additional pages as needed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# 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) 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 # yield singular non-nil items (for genomics API) yield items 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 |