Skip to content

Commit

Permalink
Merge pull request #263 from intercom/readme-update
Browse files Browse the repository at this point in the history
Allow pagination for Ruby SDK
  • Loading branch information
choran authored Jul 8, 2016
2 parents fbe2e38 + ee8be5b commit f5a1e7b
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/intercom/client_collection_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ def initialize(resource_name, finder_details: {}, client:)

def each(&block)
next_page = nil
current_page = nil
loop do
if next_page
response_hash = @client.get(next_page, {})
else
response_hash = @client.get(@finder_url, @finder_params)
end
raise Intercom::HttpError.new('Http Error - No response entity returned') unless response_hash
current_page = extract_current_page(response_hash)
deserialize_response_hash(response_hash, block)
next_page = extract_next_link(response_hash)
break if next_page.nil?
break if next_page.nil? or (@finder_params[:page] and (current_page >= @finder_params[:page]))
end
self
end
Expand Down Expand Up @@ -62,5 +64,10 @@ def extract_next_link(response_hash)
paging_info = response_hash.delete('pages')
paging_info["next"]
end

def extract_current_page(response_hash)
return nil unless paging_info_present?(response_hash)
response_hash['pages']['page']
end
end
end
72 changes: 72 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,61 @@ def test_user(email="[email protected]")
}
end

def test_user_dates(email="[email protected]", created_at=1401970114, last_request_at=1401970113)
{
"type" =>"user",
"id" =>"aaaaaaaaaaaaaaaaaaaaaaaa",
"user_id" => 'id-from-customers-app',
"email" => email,
"name" => "Joe Schmoe",
"avatar" => {"type"=>"avatar", "image_url"=>"https://graph.facebook.com/1/picture?width=24&height=24"},
"app_id" => "the-app-id",
"custom_attributes" => {"a" => "b", "b" => 2},
"companies" =>
{"type"=>"company.list",
"companies"=>
[{"type"=>"company",
"company_id"=>"123",
"id"=>"bbbbbbbbbbbbbbbbbbbbbbbb",
"app_id"=>"the-app-id",
"name"=>"Company 1",
"remote_created_at"=>1390936440,
"created_at"=>1401970114,
"updated_at"=>1401970114,
"last_request_at"=>1401970113,
"monthly_spend"=>0,
"session_count"=>0,
"user_count"=>1,
"tag_ids"=>[],
"custom_attributes"=>{"category"=>"Tech"}}]},
"session_count" => 123,
"unsubscribed_from_emails" => true,
"last_request_at" =>last_request_at,
"created_at" =>created_at,
"remote_created_at" =>1393613864,
"updated_at" =>1401970114,
"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",
"social_profiles" =>{"type"=>"social_profile.list",
"social_profiles" => [
{"type" => "social_profile", "name" => "twitter", "url" => "http://twitter.com/abc", "username" => "abc", "id" => nil},
{"type" => "social_profile", "name" => "twitter", "username" => "abc2", "url" => "http://twitter.com/abc2", "id" => nil},
{"type" => "social_profile", "name" => "facebook", "url" => "http://facebook.com/abc", "username" => "abc", "id" => "1234242"},
{"type" => "social_profile", "name" => "quora", "url" => "http://facebook.com/abc", "username" => "abc", "id" => "1234242"}
]},
"location_data"=>
{"type"=>"location_data",
"city_name"=> 'Dublin',
"continent_code"=> 'EU',
"country_name"=> 'Ireland',
"latitude"=> '90',
"longitude"=> '10',
"postal_code"=> 'IE',
"region_name"=> 'Europe',
"timezone"=> '+1000',
"country_code" => "IRL"}
}
end

def test_admin_list
{
"type" => "admin.list",
Expand Down Expand Up @@ -161,6 +216,23 @@ def page_of_users(include_next_link= false)
}
end

def users_pagination(include_next_link=false, per_page=0, page=0, total_pages=0, total_count=0, user_list=[])
{
"type"=>"user.list",
"pages"=>
{
"type"=>"pages",
"next"=> (include_next_link ? "https://api.intercom.io/users?per_page=" \
+ per_page.to_s + "&page=" + (page+1).to_s : nil),
"page"=>page,
"per_page"=>per_page,
"total_pages"=>total_pages
},
"users"=> user_list,
"total_count"=>total_count
}
end

def test_conversation
{
"type" => "conversation",
Expand Down
45 changes: 45 additions & 0 deletions spec/unit/intercom/client_collection_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,49 @@
client.expects(:get).with("/users", {:tag_name => 'Taggart J'}).returns(page_of_users(false))
client.users.find_all(:tag_name => 'Taggart J').map(&:email).must_equal %W([email protected] [email protected] [email protected])
end

it "supports single page pagination" do
users = [test_user("[email protected]"), test_user("[email protected]"), test_user("[email protected]"),
test_user("[email protected]"), test_user("[email protected]"), test_user("[email protected]"),
test_user("[email protected]"), test_user("[email protected]"), test_user("[email protected]"),
test_user("[email protected]")]
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))
result = client.users.find_all(:type=>'users', :per_page => 10, :page => 1).map {|user| user.email }
result.must_equal %W([email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected])
end

it "supports multi page pagination" do
users = [test_user("[email protected]"), test_user("[email protected]")]
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))
result = client.users.find_all(:type=>'users', :per_page => 2, :page => 3).map {|user| user.email }
result.must_equal %W([email protected] [email protected])
end

it "works with page out of range request" do
users = []
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))
result = client.users.find_all(:type=>'users', :per_page => 2, :page => 30).map {|user| user.email }
result.must_equal %W()
end

it "works with asc order" do
test_date=1457337600
time_increment=1000
users = [test_user_dates(email="[email protected]", created_at=test_date), test_user_dates(email="[email protected]", created_at=test_date-time_increment),
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)]
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))
result = client.users.find_all(:type=>'users', :per_page => 4, :page => 5, :order => "asc", :sort => "created_at").map(&:email)
result.must_equal %W([email protected] [email protected] [email protected] [email protected])
end

it "works with desc order" do
test_date=1457337600
time_increment=1000
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),
test_user_dates(email="[email protected]", created_at=test_date-time_increment), test_user_dates(email="[email protected]", created_at=test_date)]
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))
result = client.users.find_all(:type=>'users', :per_page => 4, :page => 5, :order => "desc", :sort => "created_at").map {|user| user.email }
result.must_equal %W([email protected] [email protected] [email protected] [email protected])
end

end

0 comments on commit f5a1e7b

Please sign in to comment.