Class: Promoter::Contact

Inherits:
Object
  • Object
show all
Defined in:
lib/promoter/contact.rb

Constant Summary collapse

API_URL =
"https://app.promoter.io/api/contacts"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Contact

Returns a new instance of Contact.



9
10
11
12
13
14
15
16
# File 'lib/promoter/contact.rb', line 9

def initialize(attrs)
  @id = attrs["id"]
  @email = attrs["email"]
  @first_name = attrs["first_name"]
  @last_name = attrs["last_name"]
  @created_date = Time.parse(attrs["created_date"]) if attrs["created_date"]
  @attributes = attrs["attributes"]
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/promoter/contact.rb', line 5

def attributes
  @attributes
end

#created_dateObject (readonly)

Returns the value of attribute created_date.



5
6
7
# File 'lib/promoter/contact.rb', line 5

def created_date
  @created_date
end

#emailObject (readonly)

Returns the value of attribute email.



5
6
7
# File 'lib/promoter/contact.rb', line 5

def email
  @email
end

#first_nameObject (readonly)

Returns the value of attribute first_name.



5
6
7
# File 'lib/promoter/contact.rb', line 5

def first_name
  @first_name
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/promoter/contact.rb', line 5

def id
  @id
end

#last_nameObject (readonly)

Returns the value of attribute last_name.



5
6
7
# File 'lib/promoter/contact.rb', line 5

def last_name
  @last_name
end

Class Method Details

.all(options = {}) ⇒ Object

Parameter Optional? Description page yes Returns which page of results to return. Defaults to 1

email yes Filter the results by email address. contact_list_id yes Filter the results by contact list



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/promoter/contact.rb', line 27

def self.all(options={})
  if !options.is_a?(Hash)
    puts "-- DEPRECATION WARNING--"
    puts "Passing in a number as a page is deprecated and will be removed from future versions of this gem.\nInstead pass in a hash of attributes.\n\n e.g. Promoter::Contact.all(page: 2)"
    query_string = "page=#{options}"
  else
    # default to first page
    options[:page] ||= 1

    if options.key?(:contact_list_id)
      options[:contact_list__id] = options.delete(:contact_list_id)
    end

    query_string = URI.encode_www_form(options)
  end
  response = Request.get("#{API_URL}/?#{query_string}")
  response['results'].map { |attrs| new(attrs) }
end

.create(params) ⇒ Object

Contact Params Parameter Optional? Description email no The email of the contact to add to the organization. first_name yes The first name of the contact to add to the organization. last_name yes The last name of the contact to add to the organization. contact_list yes A list of Contact List Id’s to associate a contact to.

If one is not provided the contact will be
associated to a default generated contact list.

attributes yes A dictionary of key value pairs of custom

attributes that will be associated with the
contact and contact list.

send yes A boolean value set to true in order to express

intent to survey this contact for a given campaign.

campaign yes The campaign id you would like to associate the

contact to. Note: Campaign must have a contact
list associated to it in order for the contact to
be added correctly. Otherwise, the contact will
be associated to a default generated contact list
for your given organization.


78
79
80
81
82
83
84
85
86
# File 'lib/promoter/contact.rb', line 78

def self.create(params)
  # ensure the values of the 'attributes' param are strings
  if params[:attributes]
    params[:attributes] = values_to_string(params[:attributes])
  end

  response = Request.post(API_URL + "/", params)
  new(response)
end

.destroy(email) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/promoter/contact.rb', line 51

def self.destroy(email)
  attributes = {
    email: email
  }
  response = Request.post("#{API_URL}/remove/", attributes)
  new(response)
end

.find(id) ⇒ Object



46
47
48
49
# File 'lib/promoter/contact.rb', line 46

def self.find(id)
  response = Request.get("#{API_URL}/#{id}")
  new(response)
end

.survey(params) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/promoter/contact.rb', line 88

def self.survey(params)
  contact_attributes = if Promoter.api_version == 2
    api_url = "https://app.promoter.io/api/v2"
    contact_params = {
      attributes: params[:attributes] || {},
      email: params[:email],
      first_name: params[:first_name],
      last_name: params[:last_name]
    }
    survey_params = {
      attributes: params[:survey_attributes] || {},
      campaign_id: params[:campaign_id],
      contact: contact_params,
    }
    response = Request.post(api_url + "/survey/", survey_params)

    response["contact"]
  else
    Request.post(API_URL + "/survey/", params)
  end
  new(contact_attributes)
end

.values_to_string(hash) ⇒ Object

used for ensuring the values of the attributes hashes are strings



112
113
114
# File 'lib/promoter/contact.rb', line 112

def self.values_to_string(hash)
  hash.each{ |key, value| hash[key] = value.to_s }
end

Instance Method Details

#destroyObject



18
19
20
# File 'lib/promoter/contact.rb', line 18

def destroy
  Contact.destroy(self.email)
end