Class: Emarsys::Contact

Inherits:
DataObject show all
Defined in:
lib/emarsys/data_objects/contact.rb

Overview

Methods for the Contact API

Class Method Summary collapse

Methods inherited from DataObject

get, parameterize_params, post, put, #request

Class Method Details

.batch_params(params = []) ⇒ Object



156
157
158
# File 'lib/emarsys/data_objects/contact.rb', line 156

def batch_params(params = [])
  params.map { |p| Emarsys::ParamsConverter.new(p).convert_to_ids }
end

.contact_history(contacts:, account: nil) ⇒ Hash

Get list of emails send to a contact

Examples:

Emarsys::Contact.contact_history([1,2,3]

Parameters:

  • contacts (array)

    Array of contact ids

Returns:

  • (Hash)

    result data



104
105
106
# File 'lib/emarsys/data_objects/contact.rb', line 104

def contact_history(contacts:, account: nil)
  post , "contact/getcontacthistory", {'contacts' => contacts}
end

.create(key_id:, key_value:, params: {}, account: nil) ⇒ Hash

Create a new contact. The given params are transformed to emarsys ids.

Examples:

Emarsys::Contact.create('app_id', 23, {:firstname => "Jon", :lastname => "Doe"})
Emarsys::Contact.create('3', '[email protected]', {'1' => "Jon", '2' => "Doe"})

Parameters:

  • key_id (Integer, String)

    internal id of key field

  • key_value (Integer, String)

    value of internal id field

  • params (Hash) (defaults to: {})

    Contact information to create

Returns:

  • (Hash)

    internal id of the contact



19
20
21
22
# File 'lib/emarsys/data_objects/contact.rb', line 19

def create(key_id:, key_value:, params: {}, account: nil)
  transformed_key_id = transform_key_id(key_id)
  post , "contact", params.merge!({'key_id' => transformed_key_id, transformed_key_id => key_value})
end

.create_batch(key_id:, params: [], account: nil) ⇒ Object

Batch creation of contacts.

Examples:

Emarsys::Contact.create_batch(
  'email', {:app_id => 1, :firstname => "Jon", :lastname => "Doe"},
           {:app_id => 2, :firstname => "Jane", :lastname => "Doe"}
)

Parameters:

  • key_id (Integer, String)

    internal id of key field

  • params (Hash) (defaults to: [])

    Contact information of each new contact



62
63
64
# File 'lib/emarsys/data_objects/contact.rb', line 62

def create_batch(key_id:, params: [], account: nil)
  post , "contact", {'key_id' => transform_key_id(key_id), 'contacts' => batch_params(params)}
end

.delete(key_id:, key_value:, account: nil) ⇒ Hash

Delete a contact. The given params are transformed to emarsys ids.

Examples:

Emarsys::Contact.update('app_id', 23, {:firstname => "Jon", :lastname => "Doe"})
Emarsys::Contact.update('3', '[email protected]', {'1' => "Jon", '2' => "Doe"}, true)

Parameters:

  • key_id (Integer, String)

    internal id of key field

  • key_value (Integer, String)

    value of internal id field

Returns:



92
93
94
95
96
# File 'lib/emarsys/data_objects/contact.rb', line 92

def delete(key_id:, key_value:, account: nil)
  path = "contact/delete"
  transformed_key_id = transform_key_id(key_id)
  post , path, {'key_id' => transformed_key_id, transformed_key_id => key_value}
end

.emarsys_id(key_id:, key_value:, account: nil) ⇒ Hash

Get the internal emarsys id of a contact. The given params are transformed to emarsys ids.

Examples:

Emarsys::Contact.emarsys_id('email', '[email protected]')
Emarsys::Contact.emarsys_id(1, 'John')

Parameters:

  • key_id (Integer, String)

    internal id of key field

  • key_value (Integer, String)

    value of internal id field

Returns:

  • (Hash)

    internal emarsys id of the contact



32
33
34
# File 'lib/emarsys/data_objects/contact.rb', line 32

def emarsys_id(key_id:, key_value:, account: nil)
  get , "contact", {"#{transform_key_id(key_id).to_s}" => key_value}
end

.export_registrations(distribution_method:, time_range:, contact_fields:, account: nil) ⇒ Hash

Exports the selected fields of contacts whoch registered in the specified time range

Examples:

Emarsys::Contact.export_registrations(distribution_method: 'local', time_range: ["2013-01-01","2013-12-31"], contact_fields: [1,2,3])

Parameters:

  • params (hash)

    hash of parameters according to emarsys API

Returns:

  • (Hash)

    result data



141
142
143
144
145
146
147
# File 'lib/emarsys/data_objects/contact.rb', line 141

def export_registrations(distribution_method:, time_range:, contact_fields:, account: nil)
  post , "contact/getregistrations", {
    distribution_method: distribution_method,
    time_range: time_range,
    contact_fields: contact_fields
  }
end

.query(key_id:, key_value:, return_value:, account: nil) ⇒ Hash

Query contacts by custom

Examples:

Emarsys::Contact.query('3', '[email protected]', 'uid')

Parameters:

  • key_id (Integer, String)

    The key used to query

  • key_value (String)

    Value of internal id field

  • return_value (Integer, String)

    Value of internal id field

Returns:

  • (Hash)

    result data



131
132
133
# File 'lib/emarsys/data_objects/contact.rb', line 131

def query(key_id:, key_value:, return_value: , account: nil)
  get , "contact/query", { key_id => key_value, 'return' => return_value}
end

.search(key_id:, key_values:, fields: [], account: nil) ⇒ Hash

Get contact data by custom search

TODO transform fields to numeric fields

Examples:

Emarsys::Contact.search('3', ['[email protected]'], [1,2,3])

Parameters:

  • key_id (Integer, String)

    Array of contact ids

  • key_values (array)

    Array of key field values

  • fields (array) (defaults to: [])

    requested fields. If empty, all are considered

Returns:

  • (Hash)

    result data



118
119
120
# File 'lib/emarsys/data_objects/contact.rb', line 118

def search(key_id:, key_values:, fields: [], account: nil)
  post , "contact/getdata", {'keyId' => key_id, 'keyValues' => key_values, 'fields' => fields}
end

.transform_key_id(key_id) ⇒ Object



150
151
152
153
# File 'lib/emarsys/data_objects/contact.rb', line 150

def transform_key_id(key_id)
  matching_attributes = Emarsys::FieldMapping.attributes.find{|elem| elem[:identifier] == key_id.to_s}
  matching_attributes.nil? ? key_id : matching_attributes[:id]
end

.update(key_id:, key_value:, params: {}, create_if_not_exists: false, account: nil) ⇒ Hash

Update a contact. The given params are transformed to emarsys ids.

Examples:

Emarsys::Contact.update('app_id', 23, {:firstname => "Jon", :lastname => "Doe"})
Emarsys::Contact.update('3', '[email protected]', {'1' => "Jon", '2' => "Doe"}, true)

Parameters:

  • key_id (Integer, String)

    internal id of key field

  • key_value (Integer, String)

    value of internal id field

  • params (Hash) (defaults to: {})

    Contact information to update

  • create_if_not_exists (Boolean) (defaults to: false)

    Whether to create contact if it does not exist

Returns:

  • (Hash)

    internal id of the contact



46
47
48
49
50
# File 'lib/emarsys/data_objects/contact.rb', line 46

def update(key_id:, key_value:, params: {}, create_if_not_exists: false, account: nil)
  path = "contact#{create_if_not_exists ? '/?create_if_not_exists=1' : ''}"
  transformed_key_id = transform_key_id(key_id)
  put , path, params.merge!({'key_id' => transformed_key_id, transformed_key_id => key_value})
end

.update_batch(key_id:, params: [], create_if_not_exists: false, account: nil) ⇒ Object

Batch update of contacts.

Examples:

Emarsys::Contact.update_batch(
  'email',
  [{:email => "[email protected]", :firstname => "Jon", :lastname => "Doe"},
   {:email => "[email protected]", :firstname => "Jane", :lastname => "Doe"}],
  true
)

Parameters:

  • key_id (Integer, String)

    internal id of key field

  • params (Hash) (defaults to: [])

    Contact information of each new contact

  • create_if_not_exists (Boolean) (defaults to: false)

    Whether to create non-existing contacts



79
80
81
82
# File 'lib/emarsys/data_objects/contact.rb', line 79

def update_batch(key_id:, params: [], create_if_not_exists: false, account: nil)
  path = "contact#{create_if_not_exists ? '/?create_if_not_exists=1' : ''}"
  put , path, {'key_id' => transform_key_id(key_id), 'contacts' => batch_params(params)}
end