Skip to content

Commit 095caa0

Browse files
joao-coelhoJoão CoelhoSeanHealy33
authored
Fix collection successive iterations when there are multiple pages (#581)
Co-authored-by: João Coelho <[email protected]> Co-authored-by: Sean Healy <[email protected]>
1 parent 9135607 commit 095caa0

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/intercom/base_collection_proxy.rb

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def has_next_link?(response_hash)
5858
@params[:starting_after] = paging_next['starting_after']
5959
return true
6060
else
61+
@params[:starting_after] = nil
6162
return false
6263
end
6364
end

spec/unit/intercom/base_collection_proxy_spec.rb

+18-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
let(:client) { Intercom::Client.new(token: 'token') }
55

66
it "stops iterating if no starting after value" do
7-
client.expects(:get).with("/contacts", {}). returns(page_of_contacts(false))
7+
client.expects(:get).with("/contacts", {}).returns(page_of_contacts(false))
88
emails = []
99
client.contacts.all.each { |contact| emails << contact.email }
1010
@@ -15,6 +15,7 @@
1515
client.expects(:get).with('/contacts', { starting_after: "EnCrYpTeDsTrInG" }).returns(page_of_contacts(false))
1616
emails = []
1717
client.contacts.all.each { |contact| emails << contact.email }
18+
1819
end
1920

2021
it "supports indexed array access" do
@@ -27,4 +28,20 @@
2728
emails = client.contacts.all.map { |contact| contact.email }
2829
2930
end
31+
32+
it "keeps entire collection iterable after first iteration" do
33+
contacts = client.contacts.all
34+
emails_iter1 = []
35+
emails_iter2 = []
36+
expects_pagination = proc do
37+
client.expects(:get).with("/contacts", {}).returns(page_of_contacts(true))
38+
client.expects(:get).with("/contacts", { starting_after: "EnCrYpTeDsTrInG" }).returns(page_of_contacts(false))
39+
end
40+
41+
expects_pagination.call
42+
contacts.each { |contact| emails_iter1 << contact.email }
43+
expects_pagination.call
44+
contacts.each { |contact| emails_iter2 << contact.email }
45+
_(emails_iter1).must_equal emails_iter2
46+
end
3047
end

0 commit comments

Comments
 (0)