Module: Mailkick
- Defined in:
- lib/mailkick/service/mailgun.rb,
lib/mailkick.rb,
lib/mailkick/model.rb,
lib/mailkick/engine.rb,
lib/mailkick/service.rb,
lib/mailkick/version.rb,
app/models/mailkick/opt_out.rb,
lib/mailkick/service/mandrill.rb,
lib/mailkick/service/sendgrid.rb,
lib/mailkick/service/mailchimp.rb,
app/helpers/mailkick/url_helper.rb,
lib/generators/mailkick/views_generator.rb,
lib/generators/mailkick/install_generator.rb,
app/controllers/mailkick/subscriptions_controller.rb
Overview
Defined Under Namespace
Modules: Generators, Model, UrlHelper
Classes: Engine, OptOut, Service, SubscriptionsController
Constant Summary
collapse
- VERSION =
"0.4.0"
Class Method Summary
collapse
Class Method Details
.discover_services ⇒ Object
27
28
29
30
31
|
# File 'lib/mailkick.rb', line 27
def self.discover_services
Service.subclasses.each do |service|
services << service.new if service.discoverable?
end
end
|
.fetch_opt_outs ⇒ Object
23
24
25
|
# File 'lib/mailkick.rb', line 23
def self.fetch_opt_outs
services.each(&:fetch_opt_outs)
end
|
.generate_token(email, user: nil, list: nil) ⇒ Object
98
99
100
101
102
103
|
# File 'lib/mailkick.rb', line 98
def self.generate_token(email, user: nil, list: nil)
raise ArgumentError, "Missing email" unless email
user ||= Mailkick.user_method.call(email) if Mailkick.user_method
message_verifier.generate([email, user.try(:id), user.try(:class).try(:name), list])
end
|
.message_verifier ⇒ Object
94
95
96
|
# File 'lib/mailkick.rb', line 94
def self.message_verifier
@message_verifier ||= ActiveSupport::MessageVerifier.new(Mailkick.secret_token)
end
|
.opt_in(options) ⇒ Object
52
53
54
55
56
57
58
|
# File 'lib/mailkick.rb', line 52
def self.opt_in(options)
opt_outs(options).each do |opt_out|
opt_out.active = false
opt_out.save!
end
true
end
|
.opt_out(options) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/mailkick.rb', line 37
def self.opt_out(options)
unless 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
|
.opt_outs(options = {}) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/mailkick.rb', line 60
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
relation = relation.where(parts.join(" OR "), *binds) if parts.any?
relation =
if options[:list]
relation.where("list IS NULL OR list = ?", options[:list])
else
relation.where("list IS NULL")
end
relation
end
|
.opted_out?(options) ⇒ Boolean
33
34
35
|
# File 'lib/mailkick.rb', line 33
def self.opted_out?(options)
opt_outs(options).any?
end
|
.opted_out_emails(options = {}) ⇒ Object
85
86
87
|
# File 'lib/mailkick.rb', line 85
def self.opted_out_emails(options = {})
Set.new(opt_outs(options).where("email IS NOT NULL").uniq.pluck(:email))
end
|
.opted_out_users(options = {}) ⇒ Object
does not take into account emails
90
91
92
|
# File 'lib/mailkick.rb', line 90
def self.opted_out_users(options = {})
Set.new(opt_outs(options).where("user_id IS NOT NULL").map(&:user))
end
|