Class: Bronto::Contact

Inherits:
Base show all
Defined in:
lib/bronto/contact.rb

Defined Under Namespace

Classes: Field

Instance Attribute Summary collapse

Attributes inherited from Base

#api_key, #errors, #id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

api, api_key, api_key=, #create, create, #destroy, destroy, plural_class_name, request, #request, session_expired, update, #update

Constructor Details

#initialize(options = {}) ⇒ Contact

Returns a new instance of Contact.



40
41
42
43
44
45
46
# File 'lib/bronto/contact.rb', line 40

def initialize(options = {})
  self.fields = {}
  fields = options.delete(:fields)
  Array.wrap(fields).each { |field| set_field(field[:field_id], field[:content]) }

  super(options)
end

Instance Attribute Details

#createdObject

Returns the value of attribute created.



3
4
5
# File 'lib/bronto/contact.rb', line 3

def created
  @created
end

#emailObject

Returns the value of attribute email.



3
4
5
# File 'lib/bronto/contact.rb', line 3

def email
  @email
end

#fieldsObject

Returns the value of attribute fields.



3
4
5
# File 'lib/bronto/contact.rb', line 3

def fields
  @fields
end

#list_idsObject

Returns the value of attribute list_ids.



3
4
5
# File 'lib/bronto/contact.rb', line 3

def list_ids
  @list_ids
end

#modifiedObject

Returns the value of attribute modified.



3
4
5
# File 'lib/bronto/contact.rb', line 3

def modified
  @modified
end

#num_clicksObject

Returns the value of attribute num_clicks.



3
4
5
# File 'lib/bronto/contact.rb', line 3

def num_clicks
  @num_clicks
end

#num_opensObject

Returns the value of attribute num_opens.



3
4
5
# File 'lib/bronto/contact.rb', line 3

def num_opens
  @num_opens
end

#num_sendsObject

Returns the value of attribute num_sends.



3
4
5
# File 'lib/bronto/contact.rb', line 3

def num_sends
  @num_sends
end

#statusObject

Returns the value of attribute status.



3
4
5
# File 'lib/bronto/contact.rb', line 3

def status
  @status
end

Class Method Details

.find(filter = Bronto::Filter.new, page_number = 1, fields = nil, include_lists = false, api_key = nil) ⇒ Object

Finds contacts based on the ‘filter` (Bronto::Filter object).

  • ‘page_number` is the page of contacts to request. Bronto doesn’t specify how many contacts are returned per page,

    only that you should keep increasing the number until no more contacts are returned.
    
  • ‘fields` can be an array of field IDs or an array of Field objects.

  • ‘include_lists` determines whether to include the list IDs each contact belongs to.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/bronto/contact.rb', line 9

def self.find(filter = Bronto::Filter.new, page_number = 1, fields = nil, include_lists = false, api_key = nil)
  body = { filter: filter.to_hash, page_number: page_number }
  api_key = api_key || self.api_key

  body[:fields] = Array.wrap(fields).map { |f| f.is_a?(Bronto::Field) ? f.id : f } if Array(fields).length > 0
  body[:include_lists] = include_lists

  resp = request(:read, body)

  Array.wrap(resp[:return]).map { |hash| new(hash) }
end

.save(*objs) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bronto/contact.rb', line 21

def self.save(*objs)
  objs = objs.flatten
  api_key = objs.first.is_a?(String) ? objs.shift : self.api_key

  resp = request(:add_or_update, {plural_class_name => objs.map(&:to_hash)})

  objs.each { |o| o.errors.clear }

  Array.wrap(resp[:return][:results]).each_with_index do |result, i|
    if result[:is_error]
      objs[i].errors.add(result[:error_code], result[:error_string])
    else
      objs[i].id = result[:id]
    end
  end

  objs
end

Instance Method Details

#get_field(field) ⇒ Object



80
81
82
83
# File 'lib/bronto/contact.rb', line 80

def get_field(field)
  id = field.is_a?(Bronto::Field) ? field.id : field
  self.fields[id].try(:content)
end

#reloadObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bronto/contact.rb', line 48

def reload
  return false if self.email.blank?

  filter = Bronto::Filter.new
  filter.add_filter("email", "EqualTo", self.email)

  new_contact = self.class.find(filter, 1, self.fields, true, self.api_key).first

  self.id = new_contact.id
  self.fields = new_contact.fields
  self.list_ids = new_contact.list_ids

  true
end

#saveObject



63
64
65
# File 'lib/bronto/contact.rb', line 63

def save
  self.class.save(self)
end

#set_field(field, value) ⇒ Object



75
76
77
78
# File 'lib/bronto/contact.rb', line 75

def set_field(field, value)
  id = field.is_a?(Bronto::Field) ? field.id : field
  self.fields[id] = Field.new(id, value)
end

#to_hashObject



67
68
69
70
71
72
73
# File 'lib/bronto/contact.rb', line 67

def to_hash
  if id.present?
    { id: id, email: email, fields: fields.values.map(&:to_hash) }
  else
    { email: email, fields: fields.values.map(&:to_hash) }
  end
end