Class: Mautic::Model
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- Mautic::Model
show all
- Defined in:
- lib/mautic/model.rb
Overview
Virtual model for Mautic endpoint
@see https://developer.mautic.org/#endpoints
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.
45
46
47
48
49
50
51
|
# File 'lib/mautic/model.rb', line 45
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
#changed=(value) ⇒ Object
Sets the attribute changed
42
43
44
|
# File 'lib/mautic/model.rb', line 42
def changed=(value)
@changed = value
end
|
#connection ⇒ Object
Returns the value of attribute connection.
40
41
42
|
# File 'lib/mautic/model.rb', line 40
def connection
@connection
end
|
#errors ⇒ Object
Returns the value of attribute errors.
41
42
43
|
# File 'lib/mautic/model.rb', line 41
def errors
@errors
end
|
Class Method Details
.endpoint ⇒ Object
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
#attributes ⇒ Object
115
116
117
|
# File 'lib/mautic/model.rb', line 115
def attributes
@table.to_h
end
|
#attributes=(hash) ⇒ Object
119
120
121
122
123
124
|
# File 'lib/mautic/model.rb', line 119
def attributes=(hash)
hash.each_pair do |k, v|
k = k.to_sym
@table[k] = v
end
end
|
#changed? ⇒ Boolean
109
110
111
112
113
|
# File 'lib/mautic/model.rb', line 109
def changed?
return @changed unless @changed.nil?
@changed = !changes.empty?
end
|
#changes ⇒ Object
105
106
107
|
# File 'lib/mautic/model.rb', line 105
def changes
@table.changes
end
|
#create ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/mautic/model.rb', line 83
def create
begin
json = @connection.request(:post, "api/#{endpoint}/#{id && "#{id}/"}new", body: to_mautic)
assign_attributes json[endpoint.singularize]
clear_changes
rescue ValidationError => e
self.errors = e.errors
end
errors.blank?
end
|
#destroy ⇒ Object
95
96
97
98
99
100
101
102
103
|
# File 'lib/mautic/model.rb', line 95
def destroy
begin
@connection.request(:delete, "api/#{endpoint}/#{id}/delete")
true
rescue RequestError => e
self.errors = e.errors
false
end
end
|
#mautic_id ⇒ Object
53
54
55
|
# File 'lib/mautic/model.rb', line 53
def mautic_id
"#{id}/#{@connection.id}"
end
|
#save(force = false) ⇒ Object
57
58
59
|
# File 'lib/mautic/model.rb', line 57
def save(force = false)
id.present? ? update(force) : create
end
|
#to_mautic(data = @table) ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/mautic/model.rb', line 126
def to_mautic(data = @table)
data.each_with_object({}) do |(key, val), mem|
mem[key] = if val.respond_to?(:to_mautic)
val.to_mautic
elsif val.is_a?(Array)
val.join("|")
else
val
end
end
end
|
#update(force = false) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/mautic/model.rb', line 61
def update(force = false)
return false unless changed?
begin
json = @connection.request((force && :put || :patch), "api/#{endpoint}/#{id}/edit", body: to_mautic)
assign_attributes json[endpoint.singularize]
clear_changes
rescue ValidationError => e
self.errors = e.errors
end
errors.blank?
end
|
#update_columns(attributes = {}) ⇒ Object
75
76
77
78
79
80
81
|
# File 'lib/mautic/model.rb', line 75
def update_columns(attributes = {})
json = @connection.request(:patch, "api/#{endpoint}/#{id}/edit", body: to_mautic(attributes))
assign_attributes json[endpoint.singularize]
clear_changes
rescue ValidationError => e
self.errors = e.errors
end
|