Commit 3d88095b authored by Raphael Sofaer's avatar Raphael Sofaer

Updating batch inviter

parent 983d4e55
......@@ -19,6 +19,10 @@ vendor/bundle/*
config/database.yml
.rvmrc_custom
#Mailing list stuff
config/email_offset
config/mailing_list.csv
# Generated files
log/*
public/stylesheets/*.css
......
......@@ -2,26 +2,32 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
module RakeHelpers
def process_emails(csv, num_to_process, offset, num_invites=10, test=true)
def load_yaml
end
def process_emails(csv, num_to_process, offset, test=true)
if RUBY_VERSION.include? "1.8"
require 'fastercsv'
backers = FasterCSV.read(csv)
else
require 'csv'
backers = CSV.read(csv)
end
puts "IN TEST MODE" if test
puts "DRY RUN" if test
churn_through = 0
num_to_process.times do |n|
if backers[n+offset] == nil
break
end
churn_through = n
backer_name = backers[n+offset][0].to_s.strip
backer_email = backers[n+offset][1].to_s.gsub('.ksr', '').strip
unless User.find_by_email(backer_email)
backer_name = backers[n+offset][1].to_s.strip
backer_email = backers[n+offset][0].to_s.strip
unless User.find_by_email(backer_email) || User.find_by_invitation_identifier(backer_email)
puts "sending email to: #{backer_name} #{backer_email}" unless Rails.env == 'test'
Invitation.create_invitee(:service => 'email', :identifier => backer_email, :name => backer_name, :invites => num_invites) unless test
Invitation.create_invitee(:service => 'email', :identifier => backer_email, :name => backer_name ) unless test
else
puts "user with the email exists: #{backer_email} , #{backer_name} " unless Rails.env == 'test'
end
......
......@@ -6,29 +6,30 @@ include RakeHelpers
namespace :invites do
desc 'send a bunch of invites from a csv with rows of name, email'
task :send, :filename, :number, :start do |t, args|
puts "this task assumes the first line of your csv is just titles(1 indexed)"
puts "MAKE SURE YOU HAVE RAN THIS ON THE RIGHT DB rake 'invites:send[filename, number, start] RAILS_ ENV=production'"
puts Rails.env
unless args[:filename]
raise "please give me {filename.csv} {number of people to churn}, {where to start in the file}"
end
desc 'send a bunch of invites from a csv with rows of name, email'
task :send, :number, :test do |t, args|
require File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment')
filename = args[:filename]
start = args[:start].to_i || 0
number_of_backers = args[:number] || 1000
offset = 1 + start
puts "emailing #{number_of_backers.to_i} people listed in #{filename} starting at #{offset}"
filename = Rails.root + 'mailing_list.csv'
offset_filename = File.join(Rails.root, 'config', 'email_offset')
number_of_backers = args[:number] ? args[:number].to_i : 1000
offset = if File.exists?(offset_filename)
File.read(offset_filename).to_i
else
1
end
test = !(args[:test] == 'false')
puts "emailing #{number_of_backers} people listed in #{filename} starting at #{offset}"
finish_num = process_emails(filename, number_of_backers.to_i, offset)
finish_num = process_emails(filename, number_of_backers, offset, test)
puts "you ended on #{offset + finish_num}"
new_offset = offset + finish_num + 1
File.open(File.join(Rails.root, 'config', 'email_offset'), 'w') do |f|
f.write(new_offset)
end
puts "you ended on #{new_offset}"
puts "all done"
end
end
......@@ -15,25 +15,17 @@ describe RakeHelpers do
end
it 'should send emails to each backer' do
Invitation.should_receive(:create_invitee).exactly(3).times
process_emails(@csv, 100, 1, 10, false)
process_emails(@csv, 100, 1, false)
end
it 'should not send the email to the same email twice' do
process_emails(@csv, 100, 1, 10, false)
process_emails(@csv, 100, 1, false)
Devise.mailer.deliveries.count.should == 3
process_emails(@csv, 100, 1, 10, false)
process_emails(@csv, 100, 1, false)
Devise.mailer.deliveries.count.should == 3
end
it 'should make a user with 10 invites' do
lambda {
process_emails(@csv, 1, 1, 10, false)
}.should change(User, :count).by(1)
User.last.invites.should == 10
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment