#459 - Fix iteration for genomics API which only has 1 item per page

This commit is contained in:
Steve Bazyl 2016-08-31 13:26:24 -07:00
parent 8735e62c70
commit 70596d26f6
2 changed files with 17 additions and 8 deletions

View File

@ -64,11 +64,16 @@ module Google
loop do loop do
@last_result = @fetch_proc.call(page_token) @last_result = @fetch_proc.call(page_token)
items = @last_result.send(@items_field) items = @last_result.send(@items_field)
if items.kind_of?(Array)
for item in items for item in items
item_count = item_count + 1 item_count = item_count + 1
break if @max && item_count > @max break if @max && item_count > @max
yield item yield item
end unless items.nil? end
elsif items
# yield singular non-nil items (for genomics API)
yield items
end
break if @max && item_count >= @max break if @max && item_count >= @max
break if @last_result.next_page_token.nil? || @last_result.next_page_token == page_token break if @last_result.next_page_token.nil? || @last_result.next_page_token == page_token
page_token = @last_result.next_page_token page_token = @last_result.next_page_token

View File

@ -248,9 +248,9 @@ EOF
context 'with fetch_all' do context 'with fetch_all' do
let(:responses) do let(:responses) do
data = {} data = {}
data[nil] = OpenStruct.new(next_page_token: 'p1', items: ['a', 'b', 'c'], alt_items: [1, 2 , 3]) 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]) 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]) data['p2'] = OpenStruct.new(next_page_token: nil, items: ['g', 'h', 'i'], alt_items: [7,8, 9], singular: 'baz')
data data
end 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) expect(service.fetch_all(items: :alt_items) { |token| responses[token] } ).to contain_exactly(1, 2, 3, 4, 5, 6, 7, 8, 9)
end 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 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') expect(service.fetch_all(max: 5) { |token| responses[token] } ).to contain_exactly('a', 'b', 'c', 'd', 'e')
end end