diff --git a/Gemfile b/Gemfile index 2815a8c..f0851c1 100644 --- a/Gemfile +++ b/Gemfile @@ -44,6 +44,8 @@ gem 'rails_admin' gem 'mongoid_paranoia' +gem 'mailchimp-api', require: 'mailchimp' + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' @@ -58,5 +60,5 @@ group :development do gem 'web-console', '~> 2.0' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring - gem 'spring' + # gem 'spring' end diff --git a/Gemfile.lock b/Gemfile.lock index 71cea83..3acad52 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -67,6 +67,7 @@ GEM debug_inspector (0.0.2) diff-lcs (1.2.5) erubis (2.7.0) + excon (0.49.0) execjs (2.6.0) font-awesome-rails (4.6.1.0) railties (>= 3.2, < 5.1) @@ -93,6 +94,9 @@ GEM nokogiri (>= 1.5.9) mail (2.6.4) mime-types (>= 1.16, < 4) + mailchimp-api (2.0.6) + excon (>= 0.16.0) + json (>= 1.7.7) method_source (0.8.2) mime-types (3.0) mime-types-data (~> 3.2015) @@ -229,7 +233,6 @@ GEM railties (>= 3.1, < 5.0) slim (~> 3.0) slop (3.6.0) - spring (1.6.4) sprockets (3.5.2) concurrent-ruby (~> 1.0) rack (> 1, < 3) @@ -266,6 +269,7 @@ DEPENDENCIES geocoder jbuilder (~> 2.0) jquery-rails + mailchimp-api mongoid (~> 5.0.0) mongoid-rspec (= 3.0.0) mongoid_paranoia @@ -281,7 +285,6 @@ DEPENDENCIES simple_form slim slim-rails - spring turbolinks uglifier (>= 1.3.0) web-console (~> 2.0) diff --git a/lib/mail_chimp_batch_update.rb b/lib/mail_chimp_batch_update.rb new file mode 100644 index 0000000..11bea7d --- /dev/null +++ b/lib/mail_chimp_batch_update.rb @@ -0,0 +1,25 @@ +class MailChimpBatchUpdate + def self.update_all(api_key, list_id) + subscribers = [] + Supporter.each do |supporter| + subscriber = { + 'EMAIL' => { 'email' => supporter.email }, + 'language' => supporter.language, + :EMAIL_TYPE => 'html', + :merge_vars => { + 'EMAIL' => supporter.email, + 'FNAME' => supporter.first_name, + 'LNAME' => supporter.last_name, + 'SUPPORT' => supporter.support, + 'MEMBERSHIP' => supporter.li_membership.to_s, + 'AGE' => supporter.age_category, + 'LANG' => supporter.language + } + } + subscribers.push(subscriber) + end + + mailchimp = Mailchimp::API.new(api_key) + mailchimp.lists.batch_subscribe(list_id, subscribers, false, true, false) + end +end