Skip to content
Browse files

api refinements

tests fixes
added tests for gateway class
  • Loading branch information...
1 parent bab2518 commit d6f373959ebfdc4c0c58068b0e1a6e6774e982d5 @alextk committed
View
6 lib/smsim/delivery_notification.rb
@@ -29,15 +29,15 @@ def reason_not_delivered
end
def delivered?
- @status == 2
+ status == 2
end
def not_delivered?
- @status == -2
+ status == -2
end
def blocked?
- @status == -4
+ status == -4
end
# params will look something like the following:
View
6 lib/smsim/gateway.rb
@@ -25,13 +25,13 @@ def username
# * delivery_notification_url - url to which delivery notification will be sent
# * reply_to_number - to which number sms receiver will reply
# Returns unique message id string. Uou must save this id if you want to receive delivery notifications via push/pull
- def send(text, phones, options = {})
+ def send_sms(text, phones, options = {})
options = options.update(@options)
- options[:customer_message_id] = self.class.generate_message_id
+ options[:message_id] = self.class.generate_message_id
xml = XmlRequestBuilder.build_send_sms(text, phones, options)
response = HttpExecutor.send_sms(xml)
raise Smsim::Errors::GatewayError.new(response.status, "Sms send failed: #{response.description}") unless self.class.send_response_status_ok?(response.status)
- options[:customer_message_id]
+ options[:message_id]
end
def self.generate_message_id
View
4 lib/smsim/xml_request_builder.rb
@@ -7,7 +7,7 @@ def self.build_send_sms(message_text, phones, options = {})
raise ArgumentError.new("Text must be at least 1 character long") if message_text.blank?
raise ArgumentError.new("Phones must include at least one phone") if phones.blank?
raise ArgumentError.new("Username and password must be present in options") if options[:username].blank? || options[:password].blank?
- raise ArgumentError.new("Message id must be present in options") if options[:customer_message_id].blank?
+ raise ArgumentError.new("Message id must be present in options") if options[:message_id].blank?
phones = phones.to_a unless phones.is_a?(Array)
raise ArgumentError.new("Max phones number is 100") if phones.count > 100
@@ -32,7 +32,7 @@ def self.build_send_sms(message_text, phones, options = {})
end
root.Settings do |settings|
settings.SenderNumber options[:reply_to_number]
- settings.CustomerMessageId options[:customer_message_id]
+ settings.CustomerMessageId options[:message_id]
settings.DeliveryNotificationUrl options[:delivery_notification_url] if options[:delivery_notification_url].present?
end
end
View
28 spec/smsim/gateway_spec.rb
@@ -14,6 +14,34 @@
end
end
+ describe '#send_sms' do
+ let(:g){ Smsim::Gateway.new('user', 'pass') }
+ before :each do
+ XmlResponseStubs.stub_request_with_sms_send_response(self)
+ end
+
+ it 'should pass username, password and message_id to request builder' do
+ Smsim::XmlRequestBuilder.should_receive(:build_send_sms).with('alex is king', '054123456', hash_including(:message_id))
+ g.send_sms('alex is king', '054123456')
+ end
+
+ it 'should pass reply_to_number and delivery_notification_url' do
+ g = Smsim::Gateway.new('user', 'pass', :reply_to_number => '0501234567')
+ Smsim::XmlRequestBuilder.should_receive(:build_send_sms).with('alex is king', '054123456', hash_including(:message_id, :reply_to_number => '0501234567'))
+ g.send_sms('alex is king', '054123456')
+
+ g = Smsim::Gateway.new('user', 'pass', :delivery_notification_url => 'google.com')
+ Smsim::XmlRequestBuilder.should_receive(:build_send_sms).with('alex is king', '054123456', hash_including(:message_id, :delivery_notification_url => 'google.com'))
+ g.send_sms('alex is king', '054123456')
+ end
+
+ it 'should return generated message id when send succeeds' do
+ mid = g.send_sms('alex is king', '054123456')
+ mid.should be_present
+ mid.should be_a(String)
+ mid.length.should > 10
+ end
+ end
end
View
8 spec/smsim/xml_request_builder_spec.rb
@@ -3,7 +3,7 @@
describe Smsim::XmlRequestBuilder do
describe '#build_send_sms' do
- let(:options){ {:username => 'alex', :password => 'pass', :reply_to_number => '0501234567', :customer_message_id => '123'} }
+ let(:options){ {:username => 'alex', :password => 'pass', :reply_to_number => '0501234567', :message_id => '123'} }
let(:message){ 'my message text' }
let(:phone){ '0541234567' }
let(:xml){ Smsim::XmlRequestBuilder.build_send_sms(message, phone, options) }
@@ -45,11 +45,11 @@
xml_doc.css('Inforu Settings SenderNumber').text.should == options[:reply_to_number]
end
- it 'should have customer_message_id' do
- xml_doc.css('Inforu Settings CustomerMessageId').text.should == options[:customer_message_id]
+ it 'should have message_id' do
+ xml_doc.css('Inforu Settings CustomerMessageId').text.should == options[:message_id]
end
- it 'should have delivery notification url if specified' do
+ it 'should have delivery notification url with gateway_user if specified' do
xml_doc.css('Inforu Settings DeliveryNotificationUrl').text.should be_blank
xml_doc = Nokogiri::XML(Smsim::XmlRequestBuilder.build_send_sms(message, phone, options.update(:delivery_notification_url => 'http://google.com')))
View
8 spec/smsim/xml_response_parser_spec.rb
@@ -6,20 +6,20 @@
let(:request_uri){ Smsim::HttpExecutor.urls.send_sms }
it 'should raise XmlResponseError when response Status is not an integer' do
- stub_request(:any, request_uri).to_return(:status => 200, :body => XmlResponseStubs.sms_send_response("asdf"))
+ XmlResponseStubs.stub_request_with_sms_send_response(self, :status => "asdf")
lambda{ Smsim::HttpExecutor.send_sms('xml') }.should raise_error(Smsim::Errors::XmlResponseError)
end
it 'should raise XmlResponseError when response NumberOfRecipients is not an integer' do
- stub_request(:any, request_uri).to_return(:status => 200, :body => XmlResponseStubs.sms_send_response(1, "desc", "asdf"))
+ XmlResponseStubs.stub_request_with_sms_send_response(self, :status => '1', :description => "received ok", :number_of_recipients => 'df')
lambda{ Smsim::HttpExecutor.send_sms('xml') }.should raise_error(Smsim::Errors::XmlResponseError)
end
it 'should return XmlResponse with status, description and number of recipients initialized' do
- stub_request(:any, request_uri).to_return(:status => 200, :body => XmlResponseStubs.sms_send_response(1, "desc", 2))
+ XmlResponseStubs.stub_request_with_sms_send_response(self, :status => '1', :description => "received ok", :number_of_recipients => '2')
response = Smsim::HttpExecutor.send_sms('xml')
response.status.should == 1
- response.description.should == "desc"
+ response.description.should == "received ok"
response.number_of_recipients.should == 2
end
end
View
14 spec/support/smsim_gateway_macros.rb
@@ -1,15 +1,21 @@
class XmlResponseStubs
class << self
- def sms_send_response(code, description = '', number_of_recipients = 0)
+ def sms_send_response(response_options = {})
+ response_options = {:status => 1, :description => 'received ok', :number_of_recipients => 0}.update(response_options)
xml = Builder::XmlMarkup.new( :indent => 2 )
xml.instruct!
xml.Result do |root|
- root.Status code
- root.Description description
- root.NumberOfRecipients number_of_recipients
+ root.Status response_options[:status]
+ root.Description response_options[:description]
+ root.NumberOfRecipients response_options[:number_of_recipients]
end
end
+
+ def stub_request_with_sms_send_response(example, options = {})
+ options = {:http_code => 200}.update(options)
+ example.stub_request(:any, Smsim::HttpExecutor.urls.send_sms).to_return(:status => options.delete(:http_code), :body => XmlResponseStubs.sms_send_response(options))
+ end
end
end

0 comments on commit d6f3739

Please sign in to comment.
Something went wrong with that request. Please try again.