blib/lib/SMS/Send/IN/Unicel.pm

filter: Statementstmt 41.6% 30 / 72 Branchbran 6.6% 2 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 61.5% 8 / 13 Podpod 100% 2 / 2 totaltotal 33.0% 42 / 127 CC 18 SCAR 32.2
CC
18
Cov
30%
_TO
56.0
_send_method
42.0
_ERRORHANDLER
20.0
CRAP 24.9
↑ Summary
1 package SMS::Send::IN::Unicel;
2  
3 # ABSTRACT: SMS::Send driver to send messages via Unicel (http://unicel.in/)
4  
5 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use base 'SMS::Send::Driver';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 our $VERSION = '0.01'; # VERSION
14 4 our $AUTHORITY = 'cpan:INDRADG'; # AUTHORITY
15  
16 # setup error code lookup list for Unicel
17 4 our %unicel_error_codes = ( "0x200", "Invalid Username or Password",
18 "0x201", "Account suspended due to one of several defined reasons",
19 "0x202", "Invalid Source Address/Sender ID. As per GSM standard, the sender ID should be within 11 characters",
20 "0x203", "Message length exceeded (more than 160 characters) if concat is set to 0 Message",
21 "0x204", "Message length exceeded (more than 459 characters) in concat is set to 1",
22 "0x205", "DRL URL is not set",
23 "0x206", "Only the subscribed service type can be accessed - make sure of the service type you are trying to connect with",
24 "0x207", "Invalid Source IP - kindly check if the IP is responding",
25 "0x208", "Account deactivated/expired",
26 "0x209", "Invalid message length (less than 160 characters) if concat is set to 1",
27 "0x210", "Invalid Parameter values",
28 "0x211", "Invalid Message Length (more than 280 characters)",
29 "0x212", "Invalid Message Length",
30 "0x213", "Invalid Destination Number" );
31  
32 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
33 2 my ( $class, %args ) = @_;
34  
35 # check we have an username and password
36 2 ▶ die "Username needs to be passed as 'uname'" unless ( $args{_login} );
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"_login"} 0 2 2
37 2 ▶ die "Password needs to be passed as 'pass'" unless ( $args{_password} );
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"_password"} 0 2 2
38  
39 # build the object
40 2 my $self = bless {
41 _endpoint => 'https://unicel.in/SendSMS/sendmsg.php',
42 _debug => 0,
43 %args
44 }, $class;
45  
46 # get an LWP user agent ready
47 2 $self->{ua} = LWP::UserAgent->new;
48
49 2 return $self;
50 }
51  
52 0 ▶sub _send_method {
Subroutine _send_method: not called CC=6 SCAR=37.4
53 0 my ( $self, @args ) = @_;
54  
55 0 my @params;
56 0 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 0 0
57 0 my $key = shift @args;
58 0 my $val = shift @args;
59 0 push( @params, join( '=', uri_escape($key), uri_escape($val) ) );
60 0 ▶ print STDERR ">>> Arg $key = $val\n" if ( $self->{_debug} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
61 }
62 0 my $url = join( '?', $self->{_endpoint}, join( '&', @params ) );
63 0 ▶ print STDERR ">>> GET $url\n" if ( $self->{_debug} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
64  
65 0 my $res = $self->{ua}->get($url);
66  
67 0 ▶ printf STDERR "<<< Status: %s\n<<< Content: %s\n", $res->code, $res->content if ( $self->{_debug} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
68  
69 0 my $errorbroker = $self->_ERRORHANDLER ( $res, %unicel_error_codes );
70  
71 0 ▶ die $res->status_line unless ( $res->is_success );
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
72  
73 0 return $res;
74 }
75  
76 0 ▶sub send_sms {
Subroutine send_sms: not called CC=1 SCAR=6.9
Pod documented
77  
78 0 my ( $self, %args ) = @_;
79
80 # check for message for 160 char limit
81 0 my $text = $self->_MESSAGETEXT ( $args{text} );
82
83 # check destination number for well-formedness under NNP 2003 schema
84 0 my $to = $self->_TO ( $args{to} );
85  
86 $self->_send_method(
87 uname => $self->{_login},
88 pass => $self->{_password},
89 dest => $args{to},
90 msg => $args{text},
91 0 );
92 }
93  
94 # -----------------------------------------------------
95 # internal sanitization routines
96 # -----------------------------------------------------
97  
98 0 ▶sub _MESSAGETEXT {
Subroutine _MESSAGETEXT: not called CC=2 SCAR=10.1
99 0 my ( $self, $text ) = @_;
100 4 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 0 ▶ die "Message length over limit. Max length is 160 characters" unless ( length($text) <= 160 );
Branch: 1 branch
BranchTrueFalseTotal
unless length $text <= 160 0 0 0
102 } # check for 160 char length of message text
103  
104 # As per National Numbering Plan 2003, Indian mobile phone numbers have to be in
105 # [9|8|7]XXXXXXXXX format. So we need to sanitize our input. The driver expects
106 # number string in 91XXXXXXXXXX format
107  
108 0 ▶sub _TO {
Subroutine _TO: not called CC=7 SCAR=40.3
109 0 my ( $self, $dest ) = @_;
110  
111 0 my $checkseries;
112 my $countrycode;
113  
114 # strip out NaN characters
115 0 $dest =~ s/[^\d]//g;
116  
117 # strip leading zero as some have the habit of inputing numbers as 0XXXXXXXXXX
118 0 $dest =~ s/^0+//g;
119  
120 # check destination number length and format for well-formedness and fix common issues.
121 0 ▶ if ( length($dest) == 12 or length($dest) == 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dest == 12 or length $dest == 10) { } 0 0 0
Condition0%0/3
length $dest == 12 or length $dest == 10
l!l&&r!l&&!r
000
MC/DC0%0/2
length $dest == 12 or length $dest == 10
length $dest == 12 length $dest == 10
Truth table0%0/3
A or B
A= length $dest == 12B= length $dest == 10
ABresult
1X1
011
000
122 0 ▶ if ( length($dest) == 12 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dest == 12) 0 0 0
123 0 $countrycode = substr $dest, 0, 2;
124 0 ▶ die "Country code incorrect, needs to be 91 for India" unless ( $countrycode eq '91' );
Branch: 1 branch
BranchTrueFalseTotal
unless $countrycode eq 91 0 0 0
125 }
126 0 ▶ if ( length($dest) == 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dest == 10) 0 0 0
127 0 $countrycode = "91";
128 0 $dest = $countrycode . $dest; #bring it up to 91XXXXXXXXXX
129 }
130  
131 # check for 9,8,7 series numbering under NNP 2003
132 0 $checkseries = substr $dest, 2, 1;
133 0 ▶ die "Invalid phone number as per National Numbering Plan 2003" unless ( $checkseries =~ /[9|8|7]/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $checkseries =~ /[9|8|7]/ 0 0 0
134 } else {
135 0 die "Invalid phone number format";
136 }
137 0 return $dest;
138 }
139  
140 0 ▶sub _ERRORHANDLER {
Subroutine _ERRORHANDLER: not called CC=4 SCAR=30.0
141 0 my ( $self, $res, %uecodes ) = @_;
142  
143 0 my $res_content = $res->content;
144  
145 # check for unique MID (message ID) signifying a successful transaction
146  
147 0 ▶ if ( $res_content =~ /^[0-9]+$/ and length( $res_content ) == 19 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res_content =~ /^[0-9]+$/ and length $res_content == 19) { } 0 0 0
Condition0%0/3
$res_content =~ /^[0-9]+$/ and length $res_content == 19
!ll&&!rl&&r
000
MC/DC0%0/2
$res_content =~ /^[0-9]+$/ and length $res_content == 19
$res_content =~ /^[0-9]+$/ length $res_content == 19
Truth table0%0/3
A and B
A= $res_content =~ /^[0-9]+$/B= length $res_content == 19
ABresult
0X0
100
111
148 0 return "Successfully transmitted with MID $res_content\n";
149 } else {
150 0 my $ecode = substr $res_content, 0, 5;
151 0 ▶ if ( $uecodes{$ecode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uecodes{$ecode}) 0 0 0
152 0 die "Unicel error $ecode : $uecodes{$ecode}";
153 }
154 }
155 }
156  
157 4 1;
158  
159 __END__
↑ Summary