Class: WhitelistMailProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/whitelist_mail_proxy.rb

Defined Under Namespace

Classes: BlockedDelivery, SettingsError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ WhitelistMailProxy

Returns a new instance of WhitelistMailProxy.

Raises:



6
7
8
9
10
11
12
13
# File 'lib/whitelist_mail_proxy.rb', line 6

def initialize(options)
  @delivery_method = options[:delivery_method]
  @regexp = options[:regexp]
  @domains = options[:domain] && Array(options[:domain])
  
  raise SettingsError, "you must specify config.action_mailer.whitelist_proxy_settings to contain a :delivery_method"   unless @delivery_method
  raise SettingsError, "you must specify config.action_mailer.whitelist_proxy_settings to contain a :regexp or :domain" unless @regexp || @domains
end

Instance Attribute Details

#delivery_methodObject (readonly)

Returns the value of attribute delivery_method.



14
15
16
# File 'lib/whitelist_mail_proxy.rb', line 14

def delivery_method
  @delivery_method
end

#domainsObject (readonly)

Returns the value of attribute domains.



14
15
16
# File 'lib/whitelist_mail_proxy.rb', line 14

def domains
  @domains
end

#regexpObject (readonly)

Returns the value of attribute regexp.



14
15
16
# File 'lib/whitelist_mail_proxy.rb', line 14

def regexp
  @regexp
end

Class Method Details

.extract_email_address(recipient) ⇒ Object

eg.

"Matthew Rudy Jacobs"<[email protected]>
[email protected]


31
32
33
# File 'lib/whitelist_mail_proxy.rb', line 31

def self.extract_email_address(recipient)
  recipient.split("<").last.gsub(/>$/, "").strip
end

.extract_email_domain(recipient) ⇒ Object



35
36
37
# File 'lib/whitelist_mail_proxy.rb', line 35

def self.extract_email_domain(recipient)
  extract_email_address(recipient).split("@").last
end

Instance Method Details

#block_recipient?(string) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/whitelist_mail_proxy.rb', line 39

def block_recipient?(string)
  block_by_regexp?(string) || block_by_domain?(string)
end

#deliver!(mail) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/whitelist_mail_proxy.rb', line 16

def deliver!(mail)
  blocked = mail.destinations.select do |destination|
    block_recipient?(destination)
  end

  if blocked.any?
    raise BlockedDelivery.new("cannot send to #{blocked.inspect}, whitelist is #{whitelist_description}")
  else
    real_delivery_method.deliver!(mail)
  end
end

#whitelist_descriptionObject



43
44
45
46
47
48
49
# File 'lib/whitelist_mail_proxy.rb', line 43

def whitelist_description
  bits = []
  bits << "addresses LIKE #{regexp.inspect}" if self.regexp
  bits << "domains IN #{domains.inspect}"    if self.domains

  bits.join(" OR ")
end