class MailAllowlist

Filter mails with a specific allowlist of e-mailaddresses and only leaves those in the 'to'.

Constants

VERSION

Attributes

allowlist[R]
fallback[R]

Public Class Methods

new(allowlist, fallback = nil) click to toggle source

@param [Array<String>, include?] allowlist @param [String] fallback

# File lib/mail_allowlist.rb, line 10
def initialize(allowlist, fallback = nil)
  @allowlist = allowlist
  @fallback = fallback
end

Public Instance Methods

delivering_email(mail) click to toggle source
# File lib/mail_allowlist.rb, line 15
def delivering_email(mail)
  mail.to = mail.to.select { |recipient| allowlisted?(recipient) }
  mail.to = [fallback] unless mail.to.any?
end

Private Instance Methods

allowlisted?(recipient) click to toggle source
# File lib/mail_allowlist.rb, line 22
def allowlisted?(recipient)
  allowlist.any? do |allowlisted_address|
    if allowlisted_address.start_with?('@')
      recipient.end_with?(allowlisted_address)
    else
      allowlisted_address == recipient
    end
  end
end