summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrendan G. Lim2010-08-28 23:43:35 -0500
committerBrendan G. Lim2010-08-28 23:43:35 -0500
commit52beb1ab47be6b5db42e4b420ed409f54e082b3a (patch)
treeae22354b1ed5b54e908efa64102de0970f8a2f6e
parent9dbac74702a33b844ad71c8353207b2f2767de71 (diff)
downloadsms-fu-52beb1ab47be6b5db42e4b420ed409f54e082b3a.tar.bz2
Now can use ActionMailer or Pony for delivery. Refactoring as well.
-rw-r--r--MIT-LICENSE2
-rw-r--r--README.rdoc59
-rw-r--r--Rakefile13
-rw-r--r--lib/sms_fu.rb8
-rw-r--r--lib/sms_fu/sms_fu.rb139
-rw-r--r--lib/sms_fu/sms_fu_helper.rb4
-rw-r--r--lib/sms_fu/sms_notifier.rb46
-rw-r--r--sms_fu.gemspec13
8 files changed, 145 insertions, 139 deletions
diff --git a/MIT-LICENSE b/MIT-LICENSE
index 9fcf9e2..35d2491 100644
--- a/MIT-LICENSE
+++ b/MIT-LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2008 Brendan G. Lim
+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
diff --git a/README.rdoc b/README.rdoc
index 0d1b92a..db4e958 100644
--- a/README.rdoc
+++ b/README.rdoc
@@ -25,20 +25,20 @@ etc.
== Setup Instructions
-Install sms_fu gem
+* Install sms_fu gem
sudo gem install sms_fu
-Add sms_fu.yml to your config folder (Rails only)
+* Make sure you have the 'action_mailer' (>= 3.0.0rc2) or 'pony' (>= 1.0) gem installed.
- http://github.com/brendanlim/sms-fu/blob/master/templates/sms_fu.yml
+* Add sms_fu.yml to your config folder (Rails only)
-Modify sms_fu.yml in your config folder with your reply-to e-mail address.
+ http://github.com/brendanlim/sms-fu/blob/master/templates/sms_fu.yml
-That's it! Now you're good to go.
+* (Optional) Modify sms_fu.yml in your config folder with your reply-to e-mail address.
== Example Usage
-
+
* You have to send in the phone number, without any non-numeric characters. The
phone numbers must be 10 digits in length.
* The two required parameters are the phone number and the phone carrier.
@@ -54,25 +54,60 @@ That's it! Now you're good to go.
Verizon Wireless => "verizon"
Vodafone Tokyo => "vodafone-jp-tokyo"
-* <b>Check sms_fu.yml for a complete list of supported carriers, including international
- carriers as well.</b>
+* Check sms_fu.yml for a complete list of supported carriers, including international
+ carriers as well.
+
+* Configure and setup SMS Fu
+
+SMS Fu relies on either ActionMailer or Pony for delivery. You can now specify
+which you'd like to use for delivery.
- SMSFu.deliver("5558675309","at&t","message")
+You can use your ActionMailer settings by just specifying the :delivery option as
+:action_mailer.
+
+If you would like to use Pony, you can configure it to use :sendmail or :smtp via
+Pony. Set the :delivery option to :pony and then make sure to specify the
+:pony_options as well, if necessary.
+
+ sms_fu = SMSFu.configure(:delivery => :action_mailer)
+
+ -- or --
+
+ sms_fu = SMSFu.configure(:delivery => :pony, :pony_options => { :via => :sendmail })
+
+ -- or --
+
+ sms_fu = SMSFu.configure(:delivery => :pony,
+ :pony_options => {
+ :via => :smtp,
+ :via_options => {
+ :address => 'smtp.gmail.com',
+ :port => '587',
+ :user_name => 'username',
+ :password => 'password',
+ :authentication => :plain,
+ :domain => "localhost.localdomain"
+ :enable_starttls_auto => true,
+ }})
+
+You can view more pony 'via_options' at http://github.com/benprew/pony
+
+ sms_fu.deliver("5558675309","at&t","message")
* If you want to set a custom from e-mail per SMS message, you can do so
by doing the following.
- SMSFu.deliver("5558675309","at&t","message", :from => "bob@test.com")
+ sms_fu.deliver("5558675309","at&t","message", :from => "bob@test.com")
* You can set the maximum length of the SMS message, which is not set by
default. Most phones can only accept 128 characters. To do this just
specify the limit option.
- SMSFu.deliver("5558675309","at&t","message", :limit => 128)
+ sms_fu.deliver("5558675309","at&t","message", :limit => 128)
* You can retrieve just the formatted address to use in your own mailer.
- SMSFu.sms_address("5558675309","at&t") # => "5558675309@txt.att.net"
+ sms_fu.sms_address("5558675309","at&t") # => "5558675309@txt.att.net"
== View Helpers (Rails)
diff --git a/Rakefile b/Rakefile
index 050e43a..86d5669 100644
--- a/Rakefile
+++ b/Rakefile
@@ -21,16 +21,3 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('lib/**/*.rb')
end
-begin
- require 'jeweler'
- Jeweler::Tasks.new do |gemspec|
- gemspec.name = "sms-fu"
- gemspec.summary = "SMS Fu allows ou to send a text-message for free in the form of an e-mail to a mobile recipient."
- gemspec.description = "SMS Fu allows ou to send a text-message for free in the form of an e-mail to a mobile recipient."
- gemspec.email = "brendangl@gmail.com"
- gemspec.homepage = "http://github.com/brendanlim/sms-fu"
- gemspec.authors = ["Brendan G. Lim"]
- end
-rescue LoadError
- puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
-end
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
diff --git a/sms_fu.gemspec b/sms_fu.gemspec
index f3847c0..d24e0b4 100644
--- a/sms_fu.gemspec
+++ b/sms_fu.gemspec
@@ -2,7 +2,7 @@
Gem::Specification.new do |s|
s.name = %q{sms_fu}
- s.version = "1.0.3"
+ s.version = "1.1.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Brendan G. Lim"]
@@ -17,8 +17,6 @@ Gem::Specification.new do |s|
"MIT-LICENSE",
"README.rdoc",
"Rakefile",
- "VERSION",
- "init.rb",
"install.rb",
"lib/sms_fu.rb",
"lib/sms_fu/sms_fu.rb",
@@ -28,7 +26,6 @@ Gem::Specification.new do |s|
"tasks/sms_fu_tasks.rake",
"templates/sms_fu.yml",
"test/sms_fu_test.rb",
- "uninstall.rb",
"views/sms_notifier/sms_message.html.erb"
]
s.has_rdoc = true
@@ -43,11 +40,17 @@ Gem::Specification.new do |s|
if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
- s.specification_version = 2
+ s.specification_version = 3
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_development_dependency(%q<action_mailer>, [">= 3.0.0.rc2"])
+ s.add_development_dependency(%q<pony>, [">= 1.0"])
else
+ s.add_dependency(%q<action_mailer>, [">= 3.0.0.rc2"])
+ s.add_dependency(%q<pony>, [">= 1.0"])
end
else
+ s.add_dependency(%q<action_mailer>, [">= 3.0.0.rc2"])
+ s.add_dependency(%q<pony>, [">= 1.0"])
end
end