Class: Mautic::Contact

Inherits:
Model
  • Object
show all
Defined in:
app/models/mautic/contact.rb

Instance Attribute Summary

Attributes inherited from Model

#connection

Do Not Contact collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#attributes, #attributes=, #changes, #create, #destroy, endpoint, #initialize, #mautic_id, #save, #to_mautic, #update

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



15
16
17
18
19
20
21
22
23
# File 'app/models/mautic/contact.rb', line 15

def assign_attributes(source = nil)
  super
  if source
    self.attributes = {
      tags: (source['tags'] || []).collect { |t| Mautic::Tag.new(@connection, t) }.sort_by(&:name),
      doNotContact: source['doNotContact'] || [],
    }
  end
end

#bounced?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/models/mautic/contact.rb', line 48

def bounced?
  do_not_contact? && !!do_not_contact.detect { |dnc| dnc.key?(:bounced) }
end

#do_not_contactArray[Hash]

Returns:

  • (Array[Hash])


38
39
40
41
42
43
44
45
46
# File 'app/models/mautic/contact.rb', line 38

def do_not_contact
  return unless do_not_contact?

  # Based on mautic docs => Contacts constants: Contacts::UNSUBSCRIBED (1), Contacts::BOUNCED (2), Contacts::MANUAL (3)
  reason_list = { 1 => :unsubscribed, 2 => :bounced, 3 => :manual }
  @do_not_contact ||= doNotContact.collect do |hsh|
    { reason_list[hsh["reason"]] => hsh["comments"] }
  end
end

#do_not_contact!(comments: '') ⇒ Object Also known as: add_dnc



56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/mautic/contact.rb', line 56

def do_not_contact!(comments: '')
  begin
    json = @connection.request(:post, "api/contacts/#{id}/dnc/email/add", body: { comments: comments })
    self.attributes = { doNotContact: json[endpoint.singularize]["doNotContact"] }
    clear_changes
  rescue ValidationError => e
    self.errors = e.errors
  end

  self.errors.blank?
end

#do_not_contact?Boolean Also known as: dnc?



32
33
34
# File 'app/models/mautic/contact.rb', line 32

def do_not_contact?
  doNotContact.present?
end

#eventsObject



25
26
27
# File 'app/models/mautic/contact.rb', line 25

def events
  @proxy_events ||= Proxy.new(connection, "contacts/#{id}/events", klass: "Mautic::Event")
end

#nameObject



11
12
13
# File 'app/models/mautic/contact.rb', line 11

def name
  "#{firstname} #{lastname}"
end

#remove_do_not_contact!Object Also known as: remove_dnc



69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/mautic/contact.rb', line 69

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

#unsubscribed?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'app/models/mautic/contact.rb', line 52

def unsubscribed?
  do_not_contact? && !!do_not_contact.detect { |dnc| dnc.key?(:unsubscribed) }
end