module MailyHerald
Constants
- VERSION
Public Class Methods
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
# File lib/maily_herald.rb, line 125 def conditions_procs @@conditions_procs ||= {} end
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
Return all defined {Context Contexts}.
# File lib/maily_herald.rb, line 351 def contexts @@contexts ||= {} end
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
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
# 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
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
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 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 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
Get list of locked dispatches.
# File lib/maily_herald.rb, line 82 def locked_dispatches @@locked_dispatches ||= [] end
Get list of locked lists.
# File lib/maily_herald.rb, line 102 def locked_lists @@locked_lists ||= [] end
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
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
Returns config options read from config file.
# File lib/maily_herald.rb, line 72 def options @options ||= read_options end
Assign config options.
# File lib/maily_herald.rb, line 77 def options=(opts) @options = opts end
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 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
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
# File lib/maily_herald.rb, line 375 def run_all Async.perform_async(logger: MailyHerald::Logging.safe_options) end
# 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
# 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
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
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
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
# File lib/maily_herald.rb, line 121 def start_at_procs @@start_at_procs ||= {} end
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
# File lib/maily_herald.rb, line 355 def token_redirect &block if block_given? @@token_redirect = block else @@token_redirect end end
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