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

delete, get, parameterize_params, post, put, #request

Class Method Details

.contact_history(contact_ids_array) ⇒ Hash

Get list of emails send to a contact

Examples:

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

Parameters:

  • contact_ids_array (array)

    Array of contact ids

Returns:

  • (Hash)

    result data



85
86
87
# File 'lib/emarsys/data_objects/contact.rb', line 85

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

.create(key_id, key_value, params = {}) ⇒ 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 interal id field

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

    Contact information to create

Returns:

  • (Hash)

    internal id of the contact



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

def create(key_id, key_value, params = {})
  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 = []) ⇒ Object

Batch creation of contacts.

TODO params should be parameterized with field mappings

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



60
61
62
# File 'lib/emarsys/data_objects/contact.rb', line 60

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

.emarsys_id(key_id, key_value) ⇒ Hash

Get the interal 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 interal id field

Returns:

  • (Hash)

    internal emarsys id of the contact



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

def emarsys_id(key_id, key_value)
  get "contact/#{transform_key_id(key_id).to_s}=#{key_value.to_s}", {}
end

.export_registrations(params = {}) ⇒ 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) (defaults to: {})

    hash of parameters according to emarsys API

Returns:

  • (Hash)

    result data



109
110
111
# File 'lib/emarsys/data_objects/contact.rb', line 109

def export_registrations(params = {})
  post "contact/getregistrations", params
end

.search(key_id, key_values = [], fields = []) ⇒ 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) (defaults to: [])

    Array of key field values

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

    requested fields. If empty, all are considered

Returns:

  • (Hash)

    result data



99
100
101
# File 'lib/emarsys/data_objects/contact.rb', line 99

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

.transform_key_id(key_id) ⇒ Object



114
115
116
117
# File 'lib/emarsys/data_objects/contact.rb', line 114

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 = {}) ⇒ 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"})

Parameters:

  • key_id (Integer, String)

    internal id of key field

  • key_value (Integer, String)

    value of interal id field

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

    Contact information to update

Returns:

  • (Hash)

    internal id of the contact



44
45
46
47
# File 'lib/emarsys/data_objects/contact.rb', line 44

def update(key_id, key_value, params = {})
  transformed_key_id = transform_key_id(key_id)
  put "contact", params.merge!({'key_id' => transformed_key_id, transformed_key_id => key_value})
end

.update_batch(key_id, params = []) ⇒ Object

Batch update of contacts.

TODO params should be parameterized with field mappings

Examples:

Emarsys::Contact.update_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



75
76
77
# File 'lib/emarsys/data_objects/contact.rb', line 75

def update_batch(key_id, params = [])
  put "contact", {'key_id' => transform_key_id(key_id), 'contacts' => params}
end