lib/mailkick.rb in mailkick-0.0.3 vs lib/mailkick.rb in mailkick-0.0.4
- old
+ new
@@ -5,10 +5,11 @@
require "mailkick/model"
require "mailkick/service"
require "mailkick/service/mailchimp"
require "mailkick/service/mandrill"
require "mailkick/service/sendgrid"
+require "set"
module Mailkick
mattr_accessor :services, :user_method, :secret_token
self.services = []
self.user_method = proc{|email| User.where(email: email).first rescue nil }
@@ -23,9 +24,72 @@
Service.subclasses.each do |service|
if service.discoverable?
services << service.new
end
end
+ end
+
+ def self.opted_out?(options)
+ opt_outs(options).any?
+ end
+
+ def self.opt_out(options)
+ if !opted_out?(options)
+ time = options[:time] || Time.now
+ Mailkick::OptOut.create! do |o|
+ o.email = options[:email]
+ o.user = options[:user]
+ o.reason = options[:reason] || "unsubscribe"
+ o.list = options[:list]
+ o.created_at = time
+ o.updated_at = time
+ end
+ end
+ true
+ end
+
+ def self.opt_in(options)
+ opt_outs(options).each do |opt_out|
+ opt_out.active = false
+ opt_out.save!
+ end
+ true
+ end
+
+ def self.opt_outs(options = {})
+ relation = Mailkick::OptOut.where(active: true)
+
+ parts = []
+ binds = []
+ if (email = options[:email])
+ parts << "email = ?"
+ binds << email
+ end
+ if (user = options[:user])
+ parts << "user_id = ? and user_type = ?"
+ binds.concat [user.id, user.class.name]
+ end
+ if parts.any?
+ relation = relation.where(parts.join(" OR "), *binds)
+ end
+
+ relation =
+ if options[:list]
+ relation.where("list IS NULL OR list = ?", options[:list])
+ else
+ relation.where("list IS NULL")
+ end
+
+ relation
+ end
+
+ def self.opted_out_emails(options = {})
+ Set.new(opt_outs(options).where("email IS NOT NULL").uniq.pluck(:email))
+ end
+
+ # does not take into account emails
+ def self.opted_out_users(options = {})
+ Set.new(opt_outs(options).where("user_id IS NOT NULL").map(&:user))
end
end
ActionMailer::Base.send :include, Mailkick::Mailer