Sha256: 57424501809ccc3e1e4d05cd7b412d0c70794a01313cee33699f39fc8c5de284

Contents?: true

Size: 1.35 KB

Versions: 17

Compression:

Stored size: 1.35 KB

Contents

require 'intercom/user_resource'
require 'intercom/flat_store'
require 'intercom/social_profile'

module Intercom
  # A collection of your Users from Intercom
  # Uses the paginated users api under the covers (http://docs.intercom.io/api#getting_all_users)
  #
  # == Examples:
  #
  # Iterating over each user
  #    Intercom::User.all.each do |user|
  #      puts user.inspect
  #    end
  #
  class UserCollectionProxy

    QUERYABLE_ATTRIBUTES = [:tag_id, :tag_name]

    attr_accessor :query

    def initialize(attributes={})
      self.query = attributes.reject{ |key, value| !QUERYABLE_ATTRIBUTES.include?(key) }
    end

    # yields each {User} to the block provided
    # @return [void]
    def each(&block)
      page = 1
      fetch_another_page = true
      while fetch_another_page
        current_page = Intercom.get("/v1/users", query.merge({:page => page}))
        current_page["users"].each do |user|
          block.call User.from_api(user)
        end
        page = page + 1
        fetch_another_page = !current_page["next_page"].nil?
      end
    end

    include Enumerable

    # This method exists as an optimisation of Enumerable#count,
    # which would potentially fetch multiple pages of users.
    def count(item=nil) #:nodoc:
      return super unless item.nil?
      return super if block_given?

      Intercom::User.count
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
intercom-1.0.0 lib/intercom/user_collection_proxy.rb
intercom-0.3.0 lib/intercom/user_collection_proxy.rb
intercom-0.2.3 lib/intercom/user_collection_proxy.rb
intercom-0.2.2 lib/intercom/user_collection_proxy.rb
intercom-0.2.1 lib/intercom/user_collection_proxy.rb
intercom-0.2.0 lib/intercom/user_collection_proxy.rb
intercom-0.1.19 lib/intercom/user_collection_proxy.rb
intercom-0.1.17 lib/intercom/user_collection_proxy.rb
intercom-0.1.16 lib/intercom/user_collection_proxy.rb
intercom-0.1.15 lib/intercom/user_collection_proxy.rb
intercom-0.1.14 lib/intercom/user_collection_proxy.rb
intercom-0.1.13 lib/intercom/user_collection_proxy.rb
intercom-0.1.12 lib/intercom/user_collection_proxy.rb
intercom-0.1.11 lib/intercom/user_collection_proxy.rb
intercom-0.1.10 lib/intercom/user_collection_proxy.rb
intercom-0.1.9 lib/intercom/user_collection_proxy.rb
intercom-0.1.8 lib/intercom/user_collection_proxy.rb