From 97ee600bab782955a917cf70c971499bfb6fe1f2 Mon Sep 17 00:00:00 2001 From: Brendan G. Lim Date: Mon, 23 Aug 2010 18:09:09 -0400 Subject: Updating for gem consumption --- README.rdoc | 24 +++++++++----- lib/sms_fu.rb | 94 ++++++++++++++++++++++++----------------------------- lib/sms_notifier.rb | 1 - sms-fu.gemspec | 52 ----------------------------- 4 files changed, 57 insertions(+), 114 deletions(-) delete mode 100644 sms-fu.gemspec diff --git a/README.rdoc b/README.rdoc index 32dbda5..5485ac0 100644 --- a/README.rdoc +++ b/README.rdoc @@ -1,4 +1,4 @@ -= SMS Fu += SMS Fu (sms_fu) Want to send an SMS from your Rails application? SMS Fu allows you to do just that. It allows you to send a text-message in the form of an e-mail to a cell phone on any @@ -25,11 +25,17 @@ etc. == Setup Instructions -Add this this one line to the controller you want to be able to use SMS Fu in. +Install sms_fu gem + + sudo gem install sms_fu - class ExampleController < ApplicationController - has_sms_fu - end +Require the sms_fu gem. Please not that ActionMailer is required. + + # Bundler + gem "sms_fu" + + # Old School + config.gem "sms_fu" Modify sms_fu.yml in your config folder with your reply-to e-mail address. @@ -55,22 +61,22 @@ That's it! Now you're good to go. * Check sms_fu.yml for a complete list of supported carriers, including international carriers as well. - deliver_sms("5558675309","at&t","message") + SMSFu.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. - deliver_sms("5558675309","at&t","message", :from => "bob@test.com") + SMSFu.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. - deliver_sms("5558675309","at&t","message", :limit => 128) + SMSFu.deliver("5558675309","at&t","message", :limit => 128) * You can retrieve just the formatted address to use in your own mailer. - get_sms_address("5558675309","at&t") # => "5558675309@txt.att.net" + SMSFu.sms_address("5558675309","at&t") # => "5558675309@txt.att.net" == View Helpers diff --git a/lib/sms_fu.rb b/lib/sms_fu.rb index 1b3f35d..3e17b78 100644 --- a/lib/sms_fu.rb +++ b/lib/sms_fu.rb @@ -1,6 +1,8 @@ require 'yaml' +require 'action_mailer' require 'sms_notifier' -# Copyright (c) 2008 Brendan G. Lim (brendangl@gmail.com) + +# 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 @@ -22,70 +24,58 @@ require 'sms_notifier' # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. module SMSFu - def self.included(base) - base.class_eval do - def self.has_sms_fu - include SMSFu - end - end - end - RAILS_CONFIG_ROOT = defined?(Rails) ? - Rails.env == 'test' ? - "#{File.dirname(__FILE__)}/../templates" : - "#{RAILS_ROOT}/config" : - RAILS_ENV == 'test' ? - "#{File.dirname(__FILE__)}/../templates" : - "#{RAILS_ROOT}/config" + (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 << self + def carrier_name(key) + carriers[key]['name'] + end - def self.carrier_name(key) - carriers[key]['name'] - end - - def self.carriers - @@carriers.dup - end - - def deliver_sms(number,carrier,message,options={}) - raise SMSFuException.new("Cannot deliver an empty message to #{format_number(number)}") if message.nil? or message.empty? + def carriers + @@carriers.dup + end - options[:limit] ||= message.length - options[:from] ||= @@from_address - message = message[0..options[:limit]-1] - sms_email = determine_sms_email(format_number(number),carrier) + 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) - SmsNotifier.deliver_sms_message(sms_email,message,options[:from]) - rescue SMSFuException => exception - raise exception - end + SmsNotifier.deliver_sms_message(sms_email,message,options[:from]) + rescue SMSFuException => exception + raise exception + end - def get_sms_address(number,carrier) - number = format_number(number) - determine_sms_email(number,carrier) - end + def sms_address(number,carrier) + number = format_number(number) + sms_email(number,carrier) + end - private + private - 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 + 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 - def is_valid?(number) - number.length >= 10 && number[/^.\d+$/] - end + def is_valid?(number) + number.length >= 10 && number[/^.\d+$/] + end - def determine_sms_email(phone_number, carrier) - if @@carriers.has_key?(carrier.downcase) + 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']}" - else - raise SMSFuException.new("Specified carrier, #{carrier} is not supported.") - end - end + end + end class SMSFuException < StandardError; end end diff --git a/lib/sms_notifier.rb b/lib/sms_notifier.rb index c028a74..7b5d1e8 100644 --- a/lib/sms_notifier.rb +++ b/lib/sms_notifier.rb @@ -21,7 +21,6 @@ require 'yaml' # 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 diff --git a/sms-fu.gemspec b/sms-fu.gemspec deleted file mode 100644 index 31e59eb..0000000 --- a/sms-fu.gemspec +++ /dev/null @@ -1,52 +0,0 @@ -# -*- encoding: utf-8 -*- - -Gem::Specification.new do |s| - s.name = %q{sms-fu} - s.version = "1.0.0" - - s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= - s.authors = ["Brendan G. Lim"] - s.date = %q{2009-07-20} - s.description = %q{SMS Fu allows ou to send a text-message for free in the form of an e-mail to a mobile recipient.} - s.email = %q{brendangl@gmail.com} - s.extra_rdoc_files = [ - "README.rdoc" - ] - s.files = [ - "CHANGELOG", - "MIT-LICENSE", - "README.rdoc", - "Rakefile", - "VERSION", - "init.rb", - "install.rb", - "lib/sms_fu.rb", - "lib/sms_fu_helper.rb", - "lib/sms_notifier.rb", - "sms-fu.gemspec", - "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 - s.homepage = %q{http://github.com/brendanlim/sms-fu} - s.rdoc_options = ["--charset=UTF-8"] - s.require_paths = ["lib"] - s.rubygems_version = %q{1.3.1} - s.summary = %q{SMS Fu allows ou to send a text-message for free in the form of an e-mail to a mobile recipient.} - s.test_files = [ - "test/sms_fu_test.rb" - ] - - if s.respond_to? :specification_version then - current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION - s.specification_version = 2 - - if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then - else - end - else - end -end -- cgit v1.2.3