Custom and standard attributes

Custom attributes

In the previous section, we noted that tags are treated as standard attributes of the User Model. We've created some tags and checked the User Model to look at the new tags that were added. When we created our users we also added some standard attributes such as email, name and user_id.

We can also add new attributes to the User Model, called custom attributes. As we have been doing with previous actions, we can create a method to the user_tasks.rb file to do this. The below method will update existing custom attributes or create new ones where current ones do not exist.

Creating custom attributes

To create new custom attributes or update existing ones we can use the method below:

def update_customer_attrib(criteria, attrib, value)
    #Create new custom attributes for a user or update existing ones
    #1/ Find the user first
    user = find_user(criteria)
    # Set/Update the relevant Attribute
    user.custom_attributes[attrib] = value
    # Save the resultant change
    intercom.users.save(user)
  end
require './intercom_client'
require 'csv'

class UserTasks < IntercomClient
  def initialize()
  end

  def create_user(args)
    #Create a new user with list of values passed on setup
    user = @@intercom.users.create(args)
  end

  def find_user(criteria)
    begin
      #Check for users via the unique ID
      user = @@intercom.users.find(:id => criteria)
    rescue Intercom::ResourceNotFound
      begin
        #Check for users via user id if we receive not found error
        user = @@intercom.users.find(:user_id=> criteria)
      rescue Intercom::ResourceNotFound
        #Check for users via email address
        user = @@intercom.users.find(:email => criteria)
      end
    end
  end
  
  def submit_bulk_job(user_params)
    #Simple wrapper to create bulk job
    @@intercom.users.submit_bulk_job(create_items: user_params )
	end
  
  def bulk_create(csv_file)
    #Check to make sure the CSV file eixsts
    if File.exist?(csv_file)
      file = File.open(csv_file, "rb")
      body = file.read

      #Need to add to the CSV model to handle empty fields
      CSV::Converters[:blank_to_nil] = lambda do |field|
        field && field.empty? ? nil : field
      end
      csv = CSV.new(body, :headers => true, :header_converters => :symbol, :converters => [:all, :blank_to_nil])
      csv_data = csv.to_a.map {|row| row.to_hash }

      @@intercom.users.submit_bulk_job(create_items: csv_data )
    else
      puts("No CSV file found")
    end
  end

  def show_attrib(criteria, attrib )
      #First we check whether this is a standard attribute
      user = find_user(criteria)
      user.send(attrib.to_sym)
  end

  def update_customer_attrib(criteria, attrib, value)
    #Create new custom attributes for a user or update existing ones
    #1/ Find the user first
    user = find_user(criteria)
    # Set/Update the relevant Attribute
    user.custom_attributes[attrib] = value
    # Save the resultant change
    intercom.users.save(user)
  end
  
end

To check whether this is now set, we can add to the existing method we have to look for the standard attributes. The show_attrib method should now look like this:

def show_attrib(criteria, attrib )
    begin
      #First we check whether this is a standard attribute
      user = find_user(criteria)
      user.send(attrib.to_sym)
    rescue Intercom::AttributeNotSetError
      begin
        #If we cannot find it is a standard attribute lets check if it is a customer attribute
        user.custom_attributes[attrib]
      end
    end
  end

Let's set a custom attribute and then check it:

> user.update_customer_attrib("[email protected]", "occupation", "Philosopher")
=> #<Intercom::User:0x000000029f2cf0
> tags = user.show_attrib("[email protected]", "occupation")
=> "Philosopher"
[13] pry(main)> tags = user.show_attrib("[email protected]", "job")
=> nil

Updating standard attributes

We can extend this a little further if you want to update your standard attributes as well. Let's first check whether the attribute is a standard one - if it isn't, we'll get an exception saying "AttributeNotSetError". If you get this error, let's assume you want to update custom attributes and go ahead and do that. Otherwise, let's update the standard attribute.

def update_customer_attrib(criteria, attrib, value)
    #Create new custom attributes for a user or update existing ones
    #1/ Find the user first
    user = find_user(criteria)
    # Set/Update the relevant Attribute
    begin
      user.send(attrib.to_sym)
      #If no exception thrown then set the standard attribute
      user.send("#{attrib}=", value)
    rescue Intercom::AttributeNotSetError
      #If we jump in here then we will assume it is a custom attribute
      user.custom_attributes[attrib] = value
    end
    # Save the resultant change
    @@intercom.users.save(user)
  end
require './intercom_client'
require "csv"

class UserTasks < IntercomClient
  def initialize()
  end

  def create_user(args)
    #Create a new user with list of values passed on setup
    user = @@intercom.users.create(args)
  end

  def find_user(criteria)
    begin
      #Check for users via the unique ID
      user = @@intercom.users.find(:id => criteria)
    rescue Intercom::ResourceNotFound
      begin
        #Check for users via user id if we receive not found error
        user = @@intercom.users.find(:user_id=> criteria)
      rescue Intercom::ResourceNotFound
        #Check for users via email address
        user = @@intercom.users.find(:email => criteria)
      end
    end
  end

  def submit_bulk_job(user_params)
    #Simple wrapper to create bulk job
    @@intercom.users.submit_bulk_job(create_items: user_params )
	end
  
  def bulk_create(csv_file)
    #Check to make sure the CSV file eixsts
    if File.exist?(csv_file)
      file = File.open(csv_file, "rb")
      body = file.read

      #Need to add to the CSV model to handle empty fields
      CSV::Converters[:blank_to_nil] = lambda do |field|
        field && field.empty? ? nil : field
      end
      csv = CSV.new(body, :headers => true, :header_converters => :symbol, :converters => [:all, :blank_to_nil])
      csv_data = csv.to_a.map {|row| row.to_hash }

      @@intercom.users.submit_bulk_job(create_items: csv_data )
    else
      puts("No CSV file found")
    end
  end

  def show_attrib(criteria, attrib )
    begin
      #First we check whether this is a standard attribute
      user = find_user(criteria)
      user.send(attrib.to_sym)
    rescue Intercom::AttributeNotSetError
      begin
        #If we cannot find it is a standard attribute lets check if it is a customer attribute
        user.custom_attributes[attrib]
      end
    end
  end

  def update_customer_attrib(criteria, attrib, value)
    #Create new custom attributes for a user or update existing ones
    #1/ Find the user first
    user = find_user(criteria)
    # Set/Update the relevant Attribute
    begin
      user.send(attrib.to_sym)
      #If no exception thrown then set the standard attribute
      user.send("#{attrib}=", value)
    rescue Intercom::AttributeNotSetError
      #If we jump in here then we will assume it is a custom attribute
      user.custom_attributes[attrib] = value
    end
    # Save the resultant change
    @@intercom.users.save(user)
  end
end
>temp_user = usr.find_user("[email protected]")
>temp_user.name
=> "Jean"
usr.update_customer_attrib("[email protected]", "name", "Jean Paul")
temp_user = usr.find_user("[email protected]")
temp_user.name
=> "Jean Paul"

So far, we've talked about users. Now, we're going to move on to looking at leads in the next tutorial.