Browse files

Added rake task subscribe_recent_adopters.

This task should be run periodically to subscribe recent adopters to an email list managed
by GovDelivery. It subscribes all users for whom the new subscribed_at column is null.
The column is updated after they are subscribed. To resubscribe a user, that column must be
set back to null.
  • Loading branch information...
1 parent 4d31bb4 commit cc8106b807ace2d8d118264f24aa79b252f52eba @RobertStewart RobertStewart committed Feb 27, 2013
View
2 app/models/user.rb
@@ -1,7 +1,7 @@
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable,
:trackable, :validatable
- attr_accessible :address_1, :address_2, :city, :email, :last_adoption_at, :name, :organization,
+ attr_accessible :address_1, :address_2, :city, :email, :subscribed_at, :name, :organization,
:password, :password_confirmation, :remember_me, :sms_number, :state,
:voice_number, :zip
validates_formatting_of :email, :using => :email
View
2 app/views/sidebar/_combo_form.html.haml
@@ -26,7 +26,7 @@
%label{:for => "user_voice_number", :id => "user_voice_number_label"}
= t("labels.voice_number")
%small
- = image_tag "lock.png", :class => "lock", :alt => t("captions.private"), :title => t("captions.private")
+ = image_tag "lock.png", :alt => t("captions.private"), :title => t("captions.private")
= f.telephone_field "voice_number", :placeholder => t("defaults.voice_number")
%fieldset.control-group
%label{:for => "user_sms_number", :id => "user_sms_number_label"}
View
7 db/migrate/20130225055407_add_last_adoption_at_to_users.rb
@@ -1,7 +0,0 @@
-class AddLastAdoptionDateToUsers < ActiveRecord::Migration
-
- def change
- add_column :users, :last_adoption_at, :timestamp
- end
-
-end
View
7 db/migrate/20130225055407_add_subscribed_at_to_users.rb
@@ -0,0 +1,7 @@
+class AddSubscribedAtToUsers < ActiveRecord::Migration
+
+ def change
+ add_column :users, :subscribed_at, :timestamp
+ end
+
+end
View
2 db/schema.rb
@@ -107,7 +107,7 @@
t.string "state"
t.string "zip"
t.datetime "reset_password_sent_at"
- t.datetime "last_adoption_at"
+ t.datetime "subscribed_at"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
View
32 lib/tasks/scheduler.rake
@@ -8,6 +8,38 @@ task :send_monthly_personalized => :environment do
ThingMailer.send_personalized
end
+desc "This task is called by the Heroku scheduler add-on to subscribe recent adopters to the adopt-a-drain email list"
+task :subscribe_recent_adopters, [:host, :api_key] => :environment do |t, args|
+ require 'httparty'
+
+ hostname = args[:host]
+ unless hostname
+ fail "Set first arg to correct hostname for staging or production GovDelivery server"
+ end
+ topic_id = "CAOAKL_58"
+ api_key = args[:api_key]
+ unless api_key
+ fail "Set second arg to correct api key for staging or production GovDelivery server"
+ end
+ api_root = "https://#{hostname}"
+
+ # Subscribe all users who have adopted a drain and not yet been subscribed to the mailing list.
+ # We don't resubscribe on each adoption, since we don't know if they intentionally unsubscribed previously.
+ sql = "select distinct (u.*) from things_users tu inner join users u on u.id = tu.user_id where subscribed_at is NULL"
+ User.find_by_sql(sql).each do |user|
+ request_path = "/api/add_script_subscription?t=#{topic_id}&k=#{api_key}&e=#{user.email}"
+ uri = api_root + request_path
+ Rails.logger.info "Attempting to subscribe #{user.email}"
+ begin
+ response = HTTParty.get(uri)
+ Rails.logger.info response.body
+ # ToDo: Examine response body and set subscribed_at only if we're sure the request succeeded
+ user.update_attributes(:subscribed_at => Time.current)
+ rescue
+ Rails.logger.error "Error subscribing #{user.email}"
+ end
+ end
+end
desc "This task is called by the Heroku scheduler add-on to send out personalized monthly reminders"
task :send_reminder => :environment do

0 comments on commit cc8106b

Please sign in to comment.