class MailInterceptor::Interceptor

Attributes

deliver_emails_to[RW]
env[RW]
forward_emails_to[RW]
ignore_bcc[RW]
ignore_cc[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/mail_interceptor.rb, line 9
def initialize options = {}
  @deliver_emails_to = Array.wrap options[:deliver_emails_to]
  @forward_emails_to = options.fetch :forward_emails_to
  @ignore_cc         = options.fetch :ignore_cc, true
  @ignore_bcc        = options.fetch :ignore_bcc, true
  @env               = options.fetch :env, InterceptorEnv.new

  sanitize_forward_emails_to
end

Public Instance Methods

delivering_email(message) click to toggle source
# File lib/mail_interceptor.rb, line 19
def delivering_email message
  message.to  = normalize_recipients(message.to).flatten.uniq
  message.cc  = [] if ignore_cc
  message.bcc = [] if ignore_bcc
end

Private Instance Methods

forward_emails_to_empty?() click to toggle source
# File lib/mail_interceptor.rb, line 49
def forward_emails_to_empty?
  Array.wrap(forward_emails_to).reject(&:blank?).empty?
end
normalize_recipients(recipients) click to toggle source
# File lib/mail_interceptor.rb, line 27
def normalize_recipients recipients
  return Array.wrap(recipients) unless env.intercept?

  return forward_emails_to if deliver_emails_to.empty?

  recipients.map do |recipient|
    if deliver_emails_to.find { |regex| Regexp.new(regex, Regexp::IGNORECASE).match(recipient) }
      recipient
    else
      forward_emails_to
    end
  end
end
sanitize_forward_emails_to() click to toggle source
# File lib/mail_interceptor.rb, line 41
def sanitize_forward_emails_to
  self.forward_emails_to = Array.wrap forward_emails_to

  if forward_emails_to_empty? && env.intercept?
    raise "forward_emails_to should not be empty"
  end
end