class EmailInterceptor

Constants

VERSION

Public Class Methods

new(strategy, opts={}) click to toggle source
# File lib/email_interceptor.rb, line 5
def initialize(strategy, opts={})
  strategies = [:live, :internal_only, :fake]
  @strategy = strategies.include?(strategy) ? strategy : :fake
  @fake_email_address = opts[:fake_email_address] || 'fake@example.com'
  @internal_recipient_matcher = opts[:internal_recipient_matcher]
  @logger_file = opts[:logger_file]
end

Public Instance Methods

delivering_email(message) click to toggle source
# File lib/email_interceptor.rb, line 13
def delivering_email(message)
  override_mail_to(message)
  log_delivery(message)
end
log_delivery(message) click to toggle source
# File lib/email_interceptor.rb, line 24
def log_delivery(message)
  return unless @logger_file
  File.open(@logger_file, 'a') do |f|
    f.puts "#{Time.now}\n#{message}"
  end
end
override_mail_to(message) click to toggle source
# File lib/email_interceptor.rb, line 18
def override_mail_to(message)
  unless @strategy == :live
    message.to = message.to.map { |t| override_single_recipient(t) }
  end
end

Private Instance Methods

override_single_recipient(recipient) click to toggle source
# File lib/email_interceptor.rb, line 32
def override_single_recipient(recipient)
  case @strategy
  when :internal_only
    recipient =~ @internal_recipient_matcher ? recipient : @fake_email_address
  else
    @fake_email_address
  end
end