class SmsCarrier::Base
Constants
- PROTECTED_IVARS
Attributes
Allows to set the name of current carrier.
Public Class Methods
Returns the name of current carrier. If this is an anonymous carrier, this method will return anonymous
instead.
# File lib/sms_carrier/base.rb, line 77 def carrier_name @carrier_name ||= anonymous? ? "anonymous" : name.underscore end
Sets the defaults through app configuration:
config.sms_carrier.default(from: "+886987654321")
Aliased by ::default_options=
# File lib/sms_carrier/base.rb, line 89 def default(value = nil) self.default_params = default_params.merge(value).freeze if value default_params end
Allows to set defaults through app configuration:
config.sms_carrier.default_options = { from: "+886987654321" }
Instantiate a new carrier object. If method_name
is not nil
, the carrier will be initialized according to the named method. If not, the carrier will remain uninitialized (useful when you only need to invoke the “receive” method, for instance).
# File lib/sms_carrier/base.rb, line 139 def initialize(method_name=nil, *args) super() @_sms_was_called = false @_message = Sms.new process(method_name, *args) if method_name end
Register an Interceptor which will be called before SMS is sent. Either a class, string or symbol can be passed in as the Interceptor. If a string or symbol is passed in it will be camelized and constantized.
# File lib/sms_carrier/base.rb, line 64 def register_interceptor(interceptor) delivery_interceptor = case interceptor when String, Symbol interceptor.to_s.camelize.constantize else interceptor end Sms.register_interceptor(delivery_interceptor) end
Register one or more Interceptors which will be called before SMS is sent.
# File lib/sms_carrier/base.rb, line 43 def register_interceptors(*interceptors) interceptors.flatten.compact.each { |interceptor| register_interceptor(interceptor) } end
Register an Observer which will be notified when SMS is delivered. Either a class, string or symbol can be passed in as the Observer. If a string or symbol is passed in it will be camelized and constantized.
# File lib/sms_carrier/base.rb, line 50 def register_observer(observer) delivery_observer = case observer when String, Symbol observer.to_s.camelize.constantize else observer end Sms.register_observer(delivery_observer) end
Register one or more Observers which will be notified when SMS is delivered.
# File lib/sms_carrier/base.rb, line 38 def register_observers(*observers) observers.flatten.compact.each { |observer| register_observer(observer) } end
SMS do not support relative path links.
# File lib/sms_carrier/base.rb, line 283 def self.supports_path? false end
Public Instance Methods
Returns the name of the carrier object.
# File lib/sms_carrier/base.rb, line 171 def carrier_name self.class.carrier_name end
Allows you to pass random and unusual options to the new SmsCarrier::Sms
object which will add them to itself.
options['X-Special-Domain-Specific-Option'] = "SecretValue"
The resulting SmsCarrier::Sms
will have the following in its option:
X-Special-Domain-Specific-Option: SecretValue
def options
@_message.options
end
# File lib/sms_carrier/base.rb, line 187 def options(args = nil) if args @_message.options.merge!(args) else @_message end end
The main method that creates the message and renders the SMS templates. There are two ways to call this method, with a block, or without a block.
It accepts a headers hash. This hash allows you to specify the most used headers in an SMS message, these are:
-
:to
- Who the message is destined for, can be a string of addresses, or an array of addresses. -
:from
- Who the message is from
You can set default values for any of the above headers (except :date
) by using the ::default
class method:
class Notifier < SmsCarrier::Base default from: '+886987654321' end
If you do not pass a block to the sms
method, it will find all templates in the view paths using by default the carrier name and the method name that it is being called from, it will then create parts for each of these templates intelligently, making educated guesses on correct content type and sequence, and return a fully prepared Sms
ready to call :deliver
on to send.
For example:
class Notifier < SmsCarrier::Base default from: 'no-reply@test.lindsaar.net' def welcome sms(to: 'mikel@test.lindsaar.net') end end
Will look for all templates at “app/views/notifier” with name “welcome”. If no welcome template exists, it will raise an ActionView::MissingTemplate error.
However, those can be customized:
sms(template_path: 'notifications', template_name: 'another')
And now it will look for all templates at “app/views/notifications” with name “another”.
You can even render plain text directly without using a template:
sms(to: '+886987654321', body: 'Hello Mikel!')
# File lib/sms_carrier/base.rb, line 242 def sms(options = {}) return @_message if @_sms_was_called && options.blank? m = @_message # Call all the procs (if any) default_values = {} self.class.default.each do |k,v| default_values[k] = v.is_a?(Proc) ? instance_eval(&v) : v end # Handle defaults options = options.reverse_merge(default_values) # Set configure delivery behavior wrap_delivery_behavior!(options.delete(:delivery_method), options.delete(:delivery_method_options)) # Assign all options except body, template_name, and template_path assignable = options.except(:body, :template_name, :template_path) assignable.each { |k, v| m[k] = v } # Render the templates and blocks m.body = response(options) @_sms_was_called = true m end