Gracefully handle nil collections when paging

This commit is contained in:
Steve Bazyl 2016-06-23 12:43:37 -07:00
parent d9a91c83ee
commit edb841310f
2 changed files with 8 additions and 3 deletions

View File

@ -63,11 +63,12 @@ module Google
item_count = 0
loop do
@last_result = @fetch_proc.call(page_token)
for item in @last_result.send(@items_field)
items = @last_result.send(@items_field)
for item in items
item_count = item_count + 1
break if @max && item_count > @max
yield item
end
end unless items.nil?
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

View File

@ -265,6 +265,11 @@ EOF
expect(items).to contain_exactly('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i')
end
it 'should ignore nil collections' do
responses['p1'].items = nil
expect(items).to contain_exactly('a', 'b', 'c', 'g', 'h', 'i')
end
it 'should allow selecting another field for items' do
expect(service.fetch_all(items: :alt_items) { |token| responses[token] } ).to contain_exactly(1, 2, 3, 4, 5, 6, 7, 8, 9)
end
@ -307,7 +312,6 @@ EOF
expect(count).to eq 6
end
end
end
end