diff --git a/lib/intercom/user.rb b/lib/intercom/user.rb index 85bbbbe4..a715dec3 100644 --- a/lib/intercom/user.rb +++ b/lib/intercom/user.rb @@ -8,13 +8,13 @@ module Intercom # # == Example usage # * Fetching a user - # Intercom::User.find_by_email("bob@example.") + # Intercom::User.find_by_email("bob@example.com") # # * Getting the count of all users # Intercom::User.all.count # # * Fetching all users - # Intercom::User.all.each {|user| puts user.email } + # Intercom::User.all.each { |user| puts user.email } # # * Updating custom data on a user # user = Intercom::User.find_by_email("bob@example.com") @@ -70,11 +70,11 @@ def self.create(params) # Examples: # Intercom::User.all.count # > 5346 - # Intercom::User.each do |user| + # Intercom::User.all.each do |user| # puts user.inspect # end # > ["user1@example.com" ,"user2@example.com" ,....] - # Intercom::User.map(&:email) + # Intercom::User.all.map(&:email) # > ["user1@example.com" ,"user2@example.com" ,....] # # @return [UserCollectionProxy] diff --git a/lib/intercom/user_collection_proxy.rb b/lib/intercom/user_collection_proxy.rb index 91274b93..02ea092a 100644 --- a/lib/intercom/user_collection_proxy.rb +++ b/lib/intercom/user_collection_proxy.rb @@ -12,13 +12,16 @@ module Intercom # Intercom::User.all.count # # Iterating over each user - # Intercom::User.each do |user| + # Intercom::User.all.each do |user| # puts user.inspect # end # class UserCollectionProxy + include Enumerable + # @return [Integer] number of users tracked on Intercom for this application def count + raise ArgumentError.new("count doesn't support block argument") if block_given? response = Intercom.get("users", {:per_page => 1}) response["total_count"] end @@ -37,15 +40,5 @@ def each(&block) fetch_another_page = !current_page["next_page"].nil? end end - - # yields each {User} to the block provided and collects the output in the same way as Enumerable#map - # @return [Array] - def map - out = [] - each { |e| out << yield(e) } - out - end - - alias :collect :map end end diff --git a/spec/unit/intercom/user_collection_proxy_spec.rb b/spec/unit/intercom/user_collection_proxy_spec.rb index 61c1c1b0..b3020cd4 100644 --- a/spec/unit/intercom/user_collection_proxy_spec.rb +++ b/spec/unit/intercom/user_collection_proxy_spec.rb @@ -18,11 +18,19 @@ Intercom::User.all.collect { |user| user.email }.must_equal %W(user1@example.com user2@example.com user3@example.com) end - it "yields each user to the block" do - Intercom.expects(:get).with("users", {:per_page => 1}).returns(page_of_users(1,1)) + it "counts users based on total_count in paged_response" do + paged_response = page_of_users(1, 1) + Intercom.expects(:get).with("users", {:per_page => 1}).returns(paged_response) + paged_response.expects(:[]).with("total_count").returns(3) Intercom::User.all.count.must_equal 3 end + it "explicitly doesn't support block argument for count" do + assert_raises ArgumentError do + Intercom::User.all.count {|_| true } + end + end + it "loads multiple pages" do Intercom.expects(:get).with("users", {:page => 1}).returns(page_of_users(1, 1)) Intercom.expects(:get).with("users", {:page => 2}).returns(page_of_users(2, 1))