Skip to content

Commit de90c3f

Browse files
committed
Merge pull request intercom#7 from timcraft/user-collection-proxy-fixes
User collection proxy fixes
2 parents 351781a + 2ca5cc5 commit de90c3f

File tree

4 files changed

+35
-29
lines changed

4 files changed

+35
-29
lines changed

lib/intercom/user.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ module Intercom
88
#
99
# == Example usage
1010
# * Fetching a user
11-
# Intercom::User.find_by_email("bob@example.")
11+
# Intercom::User.find_by_email("bob@example.com")
1212
#
1313
# * Getting the count of all users
1414
# Intercom::User.all.count
1515
#
1616
# * Fetching all users
17-
# Intercom::User.all.each {|user| puts user.email }
17+
# Intercom::User.all.each { |user| puts user.email }
1818
#
1919
# * Updating custom data on a user
2020
# user = Intercom::User.find_by_email("bob@example.com")
@@ -68,20 +68,29 @@ def self.create(params)
6868

6969
# Retrieve all the users
7070
# Examples:
71-
# Intercom::User.all.count
72-
# > 5346
73-
# Intercom::User.each do |user|
71+
# Intercom::User.all.each do |user|
7472
# puts user.inspect
7573
# end
7674
# > ["user1@example.com" ,"user2@example.com" ,....]
77-
# Intercom::User.map(&:email)
75+
# Intercom::User.all.map(&:email)
7876
# > ["user1@example.com" ,"user2@example.com" ,....]
7977
#
8078
# @return [UserCollectionProxy]
8179
def self.all
8280
UserCollectionProxy.new
8381
end
8482

83+
# Fetches a count of all Users tracked on Intercom.
84+
# Example:
85+
# Intercom::User.all.count
86+
# > 5346
87+
#
88+
# @return [Integer]
89+
def self.count
90+
response = Intercom.get("/v1/users", {:per_page => 1})
91+
response["total_count"]
92+
end
93+
8594
# Deletes a user record on your application.
8695
#
8796
# Calls DELETE https://api.intercom.io/v1/users

lib/intercom/user_collection_proxy.rb

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,12 @@ module Intercom
88
#
99
# == Examples:
1010
#
11-
# Fetching a count of all Users tracked on Intercom
12-
# Intercom::User.all.count
13-
#
1411
# Iterating over each user
15-
# Intercom::User.each do |user|
12+
# Intercom::User.all.each do |user|
1613
# puts user.inspect
1714
# end
1815
#
1916
class UserCollectionProxy
20-
# @return [Integer] number of users tracked on Intercom for this application
21-
def count
22-
response = Intercom.get("/v1/users", {:per_page => 1})
23-
response["total_count"]
24-
end
25-
2617
# yields each {User} to the block provided
2718
# @return [void]
2819
def each(&block)
@@ -38,14 +29,15 @@ def each(&block)
3829
end
3930
end
4031

41-
# yields each {User} to the block provided and collects the output in the same way as Enumerable#map
42-
# @return [Array<Object>]
43-
def map
44-
out = []
45-
each { |e| out << yield(e) }
46-
out
47-
end
32+
include Enumerable
33+
34+
# This method exists as an optimisation of Enumerable#count,
35+
# which would potentially fetch multiple pages of users.
36+
def count(item=nil) #:nodoc:
37+
return super unless item.nil?
38+
return super if block_given?
4839

49-
alias :collect :map
40+
Intercom::User.count
41+
end
5042
end
5143
end

spec/unit/intercom/user_collection_proxy_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
Intercom::User.all.collect { |user| user.email }.must_equal %W(user1@example.com user2@example.com user3@example.com)
1919
end
2020

21-
it "yields each user to the block" do
22-
Intercom.expects(:get).with("/v1/users", {:per_page => 1}).returns(page_of_users(1,1))
23-
Intercom::User.all.count.must_equal 3
24-
end
25-
2621
it "loads multiple pages" do
2722
Intercom.expects(:get).with("/v1/users", {:page => 1}).returns(page_of_users(1, 1))
2823
Intercom.expects(:get).with("/v1/users", {:page => 2}).returns(page_of_users(2, 1))
2924
Intercom.expects(:get).with("/v1/users", {:page => 3}).returns(page_of_users(3, 1))
3025
Intercom::User.all.map { |user| user.email }.must_equal %W(user1@example.com user2@example.com user3@example.com)
3126
end
27+
28+
it "only loads the first page when counting" do
29+
Intercom.expects(:get).with("/v1/users", {:per_page => 1}).returns(page_of_users(1, 1))
30+
Intercom::User.all.count.must_equal(3)
31+
end
3232
end

spec/unit/intercom/user_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@
184184
all.must_be_instance_of(Intercom::UserCollectionProxy)
185185
end
186186

187+
it "returns the total number of users" do
188+
Intercom.expects(:get).with("/v1/users", {:per_page => 1}).returns(page_of_users)
189+
Intercom::User.count.must_be_kind_of(Integer)
190+
end
191+
187192
it "can find_by_email" do
188193
Intercom::User.expects(:find).with(:email => "bob@example.com")
189194
Intercom::User.find_by_email("bob@example.com")

0 commit comments

Comments
 (0)