Skip to content

Commit

Permalink
better..more tests...
Browse files Browse the repository at this point in the history
  • Loading branch information
nofxx committed Aug 18, 2008
1 parent c87d22b commit 53e4589
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 35 deletions.
1 change: 1 addition & 0 deletions Manifest.txt
Expand Up @@ -39,6 +39,7 @@ script/txt2html
setup.rb
sms_brasil.gemspec
spec/activesms/base_spec.rb
spec/activesms/connection_adapters/abstract_adapter_spec.rb
spec/activesms/connection_adapters/human_adapter_spec.rb
spec/activesms/email_spec.rb
spec/activesms/sms2email_spec.rb
Expand Down
7 changes: 2 additions & 5 deletions lib/activesms/config.rb
Expand Up @@ -9,15 +9,12 @@ module ActiveSms #:nodoc#
conf_yml ||= YAML::load(File.open("#{RAILS_CONFIG_ROOT}/sms.yml"))

# #
# Get general parameters
# # Get general parameters
CONFIG = conf_yml['config']

# #
# Get the configured gateway
GATEWAY = conf_yml['gateway']

# #
# Get all carriers
CARRIERS = conf_yml['carriers']


end
5 changes: 1 addition & 4 deletions lib/activesms/connection_adapters/human_adapter.rb
Expand Up @@ -74,10 +74,7 @@ def deliver(sms)
}
# Human supports scheduling
# if we got some date, share with em!
if sms.schedule
human_schedule = date_format_human(sms.schedule)
params[:schedule] = human_schedule
end
params[:schedule] = date_format_human(sms.schedule) if sms.schedule
# Send it!
send_http_request(@service_url, params)
end
Expand Down
1 change: 0 additions & 1 deletion lib/activesms/connections.rb
Expand Up @@ -40,7 +40,6 @@ def connection=(spec) #:nodoc:
# The exceptions AdapterNotSpecified, AdapterNotFound, and ArgumentError
# may be returned.
def establish_connection(config)
# config ||= GATEWAY
config = config.symbolize_keys
unless config.key?(:adapter)
raise AdapterNotSpecified, "#{config} adapter is not configured"
Expand Down
13 changes: 9 additions & 4 deletions lib/activesms/email.rb
@@ -1,8 +1,13 @@
require 'yaml'
#require 'activesms/config'

module ActiveSms #:nodoc#
module Email
# @config ||= CONFIG
@@carriers ||= CARRIERS
@@from_address ||= CONFIG['from_address']
RAILS_CONFIG_ROOT = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/config" : "#{File.dirname(__FILE__)}/../../generators/sms/templates" unless defined?(RAILS_CONFIG_ROOT)
# and load what we have!
conf_yml ||= YAML::load(File.open("#{RAILS_CONFIG_ROOT}/sms.yml"))
@@carriers ||= conf_yml['carriers']
@@from_address ||= conf_yml['config']['from_address']

def carriers
@@carriers.dup
Expand All @@ -14,7 +19,7 @@ def email_deliver(sms)#number,carrier,message,options={})
carrier = sms.carrier
# number = format_number(number)

#options[:limit] ||= message.length
#options[:limit] ||= message.length
#options[:from] ||= @@from_address
#message = message[0..options[:limit]-1]
sms_email = determine_sms_email(number, carrier)#format_number(number),carrier)
Expand Down
40 changes: 40 additions & 0 deletions spec/activesms/connection_adapters/abstract_adapter_spec.rb
@@ -0,0 +1,40 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
require 'net/https'

include ConnectionAdapters

describe AbstractAdapter do

before(:each) do
@abstract_adapter = AbstractAdapter.new(nil)
end

it "should instantiate" do
violated unless @abstract_adapter
end

it "should have a name" do
@abstract_adapter.adapter_name.should eql("Abstract")
end

it "should have a default deliver method, to be implemented by subclasses" do
@abstract_adapter.should_receive(:deliver)
@abstract_adapter.deliver(mock(Sms))
end

it "should respond to parse with nil.. for some obscure reason" do
@abstract_adapter.parse(mock(Sms)).should eql(nil)
end

it "should generate a http request with a valid url" do
class A < AbstractAdapter
def deliver
send_http_request('http://yahoo.com?teste=4', {:teste => 1})
end
end
@a = A.new
@a.deliver.should eql(1)
end


end
43 changes: 27 additions & 16 deletions spec/activesms/email_spec.rb
Expand Up @@ -22,38 +22,49 @@ def emaio
Noter.deliver(sms)
end

describe "Instantiated" do
before(:each) do
@email = Class.new { include ActiveSms::Email }.new
# @email = Aa.new
end
describe "Include Module" do
include Email

it "should be valid" do
violated unless @email
it "should format number" do
stub!(:valid?).and_return(false)
format_number('555444').should eql('555555555')
end

it "should determine a correct email address" do
@email.get_sms_address('5543214321', 'tim').should eql('5543214321@tim.com.br')
@email.get_sms_address('5543214321', 'oi').should eql('5543214321@sms.oi.com.br')
get_sms_address('5543214321', 'tim').should eql('5543214321@tim.com.br')
get_sms_address('5543214321', 'oi').should eql('5543214321@sms.oi.com.br')
end

it "should be valid with more than 10 digits" do
is_valid?('1234567890').should be_true
end

it "should be invalid with lesse than 10 digits" do
is_valid?('123456789').should_not be_true
end

it "should clean the number" do
@email.get_sms_address('5-543-2=14321', 'tim').should eql('5543214321@tim.com.br')
@email.get_sms_address('55g432g14fd321', 'oi').should eql('5543214321@sms.oi.com.br')
it "should be invalid if it has something that is not a number.. NaN hehe" do
is_valid?('123456789a').should_not be_true
end

it "should clean the number from evil chars" do
get_sms_address('5-54g3-2=143h21', 'tim').should eql('5543214321@tim.com.br')
end

it "should throw an error if the carrier is not known" do
lambda {@email.get_sms_address('5543214321', 'nofxx-telecom')}.should raise_error(ActiveSms::CarrierException)
lambda {get_sms_address('5543214321', 'nofxx-telecom')}.should raise_error(ActiveSms::CarrierException)
end

it "should throw an error if the carrier is blank" do
lambda {@email.get_sms_address('5543214321', '')}.should raise_error(ActiveSms::CarrierException)
end
lambda {get_sms_address('5543214321', '')}.should raise_error(ActiveSms::CarrierException)
end



describe "Email2Sms" do
it "should throw an error if the carrier is blank" do
@sms = ActiveSms::Sms.new
lambda {@email.email_deliver(@sms)}.should raise_error(ActiveSms::CarrierException)
lambda {email_deliver(@sms)}.should raise_error(ActiveSms::CarrierException)
end
end
end
Expand Down
8 changes: 3 additions & 5 deletions spec/spec_helper.rb
Expand Up @@ -7,12 +7,10 @@
end

$:.unshift(File.dirname(__FILE__) + '/../lib')
$:.unshift(File.dirname(__FILE__) + '/../lib/activesms')
$:.unshift(File.dirname(__FILE__) + '/../lib/activesms/connection_adapters')
require 'activesms/sms'
#$:.unshift(File.dirname(__FILE__) + '/../lib/activesms')
#$:.unshift(File.dirname(__FILE__) + '/../lib/activesms/connection_adapters')
require 'activesms'
#
# require 'activesms/connection_adapters/abstract_adapter'

include ActiveSms

# #
Expand Down

0 comments on commit 53e4589

Please sign in to comment.