diff options
| author | Brendan G. Lim | 2010-08-28 23:43:35 -0500 | 
|---|---|---|
| committer | Brendan G. Lim | 2010-08-28 23:43:35 -0500 | 
| commit | 52beb1ab47be6b5db42e4b420ed409f54e082b3a (patch) | |
| tree | ae22354b1ed5b54e908efa64102de0970f8a2f6e /lib | |
| parent | 9dbac74702a33b844ad71c8353207b2f2767de71 (diff) | |
| download | sms-fu-52beb1ab47be6b5db42e4b420ed409f54e082b3a.tar.bz2 | |
Now can use ActionMailer or Pony for delivery.  Refactoring as well.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/sms_fu.rb | 8 | ||||
| -rw-r--r-- | lib/sms_fu/sms_fu.rb | 139 | ||||
| -rw-r--r-- | lib/sms_fu/sms_fu_helper.rb | 4 | ||||
| -rw-r--r-- | lib/sms_fu/sms_notifier.rb | 46 | 
4 files changed, 89 insertions, 108 deletions
| diff --git a/lib/sms_fu.rb b/lib/sms_fu.rb index 38ca789..95c9659 100644 --- a/lib/sms_fu.rb +++ b/lib/sms_fu.rb @@ -1,5 +1,7 @@ +require 'action_mailer' +require 'pony' +require 'yaml' +  require 'sms_fu/sms_fu' -require 'sms_fu/sms_notifier'  require 'sms_fu/sms_fu_helper' -require 'action_mailer' -require 'yaml'
\ No newline at end of file +require 'sms_fu/sms_notifier'
\ No newline at end of file diff --git a/lib/sms_fu/sms_fu.rb b/lib/sms_fu/sms_fu.rb index 78f56d2..bda61af 100644 --- a/lib/sms_fu/sms_fu.rb +++ b/lib/sms_fu/sms_fu.rb @@ -1,77 +1,88 @@ -# Copyright (c) 2008-2010 Brendan G. Lim (brendan@intridea.com) -#  -# Permission is hereby granted, free of charge, to any person obtaining -# a copy of this software and associated documentation files (the -# "Software"), to deal in the Software without restriction, including -# without limitation the rights to use, copy, modify, merge, publish, -# distribute, sublicense, and/or sell copies of the Software, and to -# permit persons to whom the Software is furnished to do so, subject to -# the following conditions: -#  -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. -#  -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -module SMSFu -  RAILS_CONFIG_ROOT = defined?(Rails) ? -    (Rails.env == 'test' ? "#{File.dirname(__FILE__)}/../templates" : "#{RAILS_ROOT}/config") : -    (defined?(RAILS_ENV) ? (RAILS_ENV == 'test' ? "#{File.dirname(__FILE__)}/../templates" : "#{RAILS_ROOT}/config") :  -      "#{File.dirname(__FILE__)}/../templates") -             -  @config     ||= YAML::load(File.open("#{RAILS_CONFIG_ROOT}/sms_fu.yml")) -  @@carriers  ||= @config['carriers']  -  @@from_address = @config['config']['from_address'] +class SMSFu +  DELIVERY_OPTIONS = [:action_mailer, :pony] -  class << self -    def carrier_name(key) -      carriers[key]['name'] -    end +  def self.configure(options = {}) +    new(options) +  end -    def carriers -      @@carriers.dup +  def initialize(options = {}) +    raise_exception("Delivery options can only be: '#{DELIVERY_OPTIONS.join(", ")}'") unless DELIVERY_OPTIONS.include?(options[:delivery]) +    if options[:delivery] == :pony && (options[:pony_options][:via].to_sym != :sendmail && !options[:pony_options].has_key?(:via_options)) +      raise_exception("Missing Pony configuration options")       end +     +    @@mail_config = { :delivery => options[:delivery].to_sym, :pony_options => options[:pony_options] } +    @@yaml_config = YAML::load(options[:yaml_config] ? options[:yaml_config] : File.open("#{template_directory}/sms_fu.yml")) +  end +   +  def from_address +    @@yaml_config['config']['from_address'] +  end -    def deliver(number,carrier,message,options={}) -      raise SMSFuException.new("Can't deliver blank message to #{format_number(number)}") if message.nil? or message.empty? -      options[:limit] ||= message.length -      options[:from]  ||= @@from_address -      message = message[0..options[:limit]-1] -      sms_email = sms_email(format_number(number),carrier) +  def carriers +    @@yaml_config['carriers']  +  end -      SmsNotifier.deliver_sms_message(sms_email,message,options[:from]) -    rescue SMSFuException => exception -      raise exception -    end +  def carrier_name(key) +    carrier(key)['name'] +  end +   +  def carrier_email(key) +    carrier(key.downcase)['value'] +  end +   +  def carrier(key) +    raise_exception("Carrier (#{key}) is not supported") unless carriers.has_key?(key.downcase) +    carriers[key] +  end -    def sms_address(number,carrier) -      number = format_number(number) -      sms_email(number,carrier) +  def deliver(number, carrier, message, options = {}) +    raise_exception("Can't deliver blank message to #{format_number(number)}") if message.nil? or message.empty? +    limit   = options[:limit] || message.length +    from    = options[:from] || from_address +    message = message[0..limit-1] +    email   = sms_address(number,carrier) +   +    if @@mail_config[:delivery] == :pony +      Pony.mail(:to => email,  +        :body => message,  +        :from => from,  +        :via => @@mail_config[:pony_options][:via], +        :via_options => @@mail_config[:pony_options][:via_options]) +    else +      SMSNotifier.send_sms(email, message, from)      end +  end -    private +  def sms_address(number,carrier) +    format_number(number) + carrier_email(carrier.downcase) +  end -    def format_number(number) -      pre_formatted = number.gsub("-","").strip -      formatted =  (pre_formatted.length == 11 && pre_formatted[0,1] == "1") ? pre_formatted[1..pre_formatted.length] : pre_formatted -      return is_valid?(formatted) ? formatted : (raise SMSFuException.new("Phone number (#{number}) is not formatted correctly")) -    end +  protected -    def is_valid?(number) -      number.length >= 10 && number[/^.\d+$/] -    end   +  def format_number(number) +    stripped = number.gsub("-","").strip +    formatted = (stripped.length == 11 && stripped[0,1] == "1") ? stripped[1..stripped.length] : stripped +    raise_exception("Number (#{number}) is not formatted correctly") unless valid_number?(formatted) +    formatted +  end -    def sms_email(phone_number, carrier) -      raise SMSFuException.new("Specified carrier, #{carrier} is not supported.") unless @@carriers.has_key?(carrier.downcase) -      "#{phone_number}#{@@carriers[carrier.downcase]['value']}" -    end  +  def valid_number?(number) +    number.length >= 10 && number[/^.\d+$/] +  end   + +  def template_directory +    directory = defined?(Rails) ? "#{RAILS_ROOT}/config" : "#{File.dirname(__FILE__)}/../../templates" +    if (defined?(Rails) && Rails.env == 'test') || (defined?(RAILS_ENV) && RAILS_ENV == 'test)') +      "#{File.dirname(__FILE__)}/../../templates" +    else +      directory +    end +  end +   +  def raise_exception(message) +    raise SMSFuException.new(message)    end -    -  class SMSFuException < StandardError; end  end + +class SMSFuException < StandardError; end
\ No newline at end of file diff --git a/lib/sms_fu/sms_fu_helper.rb b/lib/sms_fu/sms_fu_helper.rb index 51b337e..39376fb 100644 --- a/lib/sms_fu/sms_fu_helper.rb +++ b/lib/sms_fu/sms_fu_helper.rb @@ -1,12 +1,12 @@  module SMSFuHelper      # Returns a collection of carriers to be used in your own select tag -  # e.g., <%= f.select :mobile_carrier, carrier_collection %> +  #   e.g., <%= f.select :mobile_carrier, carrier_collection %>    def carrier_collection      SMSFu.carriers.sort.collect{ |carrier| [carrier[1]["name"], carrier[0]] }    end    # Returns a formatted select box filled with carriers -  # e.g., <%= carrier_select %> +  #   e.g., <%= carrier_select %>    # - name => name of the method in which you want to store the carrier name    # - phrase => default selected blank option in select box     # - selected => carrier to pre-select diff --git a/lib/sms_fu/sms_notifier.rb b/lib/sms_fu/sms_notifier.rb index 7b5d1e8..be312ff 100644 --- a/lib/sms_fu/sms_notifier.rb +++ b/lib/sms_fu/sms_notifier.rb @@ -1,40 +1,8 @@ -require 'yaml' -# Copyright (c) 2008 Brendan G. Lim (brendangl@gmail.com) -#  -# Permission is hereby granted, free of charge, to any person obtaining -# a copy of this software and associated documentation files (the -# "Software"), to deal in the Software without restriction, including -# without limitation the rights to use, copy, modify, merge, publish, -# distribute, sublicense, and/or sell copies of the Software, and to -# permit persons to whom the Software is furnished to do so, subject to -# the following conditions: -#  -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. -#  -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -class SmsNotifier < ActionMailer::Base -  def sms_message(recipient, message, sender_email) -    content_type      "text/plain" -    recipients        recipient -    from              sender_email -    body['message'] = message -  end - -  view_path = File.join(File.dirname(__FILE__), '..', 'views') -  if public_methods.include?('append_view_path') -    self.append_view_path view_path -  elsif public_methods.include?("view_paths") -    self.view_paths << view_path -  else -    self.template_root = view_path -  end -   +class SMSNotifier < ActionMailer::Base +  def send_sms(recipient, message, sender_email) +    mail(:to => recipient, :from => sender_email) do |format| +      format.text { render :text => message } +      format.html { render :text => message } +    end +  end    end | 
