demarches-normaliennes/lib/action_mailer/dynamic_smtp_settings_interceptor.rb
Pierre de La Morinerie 04cfc8ed9d config: use alternate delivery methods to configure ActionMailer
Previously `SENDINBLUE_BALANCING` was used only when
`SENDINBLUE_ENABLED` was *disabled* (otherwise only SendInBlue was ever
used).

This commit:

- Ensure that `SENDINBLUE_BALANCING` is used only when SendInBlue is
  *enabled* (which is more intuitive).
- Make it easier to add other delivery methods.
2022-02-08 12:44:43 +01:00

26 lines
708 B
Ruby

# Note: this class is instanciated when being added as an interceptor
# during the app initialization.
#
# If you edit this file in development env, you will need to restart
# the app to see the changes.
class DynamicSmtpSettingsInterceptor
def self.delivering_email(message)
if balance_to_sendinblue?
ApplicationMailer.wrap_delivery_behavior(message, :sendinblue)
end
# fallback to the default delivery method
end
private
def self.balance_to_sendinblue?
if ENV.fetch('SENDINBLUE_ENABLED') != 'enabled'
false
elsif ENV.fetch('SENDINBLUE_BALANCING') == 'enabled'
rand(0..99) < ENV.fetch('SENDINBLUE_BALANCING_VALUE').to_i
else
true
end
end
end