Class: Mautic::Model

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/mautic/model.rb

Overview

Virtual model for Mautic endpoint

@see https://developer.mautic.org/#endpoints

Direct Known Subclasses

Contact, Event, Form, Tag

Defined Under Namespace

Classes: Attribute, MauticHash

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, hash = nil) ⇒ Model

Returns a new instance of Model.

Parameters:



43
44
45
46
47
48
49
# File 'lib/mautic/model.rb', line 43

def initialize(connection, hash = nil)
  @connection = connection
  @table = MauticHash.new
  self.attributes = { id: hash['id'], created_at: hash['dateAdded']&.to_time, updated_at: hash['dateModified']&.to_time } if hash
  assign_attributes(hash)
  clear_changes
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



40
41
42
# File 'lib/mautic/model.rb', line 40

def connection
  @connection
end

Class Method Details

.endpointObject



30
31
32
# File 'lib/mautic/model.rb', line 30

def endpoint
  name.demodulize.underscore.pluralize
end

.in(connection) ⇒ Object



34
35
36
# File 'lib/mautic/model.rb', line 34

def in(connection)
  Proxy.new(connection, endpoint)
end

Instance Method Details

#attributesObject



98
99
100
# File 'lib/mautic/model.rb', line 98

def attributes
  @table.to_h
end

#attributes=(hash) ⇒ Object



102
103
104
105
106
107
# File 'lib/mautic/model.rb', line 102

def attributes=(hash)
  hash.each_pair do |k, v|
    k = k.to_sym
    @table[k] = v
  end
end

#changesObject



94
95
96
# File 'lib/mautic/model.rb', line 94

def changes
  @table.changes
end

#createObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mautic/model.rb', line 72

def create
  begin
    json = @connection.request(:post, "api/#{endpoint}/#{id && "#{id}/"}new", { body: to_mautic })
    self.attributes = json[endpoint.singularize]
    clear_changes
  rescue ValidationError => e
    self.errors = e.errors
  end

  self.errors.blank?
end

#destroyObject



84
85
86
87
88
89
90
91
92
# File 'lib/mautic/model.rb', line 84

def destroy
  begin
    @connection.request(:delete, "api/#{endpoint}/#{id}/delete")
    true
  rescue RequestError => e
    self.errors = e.errors
    false
  end
end

#mautic_idObject



51
52
53
# File 'lib/mautic/model.rb', line 51

def mautic_id
  "#{id}/#{@connection.id}"
end

#save(force = false) ⇒ Object



55
56
57
# File 'lib/mautic/model.rb', line 55

def save(force = false)
  id.present? ? update(force) : create
end

#to_mauticObject



109
110
111
112
113
# File 'lib/mautic/model.rb', line 109

def to_mautic
  @table.each_with_object({}) do |(key,val), mem|
    mem[key] = val.is_a?(Array) ? val.join("|") : val
  end
end

#update(force = false) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mautic/model.rb', line 59

def update(force = false)
  return false if changes.blank?
  begin
    json = @connection.request((force && :put || :patch), "api/#{endpoint}/#{id}/edit", { body: to_mautic })
    self.attributes = json[endpoint.singularize]
    clear_changes
  rescue ValidationError => e
    self.errors = e.errors
  end

  self.errors.blank?
end