module SmsSafe

Constants

VERSION

Public Class Methods

configuration() click to toggle source

Returns the current configuration

# File lib/sms_safe/config.rb, line 7
def self.configuration
  @configuration ||=  Configuration.new
end
configure() { |configuration| ... } click to toggle source

Yields the current configuration, allowing the caller to modify it in a block

# File lib/sms_safe/config.rb, line 12
def self.configure
  yield(configuration) if block_given?
end
hook!(texter_gem) click to toggle source

Hooks into the specified texter gem to be able to analyze and modify / discard messages before they are sent.

Uses a civilized method for :action_texter, and monkey_patching for the rest, unfortunately

@param [Symbol] texter_gem the gem that is being used for sending messages.

Can be :action_texter, :twilio, or :nexmo

@return Nothing useful

# File lib/sms_safe/hooks.rb, line 9
def self.hook!(texter_gem)
  case texter_gem
    when :action_texter
      ActionTexter.register_interceptor(SmsSafe::Interceptors::ActionTexter.new)
    when :twilio
      hook_twilio!
    when :nexmo
      hook_nexmo!
    else
      raise InvalidConfigSettingError.new("Ensure texter_gem is either :action_texter, :twilio or :nexmo")
  end
end

Private Class Methods

hook_nexmo!() click to toggle source

Hooks into Nexmo’s message sending method to allows us to intercept.

Uses monkeypatching, unfortunately.
# File lib/sms_safe/hooks.rb, line 26
def self.hook_nexmo!
  Nexmo::Client.class_eval do
    alias_method :sms_safe_original_send_message, :send_message

    def send_message(params)
      interceptor = SmsSafe::Interceptors::Nexmo.new
      new_message = interceptor.process_message(params)
      if new_message.nil?
        return new_message
      else
        return sms_safe_original_send_message(new_message)
      end
    end
  end
end
hook_twilio!() click to toggle source

Hooks into Twilio’s message sending method to allows us to intercept.

Uses monkeypatching, unfortunately.
# File lib/sms_safe/hooks.rb, line 44
def self.hook_twilio!
  Twilio::REST::Messages.class_eval do
    # There is no method to alias, the gem relies on method_missing on a base class...
    #alias_method :sms_safe_original_send_message, :send_message

    def create(params)
      interceptor = SmsSafe::Interceptors::Twilio.new
      new_message = interceptor.process_message(params)
      if new_message.nil?
        return new_message
      else
        return super(new_message)
      end
    end
  end
end

Private Instance Methods

create(params) click to toggle source
There is no method to alias, the gem relies on method_missing on a base class...

alias_method :sms_safe_original_send_message, :send_message

Calls superclass method
# File lib/sms_safe/hooks.rb, line 49
def create(params)
  interceptor = SmsSafe::Interceptors::Twilio.new
  new_message = interceptor.process_message(params)
  if new_message.nil?
    return new_message
  else
    return super(new_message)
  end
end
send_message(params) click to toggle source
# File lib/sms_safe/hooks.rb, line 30
def send_message(params)
  interceptor = SmsSafe::Interceptors::Nexmo.new
  new_message = interceptor.process_message(params)
  if new_message.nil?
    return new_message
  else
    return sms_safe_original_send_message(new_message)
  end
end