From aba05f07d765306b5bd5f6c403a318092a437c0b Mon Sep 17 00:00:00 2001 From: Brendan G. Lim Date: Sun, 30 Mar 2008 16:10:05 -0400 Subject: Initial import --- MIT-LICENSE | 20 ++++++++++ README | 56 +++++++++++++++++++++++++++ Rakefile | 22 +++++++++++ init.rb | 3 ++ install.rb | 4 ++ lib/sms_fu.rb | 68 +++++++++++++++++++++++++++++++++ lib/sms_notifier.rb | 40 +++++++++++++++++++ tasks/sms_fu_tasks.rake | 0 templates/sms_fu.yml | 1 + test/sms_fu_test.rb | 21 ++++++++++ uninstall.rb | 1 + views/sms_notifier/sms_message.html.erb | 1 + 12 files changed, 237 insertions(+) create mode 100644 MIT-LICENSE create mode 100644 README create mode 100644 Rakefile create mode 100644 init.rb create mode 100644 install.rb create mode 100644 lib/sms_fu.rb create mode 100644 lib/sms_notifier.rb create mode 100644 tasks/sms_fu_tasks.rake create mode 100644 templates/sms_fu.yml create mode 100644 test/sms_fu_test.rb create mode 100644 uninstall.rb create mode 100644 views/sms_notifier/sms_message.html.erb diff --git a/MIT-LICENSE b/MIT-LICENSE new file mode 100644 index 0000000..9fcf9e2 --- /dev/null +++ b/MIT-LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2008 Brendan G. Lim + +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. diff --git a/README b/README new file mode 100644 index 0000000..1a7ab59 --- /dev/null +++ b/README @@ -0,0 +1,56 @@ +SMS Fu (0.2) +------------- + + Allows you to send an SMS in the form of an e-mail to a cell phone. Only + numbers in the United States are supported. + + Supported Carriers: + Alltel, AT&T, Boost Mobile, Sprint, T-Mobile, Virgin Mobile, Verizon Wireless + +Setup Instructions +------------------ + + **** IMPORTANT **** + + 1) Add this this one line to the controller you want to be able to use SMSFu in. + + class ExampleController < ApplicationController + include SMSFu + end + + 2) Modify sms_fu.yml in your config folder with your reply-to e-mail address. + + That's it! Now you're good to go. + +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. + * Here are the carrier values: + + Alltel Wireless => "Alltel" + AT&T/Cingular => "AT&T" + Boost Mobile => "Boost" + Sprint Wireless => "Sprint" + T-Mobile => "T-Mobile" + Virgin Mobile => "Virgin" + Verizon Wireless => "Verizon" + + deliver_sms("5558675309","AT&T","your message here") + + * 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. + + deliver_sms("5558675309","AT&T","your message here", :limit => 128) + + * You can retrieve just the formatted address to use in your own mailer. + + get_sms_address("5558675309","AT&T") + + This returns "5558675309@txt.att.net" + + +Copyright (c) 2008 Brendan G. Lim, released under the MIT license diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..de861dd --- /dev/null +++ b/Rakefile @@ -0,0 +1,22 @@ +require 'rake' +require 'rake/testtask' +require 'rake/rdoctask' + +desc 'Default: run unit tests.' +task :default => :test + +desc 'Test the sms_fu plugin.' +Rake::TestTask.new(:test) do |t| + t.libs << 'lib' + t.pattern = 'test/**/*_test.rb' + t.verbose = true +end + +desc 'Generate documentation for the sms_fu plugin.' +Rake::RDocTask.new(:rdoc) do |rdoc| + rdoc.rdoc_dir = 'rdoc' + rdoc.title = 'SmsFu' + rdoc.options << '--line-numbers' << '--inline-source' + rdoc.rdoc_files.include('README') + rdoc.rdoc_files.include('lib/**/*.rb') +end diff --git a/init.rb b/init.rb new file mode 100644 index 0000000..c6df249 --- /dev/null +++ b/init.rb @@ -0,0 +1,3 @@ +require "action_mailer" +require File.dirname(__FILE__) + '/lib/sms_fu' +ActionView::Base.send(:include, SMSFu) \ No newline at end of file diff --git a/install.rb b/install.rb new file mode 100644 index 0000000..20fd849 --- /dev/null +++ b/install.rb @@ -0,0 +1,4 @@ +require 'ftools' +require 'fileutils' +puts IO.read(File.join(File.dirname(__FILE__), 'README')) +File.copy(File.dirname(__FILE__)+'/templates/sms_fu.yml', File.dirname(__FILE__)+'/../../../config') \ No newline at end of file diff --git a/lib/sms_fu.rb b/lib/sms_fu.rb new file mode 100644 index 0000000..86810ed --- /dev/null +++ b/lib/sms_fu.rb @@ -0,0 +1,68 @@ +# 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. + +module SMSFu + class SMSFuException < StandardError; end + + def deliver_sms(number,carrier,message,options={}) + number = format_number(number) + raise SMSFuException.new("Cannot deliver an empty message to #{number}") if message.nil? or message.empty? + + options[:limit] ||= message.length + message = message[0..options[:limit]-1] + sms_email = determine_sms_email(format_number(number),carrier) + SmsNotifier.deliver_sms_message(sms_email,message) + + rescue SMSFuException => exception + raise exception + end + + def get_sms_address(number,carrier) + number = format_number(number) + determine_sms_email(number,carrier) + end + + private + + def format_number(number) + pre_formatted = number.gsub("-","").strip + formatted = (pre_formatted.length == 11) ? pre_formatted[1..pre_formatted.length] : pre_formatted + + return is_valid?(formatted) ? formatted : (raise SMSFuException.new("Phone number (#{number}) is not formatted correctly")) + end + + def is_valid?(number) + return (number.length == 10 && number[/^\d+$/]) ? true : false + end + + def determine_sms_email(phone_number, phone_carrier) + case phone_carrier.downcase + when "alltel": "#{phone_number}@message.alltell.com" + when "at&t": "#{phone_number}@txt.att.net" + when "boost": "#{phone_number}@myboostmobile.com" + when "sprint": "#{phone_number}@messaging.sprintpcs.com" + when "t-mobile": "#{phone_number}@tmomail.net" + when "virgin": "#{phone_number}@vmobl.net" + when "verizon": "#{phone_number}@vtext.com" + else raise SMSFuException.new("Specified carrier, #{phone_carrier} is not supported.") + end + end +end \ No newline at end of file diff --git a/lib/sms_notifier.rb b/lib/sms_notifier.rb new file mode 100644 index 0000000..127f35b --- /dev/null +++ b/lib/sms_notifier.rb @@ -0,0 +1,40 @@ +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 + @config = YAML::load(File.open("#{RAILS_ROOT}/config/sms_fu.yml")) + @@from_address = @config['from_address'] + cattr_accessor :from_address + + def sms_message(recipient, message) + content_type "text/plain" + recipients recipient + from from_address + + body['message'] = message + end + + def template_root + "#{File.dirname(__FILE__)}/../views" + end + +end \ No newline at end of file diff --git a/tasks/sms_fu_tasks.rake b/tasks/sms_fu_tasks.rake new file mode 100644 index 0000000..e69de29 diff --git a/templates/sms_fu.yml b/templates/sms_fu.yml new file mode 100644 index 0000000..0940493 --- /dev/null +++ b/templates/sms_fu.yml @@ -0,0 +1 @@ +from_address: noreply@domain.com \ No newline at end of file diff --git a/test/sms_fu_test.rb b/test/sms_fu_test.rb new file mode 100644 index 0000000..1b783ee --- /dev/null +++ b/test/sms_fu_test.rb @@ -0,0 +1,21 @@ +require 'test/unit' +require 'sms_fu' +require 'sms_notifier' +require 'action_mailer' + +class SmsFuTest < Test::Unit::TestCase + include SMSFu + + def test_validity_of_number + assert_raise(SMSFuException) { deliver_sms("123456789011","AT&T","Message") } + assert_equal("5555555555@txt.att.net", get_sms_address("1-555-555-5555","AT&T")) + end + + def test_handling_of_blank_message + assert_raise(SMSFuException) { deliver_sms("1234567890","AT&T","") } + end + + def test_get_sms_address + assert_equal("1234567890@txt.att.net", get_sms_address("1234567890","AT&T")) + end +end diff --git a/uninstall.rb b/uninstall.rb new file mode 100644 index 0000000..9738333 --- /dev/null +++ b/uninstall.rb @@ -0,0 +1 @@ +# Uninstall hook code here diff --git a/views/sms_notifier/sms_message.html.erb b/views/sms_notifier/sms_message.html.erb new file mode 100644 index 0000000..17a3d7c --- /dev/null +++ b/views/sms_notifier/sms_message.html.erb @@ -0,0 +1 @@ +<%= @message %> \ No newline at end of file -- cgit v1.2.3