Class: Mautic::Contact
- Inherits:
-
Model
- Object
- OpenStruct
- Model
- Mautic::Contact
show all
- Defined in:
- app/models/mautic/contact.rb
Instance Attribute Summary
Attributes inherited from Model
#changed, #connection, #errors
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Model
#attributes, #attributes=, #changed?, #changes, #create, #destroy, endpoint, #initialize, #mautic_id, #save, #update, #update_columns
Constructor Details
This class inherits a constructor from Mautic::Model
Class Method Details
.in(connection) ⇒ Object
7
8
9
|
# File 'app/models/mautic/contact.rb', line 7
def self.in(connection)
Proxy.new(connection, endpoint, default_params: { search: '!is:anonymous' })
end
|
Instance Method Details
#assign_attributes(source = nil) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'app/models/mautic/contact.rb', line 39
def assign_attributes(source = nil)
super
if source
self.owner = source['owner'] || {}
tags = (source['tags'] || []).map { |t| Mautic::Tag.new(self, t) }.sort_by(&:name)
self.attributes = {
tags: Tag::Collection.new(self, *tags),
doNotContact: source['doNotContact'] || [],
owner: owner['id'],
}
end
end
|
#bounced? ⇒ Boolean
82
83
84
|
# File 'app/models/mautic/contact.rb', line 82
def bounced?
do_not_contact? && !!do_not_contact.detect { |dnc| dnc.key?(:bounced) }
end
|
123
124
125
126
127
128
129
130
131
132
133
|
# File 'app/models/mautic/contact.rb', line 123
def campaigns
return @campaigns if @campaigns
json = @connection.request(:get, "api/contacts/#{id}/campaigns")
@campaigns = json["campaigns"].collect do |_campaign_id, campaign_attributes|
Mautic::Campaign.new @connection, campaign_attributes
end
rescue RequestError => _e
[]
end
|
72
73
74
75
76
77
78
79
80
|
# File 'app/models/mautic/contact.rb', line 72
def do_not_contact
return unless do_not_contact?
reason_list = { 1 => :unsubscribed, 2 => :bounced, 3 => :manual }
@do_not_contact ||= doNotContact.collect do |hsh|
{ reason_list[hsh["reason"]] => hsh["comments"] }
end
end
|
90
91
92
93
94
95
96
97
98
99
100
|
# File 'app/models/mautic/contact.rb', line 90
def do_not_contact!(comments: '')
begin
json = @connection.request(:post, "api/contacts/#{id}/dnc/email/add", body: { comments: })
self.attributes = { doNotContact: json[endpoint.singularize]["doNotContact"] }
clear_changes
rescue ValidationError => e
self.errors = e.errors
end
errors.blank?
end
|
65
66
67
|
# File 'app/models/mautic/contact.rb', line 65
def do_not_contact?
doNotContact.present?
end
|
#events ⇒ Object
58
59
60
|
# File 'app/models/mautic/contact.rb', line 58
def events
@proxy_events ||= Proxy.new(connection, "contacts/#{id}/events", klass: "Mautic::Event")
end
|
#name ⇒ Object
11
12
13
|
# File 'app/models/mautic/contact.rb', line 11
def name
"#{firstname} #{lastname}"
end
|
#owner ⇒ Hash
28
29
30
|
# File 'app/models/mautic/contact.rb', line 28
def owner
@owner || {}
end
|
#owner=(hash) ⇒ Object
option hash [Integer] :id option hash [String] :firstName option hash [String] :lastName
19
20
21
22
23
24
|
# File 'app/models/mautic/contact.rb', line 19
def owner=(hash)
raise ArgumentError, "must be a hash !" unless hash.is_a?(Hash)
@table[:owner] = hash["id"]
@owner = hash
end
|
#owner_id=(int) ⇒ Object
Assign mautic User ID as owner - for example for update author of contact
35
36
37
|
# File 'app/models/mautic/contact.rb', line 35
def owner_id=(int)
@table[:owner] = int
end
|
104
105
106
107
108
109
110
111
112
113
114
|
# File 'app/models/mautic/contact.rb', line 104
def remove_do_not_contact!
begin
json = @connection.request(:post, "api/contacts/#{id}/dnc/email/remove", body: {})
self.attributes = { doNotContact: json[endpoint.singularize]["doNotContact"] }
clear_changes
rescue ValidationError => e
self.errors = e.errors
end
self.errors.blank?
end
|
#to_mautic(data = @table) ⇒ Object
53
54
55
56
|
# File 'app/models/mautic/contact.rb', line 53
def to_mautic(data = @table)
data.delete(:doNotContact)
super(data)
end
|
#unsubscribed? ⇒ Boolean
86
87
88
|
# File 'app/models/mautic/contact.rb', line 86
def unsubscribed?
do_not_contact? && !!do_not_contact.detect { |dnc| dnc.key?(:unsubscribed) }
end
|