class Followanalytics::Attributes::Client

Constants

MISSING_API_KEY
MISSING_SOR
PREDEFINED_ATTRIBUTE_KEYS

Attributes

sor_identifier[RW]

Public Class Methods

new(sor_identifier) click to toggle source

Initializes the Attributes client.

@param sor_identifier [String] The System of Record we wish to set

attributes to.

@raise [Followanalytics::Error] When the System of Record is not

defined.
# File lib/followanalytics/attributes.rb, line 23
def initialize(sor_identifier)
  raise Followanalytics::Error, MISSING_SOR if sor_identifier.nil?
  @sor_identifier = sor_identifier
end

Public Instance Methods

add_set_value(value, key, customer_id) click to toggle source

Add a value to an attribute of type set.

@param value The value to add to the set. @param key The key of the set attribute. @param customer_id The customer we want to unset the attribute to.

@example Add the value “strawberry” to the set attribute with the key “fruit_salad” for the customer “tim”

client.add_set_value("strawberry", "fruit_salad", "tim")
# File lib/followanalytics/attributes.rb, line 68
def add_set_value(value, key, customer_id)
  hash = attribute_hash(value, key, customer_id).tap do |hsh|
    hsh['action_type'] = 'ADD'
  end
  send_attributes(hash)
end
remove_set_value(value, key, customer_id) click to toggle source

Remove a value to an attribute of type set.

@param value The value to add to the set. @param key The key of the set attribute. @param customer_id The customer we want to unset the attribute to.

@example Remove the value “strawberry” to the set attribute with the key “fruit_salad” for the customer “tim”

client.remove_set_value("strawberry", "fruit_salad", "tim")
# File lib/followanalytics/attributes.rb, line 83
def remove_set_value(value, key, customer_id)
  hash = attribute_hash(value, key, customer_id).tap do |hsh|
    hsh['action_type'] = 'REMOVE'
  end
  send_attributes(hash)
end
set_value(value, key, customer_id) click to toggle source

Set one value for a customer.

@param value The value to set. @param key The key of the attribute. @param customer_id The customer we want to set the attribute to.

@example Set the value “apple” to the attribute with the key “favorite_fruit” for the customer “tim”

client.set_value("apple", "favorite_fruit", "tim")
# File lib/followanalytics/attributes.rb, line 43
def set_value(value, key, customer_id)
  hash = attribute_hash(value, key, customer_id)
  send_attributes(hash)
end
unset_value(key, customer_id) click to toggle source

Unset one value for a customer.

@param key The key of the attribute. @param customer_id The customer we want to unset the attribute to.

@example Unset the value “apple” to the attribute with the key “favorite_fruit” for the customer “tim”

client.unset_value("favorite_fruit", "tim")
# File lib/followanalytics/attributes.rb, line 55
def unset_value(key, customer_id)
  hash = attribute_hash(nil, key, customer_id)
  send_attributes(hash)
end

Private Instance Methods

attribute_hash(value, key, customer_id) click to toggle source
# File lib/followanalytics/attributes.rb, line 96
def attribute_hash(value, key, customer_id)
  {
    "attribute_key"    => key,
    "attribute_value"  => value,
    "customer_id"      => customer_id,
    "customer_id_type" => "user_id"
  }
end
attributes_url() click to toggle source
# File lib/followanalytics/attributes.rb, line 92
def attributes_url
  "#{Followanalytics.configuration.attribute_base_url}/api/attribute_values"
end
send_attributes(hash) click to toggle source
# File lib/followanalytics/attributes.rb, line 105
def send_attributes(hash)
  api_key = Followanalytics.configuration.api_key
  raise Followanalytics::Error, MISSING_API_KEY if api_key.nil?

  params = Oj.dump({
    "sor" => @sor_identifier,
    "api_key" => api_key,
    "customer_attribute_values" => [hash]
  })

  response = RestClient.post(attributes_url, params, content_type: :json)
rescue RestClient::Exception => exception
  raise Followanalytics::Error.from_rest_client(exception)
end