Module: MailyHerald

Defined in:
lib/maily_herald.rb,
lib/maily_herald/cli.rb,
lib/maily_herald/utils.rb,
lib/maily_herald/engine.rb,
lib/maily_herald/context.rb,
lib/maily_herald/logging.rb,
lib/maily_herald/manager.rb,
lib/maily_herald/version.rb,
app/models/maily_herald/log.rb,
lib/maily_herald/autonaming.rb,
app/models/maily_herald/list.rb,
app/mailers/maily_herald/mailer.rb,
app/models/maily_herald/mailing.rb,
app/models/maily_herald/dispatch.rb,
app/models/maily_herald/sequence.rb,
lib/maily_herald/model_extensions.rb,
lib/maily_herald/template_renderer.rb,
app/models/maily_herald/subscription.rb,
app/helpers/maily_herald/tokens_helper.rb,
app/models/maily_herald/ad_hoc_mailing.rb,
app/models/maily_herald/one_time_mailing.rb,
app/models/maily_herald/sequence_mailing.rb,
app/models/maily_herald/periodical_mailing.rb,
lib/generators/maily_herald/install_generator.rb,
app/controllers/maily_herald/tokens_controller.rb,
app/controllers/maily_herald/application_controller.rb

Defined Under Namespace

Modules: Autonaming, Generators, Logging, ModelExtensions, TemplateRenderer, TokensHelper, Utils Classes: AdHocMailing, ApplicationController, Async, CLI, Context, Dispatch, Engine, Initializer, List, Log, Mailer, Mailing, Manager, OneTimeMailing, PeriodicalMailing, ScheduleUpdater, Sequence, SequenceMailing, Subscription, TokensController

Constant Summary collapse

VERSION =
"0.9.1"
@@token_redirect =
nil

Class Method Summary collapse

Class Method Details

.ad_hoc_mailing(name, options = {}) ⇒ Object

Fetches or defines an AdHocMailing.

If no block provided, AdHocMailing with given name is returned.

If block provided, AdHocMailing with given name is created or edited and block is evaluated within that mailing.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :locked (true, false) — default: false

    Determines whether Mailing is locked.

See Also:



208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/maily_herald.rb', line 208

def ad_hoc_mailing name, options = {}
  mailing = MailyHerald::AdHocMailing.where(name: name).first 
  lock = options.delete(:locked)

  if block_given? && !self.dispatch_locked?(name) && (!mailing || lock)
    mailing ||= MailyHerald::AdHocMailing.new(name: name)
    yield(mailing)
    mailing.save! 

    MailyHerald.lock_dispatch(name) if lock
  end

  mailing
end

.conditions_procsObject



121
122
123
# File 'lib/maily_herald.rb', line 121

def conditions_procs
  @@conditions_procs ||= {}
end

.context(name, &block) ⇒ Object

Fetches or defines a Context.

If no block provided, Context with given name is returned.

If block provided, Context with given name is created and then block is evaluated within that Context.

Parameters:

  • name (Symbol)

    Identifier name of the Context.



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/maily_herald.rb', line 177

def context name, &block
  name = name.to_s
  @@contexts ||= {}

  if block_given?
    @@contexts ||= {}
    @@contexts[name] ||= MailyHerald::Context.new(name)
    yield @@contexts[name]
  else
    @@contexts[name]
  end
end

.contextsObject

Return all defined Contexts.



347
348
349
# File 'lib/maily_herald.rb', line 347

def contexts
  @@contexts ||= {}
end

.dispatch(name) ⇒ Object

Returns a dispatch with given identifier name.

Dispatch is basically any object extending Dispatch.

Parameters:

  • name (Symbol)

    Dispatch identifier name.



195
196
197
# File 'lib/maily_herald.rb', line 195

def dispatch name
  MailyHerald::Dispatch.find_by_name(name)
end

.dispatch_locked?(name) ⇒ Boolean

Check if dispatch is locked.

Parameters:

  • name (Symbol)

    Dispatch identifier name.

Returns:

  • (Boolean)


93
94
95
# File 'lib/maily_herald.rb', line 93

def dispatch_locked? name
  self.locked_dispatches.include?(name.to_s)
end

.find_subscription_for(mailer_name, mailing_name, entity) ⇒ Object



375
376
377
378
# File 'lib/maily_herald.rb', line 375

def find_subscription_for mailer_name, mailing_name, entity
  mailing = MailyHerald::Mailing.where(mailer_name: mailer_name, name: mailing_name).first
  mailing.subscription_for entity
end

.list(name, options = {}) ⇒ Object

Fetches or defines a List.

If no block provided, List with given name is returned.

If block provided, List with given name is created or edited and block is evaluated within that list.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :locked (true, false) — default: false

    Determines whether List is locked.

See Also:



307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/maily_herald.rb', line 307

def list name, options = {}
  list = MailyHerald::List.where(name: name).first 
  lock = options.delete(:locked)

  if block_given? && !self.list_locked?(name) && (!list || lock)
    list ||= MailyHerald::List.new(name: name)
    yield(list)
    list.save!

    self.lock_list(name) if lock
  end

  list
end

.list_locked?(name) ⇒ Boolean

Check if List is locked.

Parameters:

  • name (Symbol)

    List identifier name.

Returns:

  • (Boolean)


113
114
115
# File 'lib/maily_herald.rb', line 113

def list_locked? name
  self.locked_lists.include?(name.to_s)
end

.lock_dispatch(name) ⇒ Object

Lock a dispatch.

Parameters:

  • name (Symbol)

    Dispatch identifier name.



85
86
87
88
# File 'lib/maily_herald.rb', line 85

def lock_dispatch name
  name = name.to_s
  self.locked_dispatches << name unless @@locked_dispatches.include?(name)
end

.lock_list(name) ⇒ Object

Lock a list.

Parameters:

  • name (Symbol)

    List identifier name.



105
106
107
108
# File 'lib/maily_herald.rb', line 105

def lock_list name
  name = name.to_s
  self.locked_lists << name unless @@locked_lists.include?(name)
end

.locked_dispatchesObject

Get list of locked dispatches.



78
79
80
# File 'lib/maily_herald.rb', line 78

def locked_dispatches
  @@locked_dispatches ||= []
end

.locked_listsObject

Get list of locked lists.



98
99
100
# File 'lib/maily_herald.rb', line 98

def locked_lists
  @@locked_lists ||= []
end

.loggerObject

Gets the Maily logger.



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/maily_herald.rb', line 143

def logger
  unless MailyHerald::Logging.initialized?
    opts = {
      level: options[:verbose] ? Logger::DEBUG : Logger::INFO,
    }
    opts[:target] = options[:logfile] if options[:logfile]

    MailyHerald::Logging.initialize(opts)
  end
  MailyHerald::Logging.logger
end

.one_time_mailing(name, options = {}) ⇒ Object

Fetches or defines an OneTimeMailing.

If no block provided, OneTimeMailing with given name is returned.

If block provided, OneTimeMailing with given name is created or edited and block is evaluated within that mailing.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :locked (true, false) — default: false

    Determines whether Mailing is locked.

See Also:



232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/maily_herald.rb', line 232

def one_time_mailing name, options = {}
  mailing = MailyHerald::OneTimeMailing.where(name: name).first 
  lock = options.delete(:locked)

  if block_given? && !self.dispatch_locked?(name) && (!mailing || lock)
    mailing ||= MailyHerald::OneTimeMailing.new(name: name)
    yield(mailing)
    mailing.save! 

    MailyHerald.lock_dispatch(name) if lock
  end

  mailing
end

.optionsObject

Returns config options read from config file.



68
69
70
# File 'lib/maily_herald.rb', line 68

def options
  @options ||= read_options
end

.options=(opts) ⇒ Object

Assign config options.



73
74
75
# File 'lib/maily_herald.rb', line 73

def options=(opts)
  @options = opts
end

.periodical_mailing(name, options = {}) ⇒ Object

Fetches or defines an PeriodicalMailing.

If no block provided, PeriodicalMailing with given name is returned.

If block provided, PeriodicalMailing with given name is created or edited and block is evaluated within that mailing.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :locked (true, false) — default: false

    Determines whether Mailing is locked.

See Also:



256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/maily_herald.rb', line 256

def periodical_mailing name, options = {}
  mailing = MailyHerald::PeriodicalMailing.where(name: name).first 
  lock = options.delete(:locked)

  if block_given? && !self.dispatch_locked?(name) && (!mailing || lock)
    mailing ||= MailyHerald::PeriodicalMailing.new(name: name)
    yield(mailing)
    mailing.save!

    self.lock_dispatch(name) if lock
  end

  mailing
end

.read_options(cfile = "config/maily_herald.yml") ⇒ Object

Read options from config file



381
382
383
384
385
386
387
388
# File 'lib/maily_herald.rb', line 381

def read_options cfile = "config/maily_herald.yml"
  opts = {}
  cfile = Pathname.new(cfile).relative? && defined?(Rails) ? Rails.root + cfile : cfile
  if File.exist?(cfile)
    opts = YAML.load(ERB.new(IO.read(cfile)).result)
  end
  opts
end

.redisObject

Obtains Redis connection.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/maily_herald.rb', line 126

def redis
  @redis ||= begin
               client = Redis.new(
                 url: options[:redis_url] || 'redis://localhost:6379/0',
                 driver: options[:redis_driver] || "ruby"
               )

               if options[:redis_namespace]
                 require 'redis/namespace'
                 Redis::Namespace.new(options[:redis_namespace], redis: client)
               else
                 client
               end
             end
end

.run_allObject



371
372
373
# File 'lib/maily_herald.rb', line 371

def run_all
  Async.perform_async(logger: MailyHerald::Logging.safe_options)
end

.run_mailing(mailing_name) ⇒ Object



365
366
367
368
369
# File 'lib/maily_herald.rb', line 365

def run_mailing mailing_name
  mailing_name = mailing_name.name if mailing_name.is_a?(Mailing)

  Async.perform_async mailing: mailing_name, logger: MailyHerald::Logging.safe_options
end

.run_sequence(seq_name) ⇒ Object



359
360
361
362
363
# File 'lib/maily_herald.rb', line 359

def run_sequence seq_name
  seq_name = seq_name.name if seq_name.is_a?(Sequence)

  Async.perform_async sequence: seq_name, logger: MailyHerald::Logging.safe_options
end

.schema_loaded?Boolean

Checks if Maily tables are present.

Returns:

  • (Boolean)


165
166
167
# File 'lib/maily_herald.rb', line 165

def schema_loaded?
  !([MailyHerald::Dispatch, MailyHerald::List, MailyHerald::Log, MailyHerald::Subscription].collect(&:table_exists?).select{|v| !v}.length > 0)
end

.sequence(name, options = {}) ⇒ Object

Fetches or defines an Sequence.

If no block provided, Sequence with given name is returned.

If block provided, Sequence with given name is created or edited and block is evaluated within that mailing. Additionally, within provided block, using MailyHerald::Sequence#mailing method, sequence mailings can be defined.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :locked (true, false) — default: false

    Determines whether Mailing is locked.

See Also:



283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/maily_herald.rb', line 283

def sequence name, options = {}
  sequence = MailyHerald::Sequence.where(name: name).first 
  lock = options.delete(:locked)

  if block_given? && !self.dispatch_locked?(name) && (!sequence || lock)
    sequence ||= MailyHerald::Sequence.new(name: name)
    yield(sequence)
    sequence.save!

    self.lock_dispatch(name) if lock
  end

  sequence
end

.setup {|Initializer.new(self)| ... } ⇒ Object

Performs Maily setup.

To be used in initializer file.

Yields:



158
159
160
161
162
# File 'lib/maily_herald.rb', line 158

def setup
  logger.warn("Maily migrations seems to be pending. Skipping setup...") && return unless schema_loaded?

  yield Initializer.new(self)
end

.start_at_procsObject



117
118
119
# File 'lib/maily_herald.rb', line 117

def start_at_procs
  @@start_at_procs ||= {}
end

.subscribe(entity, *list_names) ⇒ Object

Subscribe entity to lists identified by list_names.



325
326
327
328
329
330
331
332
# File 'lib/maily_herald.rb', line 325

def subscribe entity, *list_names
  list_names.each do |ln| 
    list = MailyHerald.list(ln)
    next unless list

    list.subscribe! entity
  end
end

.token_redirect(&block) ⇒ Object



351
352
353
354
355
356
357
# File 'lib/maily_herald.rb', line 351

def token_redirect &block
  if block_given?
    @@token_redirect = block
  else
    @@token_redirect
  end
end

.unsubscribe(entity, *list_names) ⇒ Object

Unsubscribe entity from lists identified by list_names.



337
338
339
340
341
342
343
344
# File 'lib/maily_herald.rb', line 337

def unsubscribe entity, *list_names
  list_names.each do |ln| 
    list = MailyHerald.list(ln)
    next unless list

    list.unsubscribe! entity
  end
end