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

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package SMS::Send::IN::Textlocal;
2
3 # ABSTRACT: SMS::Send driver to send messages via Textlocal (India) SMS Gateway ( https://api.textlocal.in/send/ )
4
5 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 0 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=2.6
11
12 use base 'SMS::Send::Driver';
13
14 our $VERSION = '1.00'; # VERSION
15 our $AUTHORITY = 'cpan:INDRADG'; # AUTHORITY
16
17 sub new {
18 my ( $class, %args ) = @_;
19
20 # check we have the necessary credentials parameters
21 die "SenderID / DLT Header needs to be passsed as 'sender'" unless ( $args{_login} );
22 die "Textlocal API key needs to be passed as 'password'" unless ( $args{_password} );
23
24 # build the object
25 my $self = bless {
26 _endpoint => 'https://api.textlocal.in/send/',
27 _debug => 0,
28 %args
29 }, $class;
30
31 # get an LWP user agent ready
32 # FIXME - bypassing SSL cert check due to NIC's cert chain borkage
33 $self->{ua} = LWP::UserAgent->new;
34 #$self->{ua}->ssl_opts(
35 # SSL_verify_mode => 0,
36 # verify_hostname => 0,
37 #);
38
39 return $self;
40 }
41
42 sub _send_method {
43 my ( $self, @args ) = @_;
44
45 my @params;
46 while (@args) {
47 my $key = shift @args;
48 my $val = shift @args;
49 push( @params, join( '=', uri_escape($key), uri_escape($val) ) );
50 print STDERR ">>> Arg $key = $val\n" if ( $self->{_debug} );
51 }
52 my $url = join( '?', $self->{_endpoint}, join( '&', @params ) );
53 print STDERR ">>> GET $url\n" if ( $self->{_debug} );
54
55 my $res = $self->{ua}->get($url);
56
57 my $errorbroker = $self->_ERRORHANDLER ( $res );
58
59 if ( $errorbroker ) {
60 print STDERR "<<< Message sent successfully\n";
61 print STDERR "<<< SMS gateway response : $res->{_content}\n" if ( $self->{_debug} );
62 return 1;
63 } else {
64 print STDERR "<<< Message send failed\n";
65 print STDERR "<<< SMS gateway response : $res->{_content}\n";
66 return;
67 }
68 }
69
70 sub send_sms {
71
72 my ( $self, %args ) = @_;
73
74 # check for message for 160 char limit
75 my $text = $self->_MESSAGETEXT ( $args{text} );
76
77 # check destination number for well-formedness under NNP 2003 schema
78 my $to = $self->_TO ( $args{to} );
79
80 $self->_send_method(
81 sender => $self->{_login},
82 apikey => $self->{_password},
83 numbers => $args{to},
84 message => $args{text},
85 );
86 }
87
88 # -----------------------------------------------------
89 # internal sanitization routines
90 # -----------------------------------------------------
91
92 sub _MESSAGETEXT {
93 my ( $self, $text ) = @_;
94 use bytes;
95 die "Message length over limit. Max length is 160 characters" unless ( length($text) <= 160 );
96 } # check for 160 char length of message text
97
98 # As per National Numbering Plan 2003, Indian mobile phone numbers have to be in
99 # [9|8|7]XXXXXXXXX format. So we need to sanitize our input. The driver expects
100 # number string in 91XXXXXXXXXX format
101
102 sub _TO {
103 my ( $self, $dest ) = @_;
104
105 my $checkseries;
106 my $countrycode;
107
108 # strip out any NaN characters
109 $dest =~ s/[^\d]//g;
110
111 # strip leading zero as some have the habit of inputing numbers as 0XXXXXXXXXX
112 $dest =~ s/^0+//g;
113
114 # check destination number length and format for well-formedness and fix common issues.
115 if ( length($dest) == 12 or length($dest) == 10 ) {
116 if ( length($dest) == 12 ) {
117 $countrycode = substr $dest, 0, 2;
118 die "Country code incorrect, needs to be 91 for India" unless ( $countrycode eq '91' );
119 }
120 if ( length($dest) == 10 ) {
121 $countrycode = "91";
122 $dest = $countrycode . $dest; #bring it up to 91XXXXXXXXXX
123 }
124
125 # check for 9,8,7,6 series numbering under NNP 2003
126 # see https://en.wikipedia.org/wiki/Mobile_telephone_numbering_in_India
127 #
128 $checkseries = substr $dest, 2, 1;
129 die "Invalid phone number as per National Numbering Plan 2003" unless ( $checkseries =~ /[9|8|7|6]/ );
130 } else {
131 die "Invalid phone number format";
132 }
133 return $dest;
134 }
135
136 sub _ERRORHANDLER {
137 my ( $self, $res ) = @_;
138
139 # check status for success or failure
140
141 my $json = decode_json $res->content;
142  
143 if ( $json->{'status'} eq 'failure' ) {
144 return;
145 } else {
146 return 1;
147 }
148 }
149
150 1;
151
152 __END__
↑ Summary