Skip to content

Commit

Permalink
Register the class Bleach instead of an instance of Bleach
Browse files Browse the repository at this point in the history
  • Loading branch information
pboling committed Jul 24, 2016
1 parent e40d482 commit 31822a8
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 43 deletions.
33 changes: 13 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,35 +81,26 @@ There are four ways SanitizeEmail can be turned on; in order of precedence they
SanitizeEmail.force_sanitize = true # by default it is nil
```

2. If SanitizeEmail seems to not be sanitizing you have probably not registered the interceptor. SanitizeEmail tries to do this for you. *Note*: If you are working in an environment that has a Mail or Mailer class that uses the register_interceptor API, the interceptor will already have been registered by SanitizeEmail (however, note lack of `:engage => true`):
2. If SanitizeEmail seems to not be sanitizing you have probably not registered the interceptor. SanitizeEmail tries to do this for you. *Note*: If you are working in an environment that has a Mail or Mailer class that uses the register_interceptor API, the interceptor will already have been registered by SanitizeEmail:

```ruby
Mail.register_interceptor(SanitizeEmail::Bleach.new(:engage => true)) # by default :engage is nil
# The gem will probably have already done this for you, but some really old versions of Rails may need you to do this manually:
Mail.register_interceptor(SanitizeEmail::Bleach)
```

Without :engage => true the interceptor is inactive, and will require engaging via one of the other methods.

```ruby
Mail.register_interceptor(SanitizeEmail::Bleach.new)
```
As an example you could do the following to engage SanitizeEmail:
Once registered, SanitizeEmail needs to be engaged:

```ruby
# in config/initializers/sanitize_email.rb
SanitizeEmail::Config.configure {|config| config[:engage] = true }
```

3. If you don't need to compute anything, then don't use this option, go with the next option.
3. If you don't need to compute anything, then don't use this option, go with the previous option.

```ruby
SanitizeEmail::Config.configure {|config| config[:activation_proc] = Proc.new { true } } # by default :activation_proc is false
```

4. This will turn it on. Period.

```ruby
SanitizeEmail::Config.configure {|config| config[:engage] = true } # by default :engage is nil
```

### Notes

Number 1, above, is the method used by the SanitizeEmail.sanitary block.
Expand All @@ -120,7 +111,9 @@ If installed but not configured, sanitize_email DOES NOTHING. Until configured
IMPORTANT: You may need to setup your own register_interceptor. If sanitize_email doesn't seem to be working for you find your Mailer/Mail class and try this:

```ruby
Mail.register_interceptor(SanitizeEmail::Bleach.new(:engage => true))
# in config/initializers/sanitize_email.rb
Mail.register_interceptor(SanitizeEmail::Bleach)
SanitizeEmail::Config.configure {|config| config[:engage] = true }
```

If that causes an error you will know why sanitize_email doesn't work.
Expand Down Expand Up @@ -210,8 +203,8 @@ SanitizeEmail::Config.configure do |config|
config[:use_actual_email_as_sanitized_user_name] = true # or false
end

# If your mail system is not one that sanitize_email automatically configures an interceptor for (ActionMailer, Mail) then you will need to do the equivalent for whatever Mail system you are using:
# Mail.register_interceptor(SanitizeEmail::Bleach.new)
# If your mail system is not one that sanitize_email automatically configures an interceptor for (ActionMailer, Mail)
# then you will need to do the equivalent for whatever Mail system you are using.

RSpec.configure do |config|
# ...
Expand Down Expand Up @@ -297,8 +290,8 @@ SanitizeEmail::Config.configure do |config|
config[:use_actual_email_as_sanitized_user_name] = true # or false
end

# If your mail system is not one that sanitize_email automatically configures an interceptor for (ActionMailer, Mail) then you will need to do the equivalent for whatever Mail system you are using:
# Mail.register_interceptor(SanitizeEmail::Bleach.new)
# If your mail system is not one that sanitize_email automatically configures an interceptor for (ActionMailer, Mail)
# then you will need to do the equivalent for whatever Mail system you are using.

# You need to know what to do here... somehow get the methods into rhw scope of your tests.
# Something like this maybe?
Expand Down
2 changes: 1 addition & 1 deletion lib/sanitize_email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MissingBlockParameter < StandardError; end
mailer = Mail
end
if mailer.respond_to?(:register_interceptor)
mailer.register_interceptor(SanitizeEmail::Bleach.new)
mailer.register_interceptor(SanitizeEmail::Bleach)
else
warn "SanitizeEmail was unable to detect a compatible Mail class to register an interceptor on."
end
Expand Down
27 changes: 10 additions & 17 deletions lib/sanitize_email/bleach.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ module SanitizeEmail
# SanitizeEmail::Bleach determines whether to sanitize the headers of an email, and does so when appropriate.
class Bleach

# Can override global configs at the instance level.
attr_accessor :engage, # Turn sanitization on or off just for this instance
:overridden_addresses # TODO: Just a stub. Needs actual implementation
attr_accessor :overridden_addresses # TODO: Just a stub. Needs actual implementation

def initialize(args = {})
def initialize
# Not using extract_options! because non-rails compatibility is a goal
@engage = args[:engage] || SanitizeEmail::Config.config[:engage]
@engage = SanitizeEmail::Config.config[:engage]
end

# If all recipient addresses are white-listed the field is left alone.
def delivering_email(message)
def self.delivering_email(message)
if self.sanitize_engaged?(message)
SanitizeEmail::MailHeaderTools.add_original_addresses_as_headers(message)
SanitizeEmail::MailHeaderTools.prepend_custom_subject(message)
Expand All @@ -34,12 +32,11 @@ def delivering_email(message)
# 1. SanitizeEmail.force_sanitize = true # by default it is nil
# Only useful for local context. Inside a method where you will be sending an email,
# set SanitizeEmail.force_sanitize = true just prior to delivering it. Also useful in the console.
# 2. Mail.register_interceptor(SanitizeEmail::Bleach.new(:engage => true)) # by default it is nil
# If SanitizeEmail seems to not be sanitizing you have probably not registered the interceptor. SanitizeEmail tries to do this for you.
# Note: If you are working in an environment that has a Mail or Mailer class that uses the register_interceptor API, the interceptor will already have been registered by SanitizeEmail (however, note lack of :engage => true):
# Mail.register_interceptor(SanitizeEmail::Bleach.new
# Without :engage => true the interceptor is inactive, and will require engaging via one of the other methods.
# As an example you could do the following to engage SanitizeEmail:
# 2. If SanitizeEmail seems to not be sanitizing you have probably not registered the interceptor. SanitizeEmail tries to do this for you. *Note*: If you are working in an environment that has a Mail or Mailer class that uses the register_interceptor API, the interceptor will already have been registered by SanitizeEmail:
# The gem will probably have already done this for you, but some really old versions of Rails may need you to do this manually:
# Mail.register_interceptor(SanitizeEmail::Bleach)
# Once registered, SanitizeEmail needs to be engaged:
# # in config/initializers/sanitize_email.rb
# SanitizeEmail::Config.configure {|config| config[:engage] = true }
# 3. SanitizeEmail::Config.configure {|config| config[:activation_proc] = Proc.new { true } } # by default it is false
# If you don't need to compute anything, then don't use the Proc, go with the next option.
Expand All @@ -49,7 +46,7 @@ def delivering_email(message)
# Note: Number 2 See note accompanying 2: you may need to setup your own register_interceptor
#
# If installed but not configured, sanitize_email DOES NOTHING. Until configured the defaults leave it turned off.
def sanitize_engaged?(message)
def self.sanitize_engaged?(message)

# Don't sanitize the message if it will not be delivered
return false unless message.perform_deliveries
Expand All @@ -58,10 +55,6 @@ def sanitize_engaged?(message)
forced = SanitizeEmail.force_sanitize
return forced unless forced.nil?

# Is this particular instance of Bleach engaged
engaged = self.engage
return engaged unless engaged.nil?

# Should we sanitize due to the activation_proc?
return SanitizeEmail.activate?(message)

Expand Down
2 changes: 1 addition & 1 deletion lib/sanitize_email/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module SanitizeEmail
class Engine < ::Rails::Engine

config.to_prepare do
ActionMailer::Base.register_interceptor(SanitizeEmail::Bleach.new)
ActionMailer::Base.register_interceptor(SanitizeEmail::Bleach)
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/sanitize_email/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# Released under the MIT license

module SanitizeEmail
# For Rails 3.0, which didn't yet support Engines
# For Rails 3.0, which didn't yet support Engines
class Railtie < ::Rails::Railtie

config.after_initialize do
ActionMailer::Base.register_interceptor(SanitizeEmail::Bleach.new)
ActionMailer::Base.register_interceptor(SanitizeEmail::Bleach)
end

end
Expand Down
4 changes: 2 additions & 2 deletions spec/sanitize_email_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def configure_sanitize_email(sanitize_hash = {})
config[:sanitized_recipients] = options[:sanitized_recipients] if options[:sanitized_recipients]
config[:force_sanitize] = options[:force_sanitize] unless options[:force_sanitize].nil?
end
Mail.register_interceptor(SanitizeEmail::Bleach.new)
Mail.register_interceptor(SanitizeEmail::Bleach)
end

def funky_config
Expand All @@ -70,7 +70,7 @@ def funky_config
config[:use_actual_environment_prepended_to_subject] = true
config[:use_actual_email_as_sanitized_user_name] = false
end
Mail.register_interceptor(SanitizeEmail::Bleach.new)
Mail.register_interceptor(SanitizeEmail::Bleach)
end

def sanitary_mail_delivery(config_options = {})
Expand Down

0 comments on commit 31822a8

Please sign in to comment.