Skip to content

Commit f5a1e7b

Browse files
authored
Merge pull request #263 from intercom/readme-update
Allow pagination for Ruby SDK
2 parents fbe2e38 + ee8be5b commit f5a1e7b

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

lib/intercom/client_collection_proxy.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@ def initialize(resource_name, finder_details: {}, client:)
1616

1717
def each(&block)
1818
next_page = nil
19+
current_page = nil
1920
loop do
2021
if next_page
2122
response_hash = @client.get(next_page, {})
2223
else
2324
response_hash = @client.get(@finder_url, @finder_params)
2425
end
2526
raise Intercom::HttpError.new('Http Error - No response entity returned') unless response_hash
27+
current_page = extract_current_page(response_hash)
2628
deserialize_response_hash(response_hash, block)
2729
next_page = extract_next_link(response_hash)
28-
break if next_page.nil?
30+
break if next_page.nil? or (@finder_params[:page] and (current_page >= @finder_params[:page]))
2931
end
3032
self
3133
end
@@ -62,5 +64,10 @@ def extract_next_link(response_hash)
6264
paging_info = response_hash.delete('pages')
6365
paging_info["next"]
6466
end
67+
68+
def extract_current_page(response_hash)
69+
return nil unless paging_info_present?(response_hash)
70+
response_hash['pages']['page']
71+
end
6572
end
6673
end

spec/spec_helper.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,61 @@ def test_user(email="[email protected]")
5757
}
5858
end
5959

60+
def test_user_dates(email="[email protected]", created_at=1401970114, last_request_at=1401970113)
61+
{
62+
"type" =>"user",
63+
"id" =>"aaaaaaaaaaaaaaaaaaaaaaaa",
64+
"user_id" => 'id-from-customers-app',
65+
"email" => email,
66+
"name" => "Joe Schmoe",
67+
"avatar" => {"type"=>"avatar", "image_url"=>"https://graph.facebook.com/1/picture?width=24&height=24"},
68+
"app_id" => "the-app-id",
69+
"custom_attributes" => {"a" => "b", "b" => 2},
70+
"companies" =>
71+
{"type"=>"company.list",
72+
"companies"=>
73+
[{"type"=>"company",
74+
"company_id"=>"123",
75+
"id"=>"bbbbbbbbbbbbbbbbbbbbbbbb",
76+
"app_id"=>"the-app-id",
77+
"name"=>"Company 1",
78+
"remote_created_at"=>1390936440,
79+
"created_at"=>1401970114,
80+
"updated_at"=>1401970114,
81+
"last_request_at"=>1401970113,
82+
"monthly_spend"=>0,
83+
"session_count"=>0,
84+
"user_count"=>1,
85+
"tag_ids"=>[],
86+
"custom_attributes"=>{"category"=>"Tech"}}]},
87+
"session_count" => 123,
88+
"unsubscribed_from_emails" => true,
89+
"last_request_at" =>last_request_at,
90+
"created_at" =>created_at,
91+
"remote_created_at" =>1393613864,
92+
"updated_at" =>1401970114,
93+
"user_agent_data" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
94+
"social_profiles" =>{"type"=>"social_profile.list",
95+
"social_profiles" => [
96+
{"type" => "social_profile", "name" => "twitter", "url" => "http://twitter.com/abc", "username" => "abc", "id" => nil},
97+
{"type" => "social_profile", "name" => "twitter", "username" => "abc2", "url" => "http://twitter.com/abc2", "id" => nil},
98+
{"type" => "social_profile", "name" => "facebook", "url" => "http://facebook.com/abc", "username" => "abc", "id" => "1234242"},
99+
{"type" => "social_profile", "name" => "quora", "url" => "http://facebook.com/abc", "username" => "abc", "id" => "1234242"}
100+
]},
101+
"location_data"=>
102+
{"type"=>"location_data",
103+
"city_name"=> 'Dublin',
104+
"continent_code"=> 'EU',
105+
"country_name"=> 'Ireland',
106+
"latitude"=> '90',
107+
"longitude"=> '10',
108+
"postal_code"=> 'IE',
109+
"region_name"=> 'Europe',
110+
"timezone"=> '+1000',
111+
"country_code" => "IRL"}
112+
}
113+
end
114+
60115
def test_admin_list
61116
{
62117
"type" => "admin.list",
@@ -161,6 +216,23 @@ def page_of_users(include_next_link= false)
161216
}
162217
end
163218

219+
def users_pagination(include_next_link=false, per_page=0, page=0, total_pages=0, total_count=0, user_list=[])
220+
{
221+
"type"=>"user.list",
222+
"pages"=>
223+
{
224+
"type"=>"pages",
225+
"next"=> (include_next_link ? "https://api.intercom.io/users?per_page=" \
226+
+ per_page.to_s + "&page=" + (page+1).to_s : nil),
227+
"page"=>page,
228+
"per_page"=>per_page,
229+
"total_pages"=>total_pages
230+
},
231+
"users"=> user_list,
232+
"total_count"=>total_count
233+
}
234+
end
235+
164236
def test_conversation
165237
{
166238
"type" => "conversation",

spec/unit/intercom/client_collection_proxy_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,49 @@
3232
client.expects(:get).with("/users", {:tag_name => 'Taggart J'}).returns(page_of_users(false))
3333
client.users.find_all(:tag_name => 'Taggart J').map(&:email).must_equal %W([email protected] [email protected] [email protected])
3434
end
35+
36+
it "supports single page pagination" do
37+
users = [test_user("[email protected]"), test_user("[email protected]"), test_user("[email protected]"),
38+
test_user("[email protected]"), test_user("[email protected]"), test_user("[email protected]"),
39+
test_user("[email protected]"), test_user("[email protected]"), test_user("[email protected]"),
40+
test_user("[email protected]")]
41+
client.expects(:get).with("/users", {:type=>'users', :per_page => 10, :page => 1}).returns(users_pagination(false, per_page=10, page=1, total_pages=1, total_count=10, user_list=users))
42+
result = client.users.find_all(:type=>'users', :per_page => 10, :page => 1).map {|user| user.email }
43+
44+
end
45+
46+
it "supports multi page pagination" do
47+
users = [test_user("[email protected]"), test_user("[email protected]")]
48+
client.expects(:get).with("/users", {:type=>'users', :per_page => 2, :page => 3}).returns(users_pagination(true, per_page=2, page=3, total_pages=5, total_count=10, user_list=users))
49+
result = client.users.find_all(:type=>'users', :per_page => 2, :page => 3).map {|user| user.email }
50+
result.must_equal %W([email protected] [email protected])
51+
end
52+
53+
it "works with page out of range request" do
54+
users = []
55+
client.expects(:get).with("/users", {:type=>'users', :per_page => 2, :page => 30}).returns(users_pagination(true, per_page=2, page=30, total_pages=2, total_count=3, user_list=users))
56+
result = client.users.find_all(:type=>'users', :per_page => 2, :page => 30).map {|user| user.email }
57+
result.must_equal %W()
58+
end
59+
60+
it "works with asc order" do
61+
test_date=1457337600
62+
time_increment=1000
63+
users = [test_user_dates(email="[email protected]", created_at=test_date), test_user_dates(email="[email protected]", created_at=test_date-time_increment),
64+
test_user_dates(email="[email protected]", created_at=test_date-2*time_increment), test_user_dates(email="[email protected]", created_at=test_date-3*time_increment)]
65+
client.expects(:get).with("/users", {:type=>'users', :per_page => 4, :page => 5, :order => "asc", :sort => "created_at"}).returns(users_pagination(true, per_page=4, page=5, total_pages=6, total_count=30, user_list=users))
66+
result = client.users.find_all(:type=>'users', :per_page => 4, :page => 5, :order => "asc", :sort => "created_at").map(&:email)
67+
68+
end
69+
70+
it "works with desc order" do
71+
test_date=1457337600
72+
time_increment=1000
73+
users = [test_user_dates(email="[email protected]", created_at=3*test_date), test_user_dates(email="[email protected]", created_at=test_date-2*time_increment),
74+
test_user_dates(email="[email protected]", created_at=test_date-time_increment), test_user_dates(email="[email protected]", created_at=test_date)]
75+
client.expects(:get).with("/users", {:type=>'users', :per_page => 4, :page => 5, :order => "desc", :sort => "created_at"}).returns(users_pagination(true, per_page=4, page=5, total_pages=6, total_count=30, user_list=users))
76+
result = client.users.find_all(:type=>'users', :per_page => 4, :page => 5, :order => "desc", :sort => "created_at").map {|user| user.email }
77+
78+
end
79+
3580
end

0 commit comments

Comments
 (0)