Class: DotMailer::Contact

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account, attributes) ⇒ Contact

Returns a new instance of Contact.



32
33
34
35
# File 'lib/dot_mailer/contact.rb', line 32

def initialize(, attributes)
  self.    = 
  self.attributes = attributes
end

Class Method Details

.find_by_email(account, email) ⇒ Object



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

def self.find_by_email(, email)
  response = .client.get("/contacts/#{email}")

  new(, response)
rescue DotMailer::NotFound
  nil
end

.find_by_id(account, id) ⇒ Object

The API makes no distinction between finding by email or id, so we just delegate to Contact.find_by_email



20
21
22
# File 'lib/dot_mailer/contact.rb', line 20

def self.find_by_id(, id)
  find_by_email , id
end

.modified_since(account, time) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/dot_mailer/contact.rb', line 24

def self.modified_since(, time)
  response = .client.get("/contacts/modified-since/#{time.utc.xmlschema}")

  response.map do |attributes|
    new(, attributes)
  end
end

Instance Method Details

#[](key) ⇒ Object

A wrapper method for accessing data field values by name, e.g.:

contact['FIRSTNAME']


83
84
85
86
87
88
89
# File 'lib/dot_mailer/contact.rb', line 83

def [](key)
  if data_fields.has_key?(key)
    data_fields[key]
  else
    raise UnknownDataField, key
  end
end

#[]=(key, value) ⇒ Object

A wrapper method for assigning data field values, e.g.:

contact['FIRSTNAME'] = 'Lewis'


95
96
97
98
99
100
101
# File 'lib/dot_mailer/contact.rb', line 95

def []=(key, value)
  if data_fields.has_key?(key)
    data_fields[key] = value
  else
    raise UnknownDataField, key
  end
end

#deleteObject



107
108
109
# File 'lib/dot_mailer/contact.rb', line 107

def delete
  client.delete "/contacts/#{id}"
end

#emailObject



41
42
43
# File 'lib/dot_mailer/contact.rb', line 41

def email
  attributes['email']
end

#email=(email) ⇒ Object



45
46
47
# File 'lib/dot_mailer/contact.rb', line 45

def email=(email)
  attributes['email'] = email
end

#email_typeObject



59
60
61
# File 'lib/dot_mailer/contact.rb', line 59

def email_type
  attributes['emailType']
end

#email_type=(email_type) ⇒ Object



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

def email_type=(email_type)
  attributes['emailType'] = email_type
end

#idObject



37
38
39
# File 'lib/dot_mailer/contact.rb', line 37

def id
  attributes['id']
end

#inspectObject



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

def inspect
  to_s
end

#opt_in_typeObject



49
50
51
# File 'lib/dot_mailer/contact.rb', line 49

def opt_in_type
  attributes['optInType']
end

#opt_in_type=(opt_in_type) ⇒ Object

Raises:



53
54
55
56
57
# File 'lib/dot_mailer/contact.rb', line 53

def opt_in_type=(opt_in_type)
  raise UnknownOptInType, opt_in_type unless OptInType.exists?(opt_in_type)

  attributes['optInType'] = opt_in_type
end

#resubscribe(return_url) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/dot_mailer/contact.rb', line 115

def resubscribe(return_url)
  return false if subscribed?

  client.post_json '/contacts/resubscribe',
    'UnsubscribedContact' => {
      'id'    => id,
      'Email' => email
    },
    'ReturnUrlToUseIfChallenged' => return_url
end

#saveObject



103
104
105
# File 'lib/dot_mailer/contact.rb', line 103

def save
  client.put_json "/contacts/#{id}", attributes.merge('dataFields' => data_fields_for_api)
end

#statusObject



67
68
69
# File 'lib/dot_mailer/contact.rb', line 67

def status
  attributes['status']
end

#subscribed?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/dot_mailer/contact.rb', line 111

def subscribed?
  status == SUBSCRIBED_STATUS
end

#to_sObject



71
72
73
# File 'lib/dot_mailer/contact.rb', line 71

def to_s
  %{#{self.class.name} id: #{id}, email: #{email}, opt_in_type: #{opt_in_type}, email_type: #{email_type}, status: #{status}, data_fields: #{data_fields.to_s}}
end