module MailyHerald

Constants

VERSION

Public Class Methods

ad_hoc_mailing(name, options = {}) { |mailing| ... } click to toggle source

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.

@option options [true, false] :locked (false) Determines whether Mailing is locked. @see Dispatch#locked?

# 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_procs() click to toggle source
# File lib/maily_herald.rb, line 125
def conditions_procs
  @@conditions_procs ||= {}
end
context(name) { |contexts| ... } click to toggle source

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.

@param name [Symbol] Identifier name of the Context.

# 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
contexts() click to toggle source

Return all defined {Context Contexts}.

# File lib/maily_herald.rb, line 351
def contexts
  @@contexts ||= {}
end
dispatch(name) click to toggle source

Returns a dispatch with given identifier name.

Dispatch is basically any object extending {MailyHerald::Dispatch}.

@param name [Symbol] Dispatch identifier name.

# File lib/maily_herald.rb, line 199
def dispatch name
  MailyHerald::Dispatch.find_by_name(name)
end
dispatch_locked?(name) click to toggle source

Check if dispatch is locked.

@param name [Symbol] Dispatch identifier name.

# 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) click to toggle source
# 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 = {}) { |list| ... } click to toggle source

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.

@option options [true, false] :locked (false) Determines whether {List} is locked. @see List#locked?

# 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) click to toggle source

Check if List is locked.

@param name [Symbol] List identifier name.

# File lib/maily_herald.rb, line 117
def list_locked? name
  self.locked_lists.include?(name.to_s)
end
lock_dispatch(name) click to toggle source

Lock a dispatch.

@param name [Symbol] Dispatch identifier name.

# 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) click to toggle source

Lock a list.

@param name [Symbol] List identifier name.

# 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_dispatches() click to toggle source

Get list of locked dispatches.

# File lib/maily_herald.rb, line 82
def locked_dispatches
  @@locked_dispatches ||= []
end
locked_lists() click to toggle source

Get list of locked lists.

# File lib/maily_herald.rb, line 102
def locked_lists
  @@locked_lists ||= []
end
logger() click to toggle source

Gets the Maily logger.

# 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 = {}) { |mailing| ... } click to toggle source

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.

@option options [true, false] :locked (false) Determines whether Mailing is locked. @see Dispatch#locked?

# 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
options() click to toggle source

Returns config options read from config file.

# File lib/maily_herald.rb, line 72
def options
  @options ||= read_options
end
options=(opts) click to toggle source

Assign config options.

# File lib/maily_herald.rb, line 77
def options=(opts)
  @options = opts
end
periodical_mailing(name, options = {}) { |mailing| ... } click to toggle source

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.

@option options [true, false] :locked (false) Determines whether Mailing is locked. @see Dispatch#locked?

# 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") click to toggle source

Read options from config file

# 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
redis() click to toggle source

Obtains Redis connection.

# 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_all() click to toggle source
# File lib/maily_herald.rb, line 375
def run_all
  Async.perform_async(logger: MailyHerald::Logging.safe_options)
end
run_mailing(mailing_name) click to toggle source
# 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) click to toggle source
# 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?() click to toggle source

Checks if Maily tables are present.

# 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 = {}) { |sequence| ... } click to toggle source

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 {Sequence#mailing} method, {SequenceMailing sequence mailings} can be defined.

@option options [true, false] :locked (false) Determines whether Mailing is locked. @see Dispatch#locked? @see Sequence#mailing

# 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| ... } click to toggle source

Performs Maily setup.

To be used in initializer file.

# 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_procs() click to toggle source
# File lib/maily_herald.rb, line 121
def start_at_procs
  @@start_at_procs ||= {}
end
subscribe(entity, *list_names) click to toggle source

Subscribe entity to {List lists} identified by list_names.

@see List#subscribe!

# 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) click to toggle source
# 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) click to toggle source

Unsubscribe entity from {List lists} identified by list_names.

@see List#unsubscribe!

# 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