VRFS-1483 integrating sendgrid_smtpapi

This commit is contained in:
Jonathan Kolyer 2014-03-19 06:20:58 +00:00
parent e8dd5f6ae5
commit 4ba18a88ee
5 changed files with 33 additions and 30 deletions

View File

@ -41,6 +41,7 @@ gem 'resque-retry'
gem 'resque-failed-job-mailer' #, :path => "/Users/seth/workspace/resque_failed_job_mailer"
gem 'resque-lonely_job', '~> 1.0.0'
gem 'resque_mailer'
gem 'sendgrid_smtpapi'
gem 'oj'
gem 'builder'
gem 'fog'

View File

@ -16,6 +16,8 @@ require "geokit-rails"
require "postgres_ext"
require 'builder'
require 'cgi'
require 'resque_mailer'
require 'sendgrid_smtpapi'
require "jam_ruby/constants/limits"
require "jam_ruby/constants/notification_types"

View File

@ -8,30 +8,35 @@ module JamRuby
def send_batch_email(batch_id, user_ids)
@users = User.find_all_by_id(user_ids)
@user = User.where(:id => user_id).limit(1).first
batch = EmailBatch.where(:id => batch_id).limit(1).first
@body = batch.merged_body(user)
mail(:to => user.email,
:from => batch.from_email,
:subject => batch.subject) do |format|
format.text
format.html
end
batch.did_send(user.email)
end
def send_batch_email_test(batch_id, email_addy)
batch = EmailBatch.where(:id => batch_id).limit(1).first
@body = batch.body
mail(:to => email_addy,
substitute(EmailBatch::VAR_FIRST_NAME, @users.map(&:first_name))
emails = @users.map(&:email)
mail(:to => emails,
:from => batch.from_email,
:subject => batch.subject) do |format|
format.text
format.html
end
batch.did_send(email_addy)
batch.did_send(emails)
end
def send_batch_email_test(batch_id)
batch = EmailBatch.where(:id => batch_id).limit(1).first
@body = batch.body
@users = batch.test_users
emails = @users.map(&:email)
substitute(EmailBatch::VAR_FIRST_NAME, @users.map(&:first_name))
mail(:to => emails,
:from => batch.from_email,
:subject => batch.subject) do |format|
format.text
format.html
end
batch.did_send(emails)
end
end

View File

@ -21,7 +21,7 @@ module JamRuby
event :enable do
transitions :from => :disabled, :to => :pending
end
event :do_test_run, :after => Proc.new { running_tests } do
event :do_test_run, :after => :running_tests do
transitions :from => [:pending, :tested, :batched], :to => :testing
end
event :did_test_run do
@ -47,9 +47,8 @@ module JamRuby
def deliver
self.do_batch_run!
self.class.qualified_users.find_each do |uu|
BatchMailer.send_batch_email_test(self.id, uu.id).deliver
self.class.qualified_users.pluck(:id).find_in_batches(batch_size: 100) do |user_ids|
BatchMailer.send_batch_email(self.id, user_ids).deliver
end
end
@ -66,18 +65,16 @@ module JamRuby
def send_test_batch
self.do_test_run!
self.test_users.each do |uu|
BatchMailer.send_batch_email_test(self.id, uu.email).deliver
end
BatchMailer.send_batch_email_test(self.id).deliver
end
def merged_body(user)
body.gsub(VAR_FIRST_NAME, user.first_name).gsub(VAR_LAST_NAME, user.last_name)
end
def did_send(email)
self.update_with_conflict_validation({ :sent_count => self.sent_count + 1 })
def did_send(emails)
self.update_with_conflict_validation({ :sent_count => self.sent_count + emails.size })
if self.sent_count >= self.qualified_count
if batching?
self.did_batch_run!
@ -87,8 +84,6 @@ module JamRuby
end
end
protected
def update_with_conflict_validation(*args)
num_try = 0
update_attributes(*args)
@ -106,7 +101,7 @@ module JamRuby
})
end
def running_test
def running_tests
self.update_attributes({:qualified_count => self.class.qualified_users.count,
:sent_count => 0
})

View File

@ -8,10 +8,10 @@ describe BatchMailer do
batch = FactoryGirl.create(:email_batch)
batch.send_test_batch
it { BatchMailer.deliveries.length.should == 4 }
it { BatchMailer.deliveries.length.should == 1 }
it { mail['from'].to_s.should == EmailBatch::DEFAULT_SENDER }
it { mail['to'].to_s.should == batch.test_emails.split(',')[0] }
it { mail['to'].to_s.split(',')[0].should == batch.test_emails.split(',')[0] }
it { mail.subject.should == batch.subject }
it { mail.multipart?.should == true } # because we send plain + html