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.4"
@@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:



212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/maily_herald.rb', line 212

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



125
126
127
# File 'lib/maily_herald.rb', line 125

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.



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/maily_herald.rb', line 181

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.



351
352
353
# File 'lib/maily_herald.rb', line 351

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.



199
200
201
# File 'lib/maily_herald.rb', line 199

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)


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

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

.find_subscription_for(mailer_name, mailing_name, entity) ⇒ Object



379
380
381
382
# File 'lib/maily_herald.rb', line 379

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:



311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/maily_herald.rb', line 311

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)


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

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.



89
90
91
92
# File 'lib/maily_herald.rb', line 89

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.



109
110
111
112
# File 'lib/maily_herald.rb', line 109

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.



82
83
84
# File 'lib/maily_herald.rb', line 82

def locked_dispatches
  @@locked_dispatches ||= []
end

.locked_listsObject

Get list of locked lists.



102
103
104
# File 'lib/maily_herald.rb', line 102

def locked_lists
  @@locked_lists ||= []
end

.loggerObject

Gets the Maily logger.



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/maily_herald.rb', line 147

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:



236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/maily_herald.rb', line 236

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.



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

def options
  @options ||= read_options
end

.options=(opts) ⇒ Object

Assign config options.



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

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:



260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/maily_herald.rb', line 260

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



385
386
387
388
389
390
391
392
# File 'lib/maily_herald.rb', line 385

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.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/maily_herald.rb', line 130

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



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

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

.run_mailing(mailing_name) ⇒ Object



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

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



363
364
365
366
367
# File 'lib/maily_herald.rb', line 363

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)


169
170
171
# File 'lib/maily_herald.rb', line 169

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:



287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/maily_herald.rb', line 287

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:



162
163
164
165
166
# File 'lib/maily_herald.rb', line 162

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

  yield Initializer.new(self)
end

.start_at_procsObject



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

def start_at_procs
  @@start_at_procs ||= {}
end

.subscribe(entity, *list_names) ⇒ Object

Subscribe entity to lists identified by list_names.



329
330
331
332
333
334
335
336
# File 'lib/maily_herald.rb', line 329

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



355
356
357
358
359
360
361
# File 'lib/maily_herald.rb', line 355

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.



341
342
343
344
345
346
347
348
# File 'lib/maily_herald.rb', line 341

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

    list.unsubscribe! entity
  end
end