Gracefully handle nil collections when paging
This commit is contained in:
parent
d9a91c83ee
commit
edb841310f
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue