require 'forwardable' require 'facets/module/cattr' require 'class_attribute' module ActionTexter # This module handles everything related to mail delivery, from registering new # delivery methods to configuring the mail object to be sent. module DeliveryMethods def self.included(base) base.extend ClassMethods base.include ClassAttribute base.class_eval do # Do not make this inheritable, because we always want it to propagate cattr_accessor :raise_delivery_errors self.raise_delivery_errors = true cattr_accessor :perform_deliveries self.perform_deliveries = true self.delivery_methods = {}.freeze # add_delivery_method :file, ActionTexter::FileDelivery, #:location => defined?(Rails.root) ? "#{Rails.root}/tmp/texts" : "#{Dir.tmpdir}/texts" add_delivery_method :test, ActionTexter::Provider::TestMessage::Delivery add_delivery_method :messagebird, ActionTexter::Provider::Messagebird::Delivery, { endpoint: 'https://rest.messagebird.com', path: '/messages' } add_delivery_method :mail, ActionTexter::Provider::Mail::Delivery, { charset: 'utf8' } # add_delivery_method :twilio, ActionTexter::TwilioDelivery # add_delivery_method :whitelist_proxy, ActionTexter::WhitelistProxyDelivery end end module ClassMethods extend Forwardable cattr_accessor :delivery_methods, :delivery_method # attr_accessor # Provides a list of SMS messages that have been delivered by ActionTexter::TestDelivery delegate deliveries: ActionTexter::Provider::TestMessage::Delivery delegate :deliveries= => ActionTexter::Provider::TestMessage::Delivery # Adds a new delivery method through the given class using the given symbol # as alias and the default options supplied: # # Example: # # add_delivery_method :sendmail, Mail::Sendmail, # :location => '/usr/sbin/sendmail', # :arguments => '-i -t' # def add_delivery_method(symbol, klass, default_options = {}) class_attribute(:"#{symbol}_settings") unless respond_to?(:"#{symbol}_settings") send(:"#{symbol}_settings=", default_options) self.delivery_methods = delivery_methods.merge(symbol.to_sym => klass).freeze end def wrap_delivery_behavior(message, method = nil) #:nodoc: method ||= delivery_method message.delivery_handler = self case method when NilClass fail 'Delivery method cannot be nil' when Symbol if klass = delivery_methods[method.to_sym] message.delivery_method(klass, send(:"#{method}_settings")) else fail "Invalid delivery method #{method.inspect}" end else message.delivery_method(method) end message.perform_deliveries = perform_deliveries message.raise_delivery_errors = raise_delivery_errors end end def wrap_delivery_behavior!(*args) #:nodoc: self.class.wrap_delivery_behavior(*args) end end end