diff --git a/lib/google/apis/core/base_service.rb b/lib/google/apis/core/base_service.rb index 9065a389a..72e7de39f 100644 --- a/lib/google/apis/core/base_service.rb +++ b/lib/google/apis/core/base_service.rb @@ -64,11 +64,16 @@ module Google loop do @last_result = @fetch_proc.call(page_token) items = @last_result.send(@items_field) - for item in items - item_count = item_count + 1 - break if @max && item_count > @max - yield item - end unless items.nil? + 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 diff --git a/spec/google/apis/core/service_spec.rb b/spec/google/apis/core/service_spec.rb index ae5fa6d44..aecf971ec 100644 --- a/spec/google/apis/core/service_spec.rb +++ b/spec/google/apis/core/service_spec.rb @@ -248,9 +248,9 @@ EOF context 'with fetch_all' do let(:responses) do data = {} - data[nil] = OpenStruct.new(next_page_token: 'p1', items: ['a', 'b', 'c'], alt_items: [1, 2 , 3]) - data['p1'] = OpenStruct.new(next_page_token: 'p2', items: ['d', 'e', 'f'], alt_items: [4, 5, 6]) - data['p2'] = OpenStruct.new(next_page_token: nil, items: ['g', 'h', 'i'], alt_items: [7,8, 9]) + data[nil] = OpenStruct.new(next_page_token: 'p1', items: ['a', 'b', 'c'], alt_items: [1, 2 , 3], singular: 'foo') + data['p1'] = OpenStruct.new(next_page_token: 'p2', items: ['d', 'e', 'f'], alt_items: [4, 5, 6], singular: 'bar') + data['p2'] = OpenStruct.new(next_page_token: nil, items: ['g', 'h', 'i'], alt_items: [7,8, 9], singular: 'baz') data end @@ -274,6 +274,10 @@ EOF expect(service.fetch_all(items: :alt_items) { |token| responses[token] } ).to contain_exactly(1, 2, 3, 4, 5, 6, 7, 8, 9) end + it 'should allow iterating over singular items' do + expect(service.fetch_all(items: :singular) { |token| responses[token] } ).to contain_exactly('foo', 'bar', 'baz') + end + it 'should allow limiting the number of items to fetch' do expect(service.fetch_all(max: 5) { |token| responses[token] } ).to contain_exactly('a', 'b', 'c', 'd', 'e') end