Module: Mailkick

Defined in:
lib/mailkick/service/mailgun.rb,
lib/mailkick.rb,
lib/mailkick/model.rb,
lib/mailkick/engine.rb,
lib/mailkick/mailer.rb,
lib/mailkick/service.rb,
lib/mailkick/version.rb,
lib/mailkick/processor.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, Mailer, Model, UrlHelper Classes: Engine, OptOut, Processor, Service, SubscriptionsController

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.discover_servicesObject



25
26
27
28
29
# File 'lib/mailkick.rb', line 25

def self.discover_services
  Service.subclasses.each do |service|
    services << service.new if service.discoverable?
  end
end

.fetch_opt_outsObject



21
22
23
# File 'lib/mailkick.rb', line 21

def self.fetch_opt_outs
  services.each(&:fetch_opt_outs)
end

.opt_in(options) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/mailkick.rb', line 50

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mailkick.rb', line 35

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mailkick.rb', line 58

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

Returns:

  • (Boolean)


31
32
33
# File 'lib/mailkick.rb', line 31

def self.opted_out?(options)
  opt_outs(options).any?
end

.opted_out_emails(options = {}) ⇒ Object



83
84
85
# File 'lib/mailkick.rb', line 83

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



88
89
90
# File 'lib/mailkick.rb', line 88

def self.opted_out_users(options = {})
  Set.new(opt_outs(options).where("user_id IS NOT NULL").map(&:user))
end