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
10
11
12
|
# File 'app/models/mautic/contact.rb', line 10
def self.in(connection)
Proxy.new(connection, endpoint, default_params: { search: '!is:anonymous' })
end
|
Instance Method Details
#assign_attributes(source = nil) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'app/models/mautic/contact.rb', line 42
def assign_attributes(source = nil)
super
if source
self.owner = source['owner'] || {}
self.stage = source['stage'] || {}
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'],
stage_id: stage&.id,
}
end
end
|
#bounced? ⇒ Boolean
87
88
89
|
# File 'app/models/mautic/contact.rb', line 87
def bounced?
do_not_contact? && !!do_not_contact.detect { |dnc| dnc.key?(:bounced) }
end
|
128
129
130
131
132
133
134
135
136
137
138
|
# File 'app/models/mautic/contact.rb', line 128
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
|
77
78
79
80
81
82
83
84
85
|
# File 'app/models/mautic/contact.rb', line 77
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
|
95
96
97
98
99
100
101
102
103
104
105
|
# File 'app/models/mautic/contact.rb', line 95
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
|
70
71
72
|
# File 'app/models/mautic/contact.rb', line 70
def do_not_contact?
doNotContact.present?
end
|
#events ⇒ Object
63
64
65
|
# File 'app/models/mautic/contact.rb', line 63
def events
@proxy_events ||= Proxy.new(connection, "contacts/#{id}/events", klass: "Mautic::Event")
end
|
#name ⇒ Object
14
15
16
|
# File 'app/models/mautic/contact.rb', line 14
def name
"#{firstname} #{lastname}"
end
|
#owner ⇒ Hash
31
32
33
|
# File 'app/models/mautic/contact.rb', line 31
def owner
@owner || {}
end
|
#owner=(hash) ⇒ Object
option hash [Integer] :id option hash [String] :firstName option hash [String] :lastName
22
23
24
25
26
27
|
# File 'app/models/mautic/contact.rb', line 22
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
38
39
40
|
# File 'app/models/mautic/contact.rb', line 38
def owner_id=(int)
@table[:owner] = int
end
|
109
110
111
112
113
114
115
116
117
118
119
|
# File 'app/models/mautic/contact.rb', line 109
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
|
145
146
147
|
# File 'app/models/mautic/contact.rb', line 145
def stage
@stage
end
|
#stage=(object_or_hash) ⇒ Object
150
151
152
153
154
155
156
157
158
159
|
# File 'app/models/mautic/contact.rb', line 150
def stage=(object_or_hash)
@stage = case object_or_hash
when Mautic::Stage
object_or_hash
when Hash
Mautic::Stage.new(connection, object_or_hash)
end
@table[:stage_id] = @stage&.id
@stage
end
|
#to_mautic(data = @table) ⇒ Object
58
59
60
61
|
# File 'app/models/mautic/contact.rb', line 58
def to_mautic(data = @table)
data.delete(:doNotContact)
super(data)
end
|
#unsubscribed? ⇒ Boolean
91
92
93
|
# File 'app/models/mautic/contact.rb', line 91
def unsubscribed?
do_not_contact? && !!do_not_contact.detect { |dnc| dnc.key?(:unsubscribed) }
end
|