Class: Intercom::User

Inherits:
UserResource show all
Defined in:
lib/intercom/user.rb

Overview

Represents a user of your application on Intercom.

Example usage

  • Fetching a user

    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 }
  • Updating custom data on a user

    user = Intercom::User.find_by_email("bob@example.com")
    user.custom_data["number_of_applications"] = 11
    user.save

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from UserResource

#email, #email=, #initialize, #to_hash, #update_from_api_response, #user_id, #user_id=

Methods included from RequiresParameters

#requires_parameters

Methods included from UnixTimestampUnwrapper

#set_time_at, #time_at

Constructor Details

This class inherits a constructor from Intercom::UserResource

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Intercom::UserResource

Class Method Details

+ (UserCollectionProxy) all

Retrieve all the users Examples:

Intercom::User.all.each do |user|
  puts user.inspect
end
  > ["user1@example.com" ,"user2@example.com" ,....]
Intercom::User.all.map(&:email)
  > ["user1@example.com" ,"user2@example.com" ,....]

Returns:



81
82
83
# File 'lib/intercom/user.rb', line 81

def self.all
  UserCollectionProxy.new
end

+ (Integer) count

Fetches a count of all Users tracked on Intercom. Example:

Intercom::User.all.count
  > 5346

Returns:

  • (Integer)


107
108
109
110
# File 'lib/intercom/user.rb', line 107

def self.count
  response = Intercom.get("/v1/users", {:per_page => 1})
  response["total_count"]
end

+ (User) create(params)

Creates (or updates when a user already exists for that email/user_id) a user record on your application.

Calls POST api.intercom.io/v1/users

returns Intercom::User object representing the state on our servers.

This operation is idempotent.

Returns:



67
68
69
# File 'lib/intercom/user.rb', line 67

def self.create(params)
  User.new(params).save
end

+ (User) delete(params)

Deletes a user record on your application.

Calls DELETE api.intercom.io/v1/users

returns Intercom::User object representing the user just before deletion.

This operation is not idempotent.

Returns:



120
121
122
123
# File 'lib/intercom/user.rb', line 120

def self.delete(params)
  response = Intercom.delete("/v1/users", params)
  User.from_api(response)
end

+ (User) find(params)

Fetches an Intercom::User from our API.

Calls GET api.intercom.io/v1/users

returns Intercom::User object representing the state on our servers.

Returns:



34
35
36
37
# File 'lib/intercom/user.rb', line 34

def self.find(params)
  response = Intercom.get("/v1/users", params)
  User.from_api(response)
end

+ (User) find_by_email(email)

Calls GET api.intercom.io/v1/users?email=EMAIL

returns Intercom::User object representing the state on our servers.

Parameters:

  • email (String)

    address of the user

Returns:



45
46
47
# File 'lib/intercom/user.rb', line 45

def self.find_by_email(email)
  find({:email => email})
end

+ (User) find_by_user_id(user_id)

Calls GET api.intercom.io/v1/users?user_id=USER-ID

returns Intercom::User object representing the state on our servers.

Parameters:

  • user_id (String)

    user id of the user

Returns:



55
56
57
# File 'lib/intercom/user.rb', line 55

def self.find_by_user_id(user_id)
  find({:user_id => user_id})
end

+ (UserCollectionProxy) where(params)

Retrieve all the users that match a query Examples:

Intercom::User.where(:tag_name => 'Free Trial').each do |user|
  puts user.inspect
end
  > ["user1@example.com" ,"user2@example.com" ,....]
Intercom::User.where(:tag_name => 'Free Trial').map(&:email)
  > ["user1@example.com" ,"user2@example.com" ,....]

Currently only supports tag_name and tag_id querying

Returns:



97
98
99
# File 'lib/intercom/user.rb', line 97

def self.where(params)
  UserCollectionProxy.new(params)
end

Instance Method Details

- (Array) companies

Multiple companies for this Intercom::User

See docs.intercom.io/#Companies for more information

Example: Setting a company for an existing user

user = Intercom::User.find(:email => "someone@example.com")
user.companies = [{:id => 6, :name => "intercom"}, {:id => 9, :name => "Test Company"}]
user.save

Returns:

  • (Array)


312
313
314
# File 'lib/intercom/user.rb', line 312

def companies
  @attributes["companies"] ||= []
end

- (Array) companies=(companies)

Set an Array of Hash company attributes to save/update on this user

Parameters:

  • companies (Array)

Returns:

  • (Array)

Raises:

  • (ArgumentError)


320
321
322
323
# File 'lib/intercom/user.rb', line 320

def companies=(companies)
  raise ArgumentError.new("Companies requires an array of hashes of companies") unless companies.is_a?(Array) && companies.all? {|company| company.is_a?(Hash)}
  @attributes["companies"] = companies.collect {|company| FlatStore.new(company) }
end

- (FlatStore) company

Company stored for this Intercom::User

See docs.intercom.io/#Companies for more information

Example: Setting a company for an existing user

user = Intercom::User.find(:email => "someone@example.com")
user.company[:id] = 6
user.company[:name] = "Intercom"
user.save

Returns:



290
291
292
# File 'lib/intercom/user.rb', line 290

def company
  @attributes["company"] ||= FlatStore.new
end

- (FlatStore) company=(company)

Set a Hash of company attributes to save/update on this user

Parameters:

  • company (Hash)

Returns:



298
299
300
# File 'lib/intercom/user.rb', line 298

def company=(company)
  @attributes["company"] = FlatStore.new(company)
end

- (Time) created_at

Get Time at which this User started using your application.

Returns:

  • (Time)


204
205
206
# File 'lib/intercom/user.rb', line 204

def created_at
  time_at("created_at")
end

- (void) created_at=(time)

This method returns an undefined value.

Set Time at which this User started using your application.



211
212
213
# File 'lib/intercom/user.rb', line 211

def created_at=(time)
  set_time_at("created_at", time)
end

- (FlatStore) custom_data

Custom attributes stored for this Intercom::User

See docs.intercom.io/#CustomData for more information

Example: Reading custom_data value for an existing user

user = Intercom::User.find(:email => "someone@example.com")
puts user.custom_data[:plan]

Example: Setting some custom data for an existing user

user = Intercom::User.find(:email => "someone@example.com")
user.custom_data[:plan] = "pro"
user.save

Returns:



267
268
269
# File 'lib/intercom/user.rb', line 267

def custom_data
  @attributes["custom_data"] ||= FlatStore.new
end

- (FlatStore) custom_data=(custom_data)

Set a Hash of custom data attributes to save/update on this user

Parameters:

  • custom_data (Hash)

Returns:



275
276
277
# File 'lib/intercom/user.rb', line 275

def custom_data=(custom_data)
  @attributes["custom_data"] = FlatStore.new(custom_data)
end

- (Time) last_impression_at

Get last time this User interacted with your application

Returns:

  • (Time)


176
177
178
# File 'lib/intercom/user.rb', line 176

def last_impression_at
  time_at("last_impression_at")
end

- (void) last_impression_at=(time)

This method returns an undefined value.

Set Time at which this User last made a request your application.



183
184
185
# File 'lib/intercom/user.rb', line 183

def last_impression_at=(time)
  set_time_at("last_impression_at", time)
end

- (Time) last_request_at

Get last time this User interacted with your application

Returns:

  • (Time)


190
191
192
# File 'lib/intercom/user.rb', line 190

def last_request_at
  time_at("last_request_at")
end

- (void) last_request_at=(time)

This method returns an undefined value.

Set Time at which this User last made a request your application.



197
198
199
# File 'lib/intercom/user.rb', line 197

def last_request_at=(time)
  set_time_at("last_request_at", time)
end

- (String) last_seen_ip

Returns:

  • (String)


144
145
146
# File 'lib/intercom/user.rb', line 144

def last_seen_ip
  @attributes["last_seen_ip"]
end

- (void) last_seen_ip=(last_seen_ip)

This method returns an undefined value.



149
150
151
# File 'lib/intercom/user.rb', line 149

def last_seen_ip=(last_seen_ip)
  @attributes["last_seen_ip"]=last_seen_ip
end

- (String) last_seen_user_agent

Returns:

  • (String)


154
155
156
# File 'lib/intercom/user.rb', line 154

def last_seen_user_agent
  @attributes["last_seen_user_agent"]
end

- (void) last_seen_user_agent=(last_seen_user_agent)

This method returns an undefined value.



159
160
161
# File 'lib/intercom/user.rb', line 159

def last_seen_user_agent=(last_seen_user_agent)
  @attributes["last_seen_user_agent"]=last_seen_user_agent
end

- (Hash) location_data

Get hash of location attributes associated with this Intercom::User

Possible entries: city_name, continent_code, country_code, country_name, latitude, longitude, postal_code, region_name, timezone

e.g.

{"city_name"=>"Santiago", "continent_code"=>"SA", "country_code"=>"CHL", "country_name"=>"Chile",
 "latitude"=>-33.44999999999999, "longitude"=>-70.6667, "postal_code"=>"", "region_name"=>"12",
 "timezone"=>"Chile/Continental"}

Returns:

  • (Hash)


249
250
251
# File 'lib/intercom/user.rb', line 249

def location_data
  @location_data ||= {}.freeze
end

- (String) name

The Intercom::User's name

Returns:



133
134
135
# File 'lib/intercom/user.rb', line 133

def name
  @attributes["name"]
end

- (void) name=(name)

This method returns an undefined value.

Parameters:



139
140
141
# File 'lib/intercom/user.rb', line 139

def name=(name)
  @attributes["name"]=name
end

- (Integer) relationship_score

Returns:

  • (Integer)


164
165
166
# File 'lib/intercom/user.rb', line 164

def relationship_score
  @attributes["relationship_score"]
end

- (User) save

instance method alternative to #create

Returns:



127
128
129
130
# File 'lib/intercom/user.rb', line 127

def save
  response = Intercom.post("/v1/users", to_hash)
  self.update_from_api_response(response)
end

- (Integer) session_count

Returns:

  • (Integer)


169
170
171
# File 'lib/intercom/user.rb', line 169

def session_count
  @attributes["session_count"]
end

- (Array<SocialProfile>) social_profiles

Get array of Intercom::SocialProfile objects attached to this Intercom::User

See docs.intercom.io/#SocialProfiles for more information

Returns:



234
235
236
# File 'lib/intercom/user.rb', line 234

def social_profiles
  @social_profiles ||= [].freeze
end

- (Boolean) unsubscribed_from_emails

Get whether user has unsubscribed from email

Returns:

  • (Boolean)


218
219
220
# File 'lib/intercom/user.rb', line 218

def unsubscribed_from_emails
  @attributes['unsubscribed_from_emails']
end

- (void) unsubscribed_from_emails=(unsubscribed_from_emails)

This method returns an undefined value.

Set whether user has unsubscribed from email



225
226
227
# File 'lib/intercom/user.rb', line 225

def unsubscribed_from_emails=(unsubscribed_from_emails)
  @attributes['unsubscribed_from_emails'] = unsubscribed_from_emails
end