Class: MailchimpKit
- Inherits:
-
Kit
- Object
- ActiveRecord::Base
- Kit
- MailchimpKit
show all
- Defined in:
- app/models/kits/mailchimp_kit.rb
Constant Summary
collapse
- QUEUE =
"mailchimp"
Instance Method Summary
collapse
Methods inherited from Kit
#abilities, #activatable?, activate, activation_requirements, acts_as_kit, #alternatives, #approvable?, approval_requirements, approve, #has_alternatives?, mailchimp, pad_with_new_kits, #requirements_met?, subklasses, visible, when_active
#record_activation, #record_approval
Instance Method Details
#add_list(list_id) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'app/models/kits/mailchimp_kit.rb', line 74
def add_list(list_id)
list_name = lists.find { |list| list[1] == list_id }[0]
self.attached_lists = attached_lists.reject { |list| list.empty? }
attached_lists << {
:list_id => list_id,
:list_name => list_name
}
save
Delayed::Job.enqueue MailchimpSyncJob.new(self, :type => :initial_sync, :list_id => list_id), :queue => QUEUE
end
|
#change_lists(new_list_ids) ⇒ Object
63
64
65
66
67
68
69
70
71
72
|
# File 'app/models/kits/mailchimp_kit.rb', line 63
def change_lists(new_list_ids)
lists.each do |list|
list_id = list[1]
if !list_attached?(list_id) && new_list_ids.include?(list_id)
add_list(list_id)
elsif list_attached?(list_id) && !new_list_ids.include?(list_id)
remove_list(list_id)
end
end
end
|
39
40
41
42
|
# File 'app/models/kits/mailchimp_kit.rb', line 39
def configured!
settings[:mailchimp_state] = "configured"
save
end
|
35
36
37
|
# File 'app/models/kits/mailchimp_kit.rb', line 35
def configured?
mailchimp_state == "configured"
end
|
#create_webhooks(list_id) ⇒ Object
93
94
95
96
97
98
|
# File 'app/models/kits/mailchimp_kit.rb', line 93
def create_webhooks(list_id)
gibbon.list_webhook_add({
:id => list_id,
:url => Rails.application.routes.url_helpers.mailchimp_webhook_url(id, :list_id => list_id, :host => MAILCHIMP_WEBHOOK_URL[:host], :protocol => MAILCHIMP_WEBHOOK_URL[:protocol] || "http")
})
end
|
#destroy_webhooks(list_id) ⇒ Object
100
101
102
103
104
105
106
|
# File 'app/models/kits/mailchimp_kit.rb', line 100
def destroy_webhooks(list_id)
gibbon = Gibbon.new(old_api_key || api_key)
gibbon.list_webhook_del({
:id => list_id,
:url => Rails.application.routes.url_helpers.mailchimp_webhook_url(id, :list_id => list_id, :host => MAILCHIMP_WEBHOOK_URL[:host], :protocol => MAILCHIMP_WEBHOOK_URL[:protocol] || "http")
})
end
|
#friendly_name ⇒ Object
27
28
29
|
# File 'app/models/kits/mailchimp_kit.rb', line 27
def friendly_name
"MailChimp"
end
|
#list_attached?(list_id) ⇒ Boolean
59
60
61
|
# File 'app/models/kits/mailchimp_kit.rb', line 59
def list_attached?(list_id)
attached_lists.any? { |list| list[:list_id] == list_id }
end
|
#list_name(list_id) ⇒ Object
267
268
269
|
# File 'app/models/kits/mailchimp_kit.rb', line 267
def list_name(list_id)
attached_lists.find { |list| list[:list_id] == list_id }[:list_name]
end
|
#lists ⇒ Object
53
54
55
56
57
|
# File 'app/models/kits/mailchimp_kit.rb', line 53
def lists
gibbon.lists["data"].map do |list|
[list["name"], list["id"]]
end
end
|
#mailchimp_list_members(list_id) ⇒ Object
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
# File 'app/models/kits/mailchimp_kit.rb', line 346
def mailchimp_list_members(list_id)
return @mailchimp_list_members if @mailchimp_list_members
response = mailchimp_exporter.list(:id => list_id).to_a
= JSON.parse(response.shift)
members = response.map { |line| JSON.parse(line) }
@mailchimp_list_members ||= members.collect do |member|
member_hash = {}
mailchimp_attributes.inject({}) do |member_hash, attribute|
member_hash[attribute] = member[.index(attribute)]
member_hash
end
end
end
|
#mailchimp_to_artfully_new_members(list_id) ⇒ Object
138
139
140
141
142
143
144
145
146
|
# File 'app/models/kits/mailchimp_kit.rb', line 138
def mailchimp_to_artfully_new_members(list_id)
members_not_in_artfully = []
mailchimp_list_members(list_id).each do |member|
if !organization_people_emails.include?(member["Email Address"])
members_not_in_artfully << member
end
end
members_not_in_artfully
end
|
#mailchimp_to_artfully_update_members(list_id) ⇒ Object
169
170
171
172
173
174
175
176
177
|
# File 'app/models/kits/mailchimp_kit.rb', line 169
def mailchimp_to_artfully_update_members(list_id)
members_in_mailchimp = []
mailchimp_list_members(list_id).each do |member|
if organization_people_emails.include?(member["Email Address"])
members_in_mailchimp << member
end
end
members_in_mailchimp
end
|
#pitch ⇒ Object
31
32
33
|
# File 'app/models/kits/mailchimp_kit.rb', line 31
def pitch
"Integrate your Mailchimp lists with Artful.ly"
end
|
#remove_list(list_id) ⇒ Object
87
88
89
90
91
|
# File 'app/models/kits/mailchimp_kit.rb', line 87
def remove_list(list_id)
self.attached_lists = attached_lists.reject { |list| list[:list_id] == list_id }
save
Delayed::Job.enqueue MailchimpSyncJob.new(self, :type => :list_removal, :list_id => list_id), :queue => QUEUE
end
|
#sync_artfully_person_update(person_id, person_changes) ⇒ Object
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
# File 'app/models/kits/mailchimp_kit.rb', line 299
def sync_artfully_person_update(person_id, person_changes)
person = organization.people.find_by_id(person_id)
return unless person
merge_vars = {}
merge_vars["FNAME"] = person_changes["first_name"][1] if person_changes["first_name"]
merge_vars["LNAME"] = person_changes["last_name"][1] if person_changes["last_name"]
email = person_changes["email"] ? person_changes["email"][0] : person.email
if person_changes.has_key?("do_not_email") && person_changes["do_not_email"][1]
return attached_lists.all? do |list|
unsubscribe_email(email, list[:list_id])
end
end
if person_changes.has_key?("subscribed_lists")
return attached_lists.all? do |list|
old_lists = person_changes["subscribed_lists"][0]
new_lists = person_changes["subscribed_lists"][1]
if !old_lists.include?(list[:list_id]) && new_lists.include?(list[:list_id])
first_name = merge_vars["FNAME"] || person.first_name
last_name = merge_vars["LNAME"] || person.last_name
subscribe_email(email, first_name, last_name, list[:list_id])
elsif old_lists.include?(list[:list_id]) && !new_lists.include?(list[:list_id])
unsubscribe_email(email, list[:list_id])
else
true
end
end
end
return unless sync_person?(person)
merge_vars["EMAIL"] = person_changes["email"][1] if person_changes["email"]
person.subscribed_lists.each do |list_id|
next unless attached_lists.any? { |list| list[:list_id] == list_id }
gibbon.list_update_member({
:id => list_id,
:email_address => email,
:merge_vars => merge_vars
})
end
end
|
#sync_mailchimp_to_artfully_new_members(list_id) ⇒ Object
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'app/models/kits/mailchimp_kit.rb', line 118
def sync_mailchimp_to_artfully_new_members(list_id)
set_count_from_mailchimp(list_id)
mailchimp_to_artfully_new_members(list_id).each do |member|
person = organization.people.create({
:first_name => member["First Name"],
:last_name => member["Last Name"],
:email => member["Email Address"],
:skip_sync_to_mailchimp => true,
:subscribed_lists => [list_id]
})
note = person.notes.build({
:text => "Imported from MailChimp",
:occurred_at => Time.now
})
note.organization_id = organization_id
note.save
end
end
|
#sync_mailchimp_to_artfully_update_members(list_id) ⇒ Object
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# File 'app/models/kits/mailchimp_kit.rb', line 148
def sync_mailchimp_to_artfully_update_members(list_id)
set_count_merged_mailchimp(list_id)
mailchimp_to_artfully_update_members(list_id).each do |member|
person = organization.people.find_by_email(member["Email Address"])
next if person.do_not_email?
member.each do |attribute, value|
attribute = mailchimp_attributes_to_artfully[attribute]
if person.send(attribute).blank?
person.send("#{attribute}=", value)
end
end
person.skip_sync_to_mailchimp = true
person.subscribed_lists << list_id
person.save
end
end
|
#sync_mailchimp_webhook_campaign_sent(list_id, data) ⇒ Object
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
# File 'app/models/kits/mailchimp_kit.rb', line 284
def sync_mailchimp_webhook_campaign_sent(list_id, data)
occurred_at = Time.now
organization.people.each do |person|
next if !person.subscribed_lists.include?(list_id)
hear_action = HearAction.new
hear_action.set_params({
:details => %{"#{data["subject"].truncate(25)}" delivered to #{list_name(list_id)} MailChimp list.},
:occurred_at => occurred_at,
:subtype => "Email (Sent)"
}, person)
hear_action.organization_id = organization_id
hear_action.save
end
end
|
#sync_mailchimp_webhook_member_unsubscribe(list_id, data) ⇒ Object
271
272
273
274
275
276
277
278
279
280
281
282
|
# File 'app/models/kits/mailchimp_kit.rb', line 271
def sync_mailchimp_webhook_member_unsubscribe(list_id, data)
person = organization.people.find_by_email(data["email"])
return unless person
note = person.notes.build({
:text => "Unsubscribed in MailChimp from #{list_name(list_id)}",
:occurred_at => Time.now
})
note.organization_id = organization_id
note.save
person.subscribed_lists.delete(list_id)
person.save
end
|
#sync_mailchimp_webhook_new_subscriber(list_id, data) ⇒ Object
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
# File 'app/models/kits/mailchimp_kit.rb', line 195
def sync_mailchimp_webhook_new_subscriber(list_id, data)
if person = organization.people.find_by_email(data["email"])
person.subscribed_lists << list_id
person.save
return sync_mailchimp_webhook_update_person(list_id, data)
end
person = organization.people.create({
:first_name => data["merges"]["FNAME"],
:last_name => data["merges"]["LNAME"],
:email => data["email"],
:skip_sync_to_mailchimp => true,
:subscribed_lists => [list_id]
})
note = person.notes.build({
:text => "Imported from MailChimp",
:occurred_at => Time.now
})
note.organization_id = organization_id
note.save
person
end
|
#sync_mailchimp_webhook_update_person(list_id, data) ⇒ Object
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
# File 'app/models/kits/mailchimp_kit.rb', line 218
def sync_mailchimp_webhook_update_person(list_id, data)
person = organization.people.find_by_email(data["email"])
if person.nil?
Rails.logger.warn "WARNING: Mailchimp sent an update webhook with an out of date email: #{data["email"]}"
return
end
return if person.do_not_email?
data["merges"].each do |attribute, value|
attribute = mailchimp_merges_to_artfully[attribute]
person.send("#{attribute}=", value) if attribute
end
person.subscribed_lists.each do |subscribed_list_id|
next if subscribed_list_id == list_id
gibbon.list_update_member({
:id => subscribed_list_id,
:email_address => person.email,
:merge_vars => {
"FNAME" => person.first_name,
"LNAME" => person.last_name
}
})
end
person.skip_sync_to_mailchimp = true
person.save
person
end
|
#sync_mailchimp_webhook_update_person_email(list_id, data) ⇒ Object
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
# File 'app/models/kits/mailchimp_kit.rb', line 250
def sync_mailchimp_webhook_update_person_email(list_id, data)
person = organization.people.find_by_email(data["old_email"])
return if person.nil? || person.do_not_email?
person.update_attributes(:email => data["new_email"], :skip_sync_to_mailchimp => true)
person.subscribed_lists.each do |subscribed_list_id|
next if subscribed_list_id == list_id
gibbon.list_update_member({
:id => subscribed_list_id,
:email_address => data["old_email"],
:merge_vars => {
"EMAIL" => data["new_email"]
}
})
end
end
|
#sync_merged_loser_to_mailchimp(email) ⇒ Object
179
180
181
|
# File 'app/models/kits/mailchimp_kit.rb', line 179
def sync_merged_loser_to_mailchimp(email)
unsubscribe_email(email)
end
|
#sync_merged_winner_to_mailchimp(person_id, new_lists) ⇒ Object
183
184
185
186
187
188
189
190
191
192
193
|
# File 'app/models/kits/mailchimp_kit.rb', line 183
def sync_merged_winner_to_mailchimp(person_id, new_lists)
person = Person.find(person_id)
new_lists.each do |list_id|
gibbon.list_subscribe({
:id => list_id,
:email_address => person.email,
:merge_vars => { "FNAME" => person.first_name, "LNAME" => person.last_name },
:double_optin => false
})
end
end
|
#unsubscribe_old_members(list_id) ⇒ Object
108
109
110
111
112
113
114
115
116
|
# File 'app/models/kits/mailchimp_kit.rb', line 108
def unsubscribe_old_members(list_id)
organization.people.each do |person|
unless person.subscribed_lists.delete(list_id).nil?
person.skip_commit = true
person.save
end
end
Sunspot.delay.commit
end
|
#valid_api_key? ⇒ Boolean
44
45
46
47
48
49
50
51
|
# File 'app/models/kits/mailchimp_kit.rb', line 44
def valid_api_key?
return unless api_key
begin
gibbon.ping == "Everything's Chimpy!"
rescue
false
end
end
|