Skip to content
Browse files

added phone number utils

made gem to return all incoming numbers with 972 country code
  • Loading branch information...
1 parent 615fcc6 commit 97816f4da61a5d0fa4d4d61cb36c02ac798a1ded @alextk committed Mar 26, 2012
View
2 lib/smsim.rb
@@ -1,6 +1,6 @@
require 'active_support/all'
-%w{config gateway sender delivery_notifications_parser report_puller sms_replies_parser cellular_phone_format_validator}.each do |file_name|
+%w{config gateway sender delivery_notifications_parser report_puller sms_replies_parser phone_number_utils}.each do |file_name|
require File.join(File.dirname(__FILE__), 'smsim', file_name)
end
View
16 lib/smsim/cellular_phone_format_validator.rb
@@ -1,16 +0,0 @@
-module Smsim
- class CellularPhoneFormatValidator
- @@valid_full_length = '972545123456'.length
-
- # validates that given phone is Israely celluar format with country code: 972545123456
- def self.valid?(phone, errors = [])
- if phone.is_a?(String)
- errors << "Must start with 972" unless phone.start_with?('972')
- errors << "Must start consist of #{@@valid_full_length} digits (like 972545123456)" unless phone =~ /^#{972}[0-9]{#{@@valid_full_length-3}}$/
- else
- errors << 'Must be a string of 12 characters starting with 972 country code'
- end
- errors.empty?
- end
- end
-end
View
3 lib/smsim/delivery_notifications_parser.rb
@@ -43,6 +43,9 @@ def self.parse_notification_values_hash(values)
raise Smsim::Errors::GatewayError.new(301, "Missing notification values key #{key}. Values were: #{values.inspect}") if values[key].blank?
end
+ values[:phone] = PhoneNumberUtils.ensure_country_code(values[:phone])
+ values[:reply_to_phone] = PhoneNumberUtils.ensure_country_code(values[:reply_to_phone]) if values[:reply_to_phone].present?
+
begin
values[:gateway_status] = Integer(values[:gateway_status])
values[:delivered?] = gateway_status_delivered?(values[:gateway_status])
View
26 lib/smsim/phone_number_utils.rb
@@ -0,0 +1,26 @@
+module Smsim
+
+ class PhoneNumberUtils
+
+ # this method adds 972 country code to given phone if needed
+ # if phone is blank --> doesn't change it
+ def self.ensure_country_code(phone)
+ if !phone.blank? && !phone.start_with?('972')
+ phone = phone[1..phone.size] if phone.start_with?('0')
+ phone = "972#{phone}"
+ end
+ phone
+ end
+
+ @@valid_full_length = '972545123456'.length
+ # validates that given phone is Israeli cellular format with country code: 972545123456
+ def self.valid_cellular_phone?(phone, errors = [])
+ phone = phone.to_s
+ errors << "Must start with 972" unless phone.start_with?('972')
+ errors << "Must start consist of #{@@valid_full_length} digits (like 972545123456)" unless phone =~ /^#{972}[0-9]{#{@@valid_full_length-3}}$/
+ errors.empty?
+ end
+
+ end
+
+end
View
4 lib/smsim/sender.rb
@@ -18,7 +18,7 @@ def initialize(options)
raise ArgumentError.new("HTTP post url is missing") if options[:http_post_url].blank?
raise ArgumentError.new("Username and password must be present") if options[:username].blank? || options[:password].blank?
raise ArgumentError.new("Username and password must be present") if options[:username].blank? || options[:password].blank?
- raise ArgumentError.new("Reply to number must be cellular phone with 972 country code") if options[:reply_to_number].present? && !CellularPhoneFormatValidator.valid?(options[:reply_to_number])
+ raise ArgumentError.new("Reply to number must be cellular phone with 972 country code") if options[:reply_to_number].present? && !PhoneNumberUtils.valid_cellular_phone?(options[:reply_to_number])
@options = options
@logger = Logging.logger[self]
end
@@ -29,7 +29,7 @@ def send_sms(message_text, phones)
phones = [phones] unless phones.is_a?(Array)
# check that phones are in valid cellular format
for p in phones
- raise ArgumentError.new("Phone number '#{p}' must be cellular phone with 972 country code") unless CellularPhoneFormatValidator.valid?(p)
+ raise ArgumentError.new("Phone number '#{p}' must be cellular phone with 972 country code") unless PhoneNumberUtils.valid_cellular_phone?(p)
end
#raise ArgumentError.new("Max phones number is 100") if phones.count > 100
View
3 lib/smsim/sms_replies_parser.rb
@@ -49,6 +49,9 @@ def self.parse_reply_values_hash(values)
raise Smsim::Errors::GatewayError.new(601, "Missing sms reply values key #{key}. Values were: #{values.inspect}") if values[key].blank?
end
+ values[:phone] = PhoneNumberUtils.ensure_country_code(values[:phone])
+ values[:reply_to_phone] = PhoneNumberUtils.ensure_country_code(values[:reply_to_phone])
+
if values[:received_at].is_a?(String)
begin
values[:received_at] = DateTime.strptime(values[:received_at], '%d/%m/%Y %H:%M:%S')
View
2 spec/resources/PullClientNotificationResponse.soap.xml
@@ -12,7 +12,7 @@
<StatusDescription>Delivered</StatusDescription>
<CustomerMessageId>1</CustomerMessageId>
<CustomerParam>2</CustomerParam>
- <SenderNumber>0545290862</SenderNumber>
+ <SenderNumber>0529992080</SenderNumber>
<SegmentsNumber>1</SegmentsNumber>
<NotificationDate>13/03/2012 10:16:56</NotificationDate>
<SentMessage>test</SentMessage>
View
26 spec/smsim/cellular_phone_format_validator_spec.rb
@@ -1,26 +0,0 @@
-require 'spec_helper'
-
-describe Smsim::CellularPhoneFormatValidator do
- let(:v) { Smsim::CellularPhoneFormatValidator }
-
- describe '#valid?' do
- it 'should not be valid without country code' do
- v.valid?('0545290862').should be_false
- end
-
- it 'should not be valid for landline phones' do
- v.valid?('035447037').should be_false
- v.valid?('97235447037').should be_false
- end
-
- it 'should not be valid with country code but of different lenth' do
- v.valid?('9725452908622').should be_false
- v.valid?('97254529086').should be_false
- end
-
- it 'should be valid with country code' do
- v.valid?('972545290862').should be_true
- end
- end
-
-end
View
26 spec/smsim/phone_number_utils_spec.rb
@@ -0,0 +1,26 @@
+require 'spec_helper'
+
+describe Smsim::PhoneNumberUtils do
+ let(:utils) { Smsim::PhoneNumberUtils }
+
+ describe '#valid_cellular_phone?' do
+ it 'should not be valid without country code' do
+ utils.valid_cellular_phone?('0545290862').should be_false
+ end
+
+ it 'should not be valid for landline phones' do
+ utils.valid_cellular_phone?('035447037').should be_false
+ utils.valid_cellular_phone?('97235447037').should be_false
+ end
+
+ it 'should not be valid with country code but of different lenth' do
+ utils.valid_cellular_phone?('9725452908622').should be_false
+ utils.valid_cellular_phone?('97254529086').should be_false
+ end
+
+ it 'should be valid with country code' do
+ utils.valid_cellular_phone?('972545290862').should be_true
+ end
+ end
+
+end
View
29 spec/smsim/report_puller_spec.rb
@@ -3,20 +3,22 @@
describe Smsim::ReportPuller do
let(:request_uri) { Smsim::config.urls[:delivery_notifications_and_sms_replies_report_pull] }
let(:puller) { Smsim::ReportPuller.new(:username => 'alex', :password => 'pass', :wsdl_url => request_uri) }
+ let(:sender_number){ '0529992080' }
+ let(:sender_number_with_country_code){ '972529992080' }
describe '#pull_delivery_notifications_and_sms_replies' do
let(:notifications) do
[
- {'PhoneNumber' => '0541234567', 'Status' => 2, 'NotificationDate' => '22/03/2012 17:49:14', 'SegmentsNumber' => '1', 'CustomerMessageId' => 'id1234', 'StatusDescription' => 'OK'},
- {'PhoneNumber' => '0541234568', 'Status' => -2, 'NotificationDate' => '22/03/2012 17:29:14', 'SegmentsNumber' => '2', 'CustomerMessageId' => 'id12345', 'StatusDescription' => 'Not received'},
- {'PhoneNumber' => '0541234569', 'Status' => -4, 'NotificationDate' => '22/03/2012 23:49:14', 'SegmentsNumber' => '3', 'CustomerMessageId' => 'id123456', 'StatusDescription' => 'Hasum'},
+ {'PhoneNumber' => '0541234567', 'Status' => 2, 'NotificationDate' => '22/03/2012 17:49:14', 'SenderNumber' => sender_number, 'SegmentsNumber' => '1', 'CustomerMessageId' => 'id1234', 'StatusDescription' => 'OK'},
+ {'PhoneNumber' => '0541234568', 'Status' => -2, 'NotificationDate' => '22/03/2012 17:29:14', 'SenderNumber' => sender_number, 'SegmentsNumber' => '2', 'CustomerMessageId' => 'id12345', 'StatusDescription' => 'Not received'},
+ {'PhoneNumber' => '0541234569', 'Status' => -4, 'NotificationDate' => '22/03/2012 23:49:14', 'SenderNumber' => sender_number, 'SegmentsNumber' => '3', 'CustomerMessageId' => 'id123456', 'StatusDescription' => 'Hasum'},
]
end
let(:replies) do
[
- {'PhoneNumber' => '0541234567', 'SentMessage' => 'alex is king', 'NotificationDate' => '22/03/2012 17:49:14'},
- {'PhoneNumber' => '0541234568', 'SentMessage' => 'kak dila?', 'NotificationDate' => '22/03/2012 17:29:14'},
- {'PhoneNumber' => '0541234569', 'SentMessage' => 'asdf', 'NotificationDate' => '22/03/2012 23:49:14'},
+ {'PhoneNumber' => '0541234567', 'SentMessage' => 'alex is king', 'NotificationDate' => '22/03/2012 17:49:14', 'SenderNumber' => sender_number},
+ {'PhoneNumber' => '0541234568', 'SentMessage' => 'kak dila?', 'NotificationDate' => '22/03/2012 17:29:14', 'SenderNumber' => sender_number},
+ {'PhoneNumber' => '0541234569', 'SentMessage' => 'asdf', 'NotificationDate' => '22/03/2012 23:49:14', 'SenderNumber' => sender_number},
]
end
@@ -62,16 +64,22 @@
m1.gateway_status.should == 2
m1.completed_at.strftime('%d/%m/%Y %H:%M:%S').should == '22/03/2012 17:49:14'
m1.parts_count.should == 1
+ m1.phone.should == '972541234567'
+ m1.reply_to_phone.should == sender_number_with_country_code
m1.message_id.should == 'id1234'
m2 = report.notifications[1]
m2.gateway_status.should == -2
+ m2.phone.should == '972541234568'
+ m2.reply_to_phone.should == sender_number_with_country_code
m2.completed_at.strftime('%d/%m/%Y %H:%M:%S').should == '22/03/2012 17:29:14'
m2.parts_count.should == 2
m2.message_id.should == 'id12345'
m3 = report.notifications[2]
m3.gateway_status.should == -4
+ m3.phone.should == '972541234569'
+ m3.reply_to_phone.should == sender_number_with_country_code
m3.completed_at.strftime('%d/%m/%Y %H:%M:%S').should == '22/03/2012 23:49:14'
m3.parts_count.should == 3
m3.message_id.should == 'id123456'
@@ -110,17 +118,20 @@
m1 = report.replies[0]
m1.received_at.strftime('%d/%m/%Y %H:%M:%S').should == '22/03/2012 17:49:14'
- m1.phone.should == '0541234567'
+ m1.phone.should == '972541234567'
+ m1.reply_to_phone.should == sender_number_with_country_code
m1.text.should == 'alex is king'
m2 = report.replies[1]
m2.received_at.strftime('%d/%m/%Y %H:%M:%S').should == '22/03/2012 17:29:14'
- m2.phone.should == '0541234568'
+ m2.phone.should == '972541234568'
+ m2.reply_to_phone.should == sender_number_with_country_code
m2.text.should == 'kak dila?'
m3 = report.replies[2]
m3.received_at.strftime('%d/%m/%Y %H:%M:%S').should == '22/03/2012 23:49:14'
- m3.phone.should == '0541234569'
+ m3.phone.should == '972541234569'
+ m3.reply_to_phone.should == sender_number_with_country_code
m3.text.should == 'asdf'
end
View
12 spec/smsim/sender_spec.rb
@@ -14,14 +14,19 @@
lambda{ Smsim::Sender.new(options.update(:password => nil)) }.should raise_error(ArgumentError)
end
+ it 'should raise error if reply_to_phone is not valid cellular phone' do
+ lambda{ Smsim::Sender.new(options.update(:reply_to_number => '1234')) }.should raise_error(ArgumentError)
+ lambda{ Smsim::Sender.new(options.update(:reply_to_number => '0545290862')) }.should raise_error(ArgumentError)
+ end
+
it 'should create when all arguments present' do
sender.should be_present
end
end
describe '#send_sms' do
let(:message){ 'my message text' }
- let(:phone){ '0541234567' }
+ let(:phone){ '972541234567' }
it 'should raise error if text is blank' do
lambda{ sender.send_sms('', phone) }.should raise_error(ArgumentError)
@@ -31,6 +36,11 @@
lambda{ sender.send_sms(message, '') }.should raise_error(ArgumentError)
end
+ it 'should raise error if phone is not valid cellular phone' do
+ lambda{ sender.send_sms(message, '0541234567') }.should raise_error(ArgumentError)
+ lambda{ sender.send_sms(message, '541234567') }.should raise_error(ArgumentError)
+ end
+
end
describe '#build_send_sms_xml' do

0 comments on commit 97816f4

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