Class: MailchimpKit

Inherits:
Kit
  • Object
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, #admin_approvable?, admin_approval_requirements, admin_approve, #alternatives, #approvable?, approval_requirements, approve, #has_alternatives?, mailchimp, pad_with_new_kits, #requirements_met?, subklasses, visible, when_active

Methods included from Ext::Integrations::Kit

#record_activation, #record_approval

Instance Method Details

#add_list(list_id) ⇒ Object



82
83
84
85
86
87
88
89
# File 'app/models/kits/mailchimp_kit.rb', line 82

def add_list(list_id)
  self.attached_lists = attached_lists.reject { |list| list.empty? }
  attached_lists << {
    :list_id => list_id,
    :list_name => find_list_name(list_id)
  }
  save
end

#change_lists(new_list_ids) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/kits/mailchimp_kit.rb', line 64

def change_lists(new_list_ids)
  added_list_names = []
  added_list_ids = []
  removed_list_names = []
  lists.each do |list|
    list_id = list[1]
    if !list_attached?(list_id) && new_list_ids.include?(list_id)
      add_list(list_id)
      added_list_ids << list_id
      added_list_names << find_list_name(list_id)
    elsif list_attached?(list_id) && !new_list_ids.include?(list_id)
      remove_list(list_id)
      removed_list_names << find_list_name(list_id)
    end
  end
  send_updated_lists_email(added_list_ids, added_list_names, removed_list_names)
end

#configured!Object



40
41
42
43
# File 'app/models/kits/mailchimp_kit.rb', line 40

def configured!
  settings[:mailchimp_state] = "configured"
  save
end

#configured?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'app/models/kits/mailchimp_kit.rb', line 36

def configured?
  mailchimp_state == "configured"
end

#create_webhooks(list_id) ⇒ Object



100
101
102
103
104
105
# File 'app/models/kits/mailchimp_kit.rb', line 100

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



107
108
109
110
111
112
113
# File 'app/models/kits/mailchimp_kit.rb', line 107

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_nameObject



28
29
30
# File 'app/models/kits/mailchimp_kit.rb', line 28

def friendly_name
  "MailChimp"
end

#list_attached?(list_id) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'app/models/kits/mailchimp_kit.rb', line 60

def list_attached?(list_id)
  attached_lists.any? { |list| list[:list_id] == list_id }
end

#list_name(list_id) ⇒ Object



286
287
288
# File 'app/models/kits/mailchimp_kit.rb', line 286

def list_name(list_id)
  attached_lists.find { |list| list[:list_id] == list_id }[:list_name]
end

#listsObject



54
55
56
57
58
# File 'app/models/kits/mailchimp_kit.rb', line 54

def lists
  gibbon.lists({:start => 0, :limit=> 100})["data"].map do |list|
    [list["name"], list["id"]]
  end
end

#mailchimp_list_members(list_id) ⇒ Object



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'app/models/kits/mailchimp_kit.rb', line 369

def mailchimp_list_members(list_id)
  return @mailchimp_list_members if @mailchimp_list_members
  response = mailchimp_exporter.list(:id => list_id).to_a
  headers = 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[headers.index(attribute)] unless headers.index(attribute).nil?
      member_hash
    end
  end
end

#mailchimp_to_artfully_new_members(list_id) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'app/models/kits/mailchimp_kit.rb', line 147

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



178
179
180
181
182
183
184
185
186
# File 'app/models/kits/mailchimp_kit.rb', line 178

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

#pitchObject



32
33
34
# File 'app/models/kits/mailchimp_kit.rb', line 32

def pitch
  "Integrate your Mailchimp lists with Artful.ly"
end

#remove_list(list_id) ⇒ Object



95
96
97
98
# File 'app/models/kits/mailchimp_kit.rb', line 95

def remove_list(list_id)
  self.attached_lists = attached_lists.reject { |list| list[:list_id] == list_id }
  save
end

#send_updated_lists_email(added_list_ids, added_list_names, removed_list_names) ⇒ Object



91
92
93
# File 'app/models/kits/mailchimp_kit.rb', line 91

def send_updated_lists_email(added_list_ids, added_list_names, removed_list_names)
  Delayed::Job.enqueue MailchimpSyncJob.new(self, :type => :initial_sync, :list_ids => added_list_ids, :added_list_names => added_list_names, :removed_list_names => removed_list_names), :queue => QUEUE
end

#sync_artfully_person_update(person_id, person_changes) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'app/models/kits/mailchimp_kit.rb', line 322

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"]

  # go over the person's subscribed lists and update them
  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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/kits/mailchimp_kit.rb', line 125

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]
    }) do |person|
      person.skip_commit = true
    end
    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



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'app/models/kits/mailchimp_kit.rb', line 157

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



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'app/models/kits/mailchimp_kit.rb', line 298

def sync_mailchimp_webhook_campaign_sent(list_id, data)
  occurred_at = Time.now

  emails = []

  begin
    response = gibbon.campaign_members(:cid => data["id"])
    response["data"].each do |user|
      emails << user["email"]
    end
  end while response["total"] > emails.count

  organization.people.each do |person|
    next if !person.subscribed_lists.include?(list_id)
    next if !emails.include?(person.email)
    hear_action = HearAction.for_organization(organization)
    hear_action.details = %{"#{data["subject"].truncate(25)}" delivered to #{list_name(list_id)} MailChimp list.}
    hear_action.occurred_at = occurred_at
    hear_action.subtype = "Mailchimp (Sent)"
    hear_action.person = person
    hear_action.save
  end
end

#sync_mailchimp_webhook_cleaned(list_id, data) ⇒ Object



259
260
261
262
263
264
265
266
267
# File 'app/models/kits/mailchimp_kit.rb', line 259

def sync_mailchimp_webhook_cleaned(list_id, data)
  person = organization.people.find_by_email(data["email"])
  return if person.nil?

  reason = (data["reason"] == "hard" ? "a hard bounce" : "abuse")
  person.subscribed_lists.delete(list_id)
  person.new_note("MailChimp cleaned #{person.email} from #{list_name(list_id)} because of #{reason}.", Time.now, nil, organization_id)
  person.save
end

#sync_mailchimp_webhook_member_unsubscribe(list_id, data) ⇒ Object



290
291
292
293
294
295
296
# File 'app/models/kits/mailchimp_kit.rb', line 290

def sync_mailchimp_webhook_member_unsubscribe(list_id, data)
  person = organization.people.find_by_email(data["email"])
  return unless person
  person.new_note("Unsubscribed in MailChimp from #{list_name(list_id)}", Time.now, nil, organization_id)
  person.subscribed_lists.delete(list_id)
  person.save
end

#sync_mailchimp_webhook_new_subscriber(list_id, data) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'app/models/kits/mailchimp_kit.rb', line 204

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



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'app/models/kits/mailchimp_kit.rb', line 227

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



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'app/models/kits/mailchimp_kit.rb', line 269

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



188
189
190
# File 'app/models/kits/mailchimp_kit.rb', line 188

def sync_merged_loser_to_mailchimp(email)
  unsubscribe_email(email)
end

#sync_merged_winner_to_mailchimp(person_id, new_lists) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
# File 'app/models/kits/mailchimp_kit.rb', line 192

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



115
116
117
118
119
120
121
122
123
# File 'app/models/kits/mailchimp_kit.rb', line 115

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

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
# File 'app/models/kits/mailchimp_kit.rb', line 45

def valid_api_key?
  return unless api_key
  begin
    gibbon.ping == "Everything's Chimpy!"
  rescue
    false
  end
end