File Coverage

blib/lib/Net/SMS/RoutoMessaging.pm
Criterion Covered Total %
statement 22 47 46.8
branch 1 12 8.3
condition 1 5 20.0
subroutine 7 9 77.7
pod 2 3 66.6
total 33 76 43.4


line stmt bran cond sub pod time code
1             package Net::SMS::RoutoMessaging;
2             {
3             $Net::SMS::RoutoMessaging::VERSION = '0.09';
4             }
5              
6             # ABSTRACT: Send SMS messages via the RoutoMessaging HTTP API
7              
8 1     1   20937 use strict;
  1         3  
  1         41  
9 1     1   6 use warnings;
  1         2  
  1         32  
10              
11 1     1   5 use Carp;
  1         5  
  1         100  
12 1     1   1049 use HTTP::Request::Common;
  1         31512  
  1         91  
13 1     1   1164 use LWP::UserAgent;
  1         26821  
  1         52  
14              
15             use constant {
16 1         515 PROVIDER => "https://smsc5.telesignmobile.com/NewSMSsend",
17             TIMEOUT => 10
18 1     1   12 };
  1         2  
19              
20             sub new {
21 1     1 1 367 my ($class, %args) = @_;
22              
23 1 50 33     8 if (! exists $args{username} || ! exists $args{password}) {
24 0         0 Carp::croak("${class}->new() requires username and password as parameters\n");
25             }
26              
27 1         2 my $self = \%args;
28 1         4 bless $self, $class;
29             }
30              
31             sub send_sms {
32 0     0 1   my ($self, %args) = @_;
33              
34 0           my $ua = LWP::UserAgent->new();
35 0           $ua->timeout(TIMEOUT);
36 0           $ua->agent("Net::SMS::RoutoMessaging/$Net::SMS::RoutoMessaging::VERSION");
37              
38 0           $args{number} =~ s{\D}{}g;
39              
40 0           my $url = PROVIDER;
41 0           my $resp = $ua->request(POST $url, [ user => $self->{username}, pass => $self->{password}, %args ]);
42 0           my $as_string = $resp->as_string;
43              
44 0 0         if (! $resp->is_success) {
45 0           my $status = $resp->status_line;
46 0           warn "HTTP request failed: $status\n$as_string\n";
47 0           return 0;
48             }
49              
50 0           my $res = $resp->content;
51 0           chomp($res);
52              
53 0           my $return = 1;
54 0 0         unless ($res =~ /^success/) {
55 0           warn "Failed: $res\n";
56 0           $return = 0;
57             }
58              
59 0 0         if ($args{long_status}) {
60 0 0         return wantarray ? ($return, $res, status_message($res)) : $return;
61             }
62             else {
63 0 0         return wantarray ? ($return, $res) : $return;
64             }
65             }
66              
67             sub status_message {
68 0     0 0   my ($status) = @_;
69              
70 0           my %desc = (
71             success => 'sending successful',
72             error => 'not all required parameters are present',
73             auth_failed => 'incorrect username and/or password and/or not allowed IP address',
74             wrong_number => 'the number contains non-numeric characters',
75             not_allowed => 'you are not allowed to send to this number',
76             too_many_numbers => 'sending to more than 10 numbers per request',
77             no_message => 'the message body is empty',
78             too_long => 'message is too long',
79             wrong_type => 'an incorrect message type was selected',
80             wrong_message => 'vCalendar or VCard contains wrong message',
81             wrong_format => 'the wrong message format was selected',
82             bad_operator => 'wrong operator code',
83             failed => 'internal error',
84             sys_error => 'the system error',
85             'No Credits Left' => 'user has no credits'
86             );
87              
88 0   0       return $desc{$status} || 'unknown or empty status';
89             }
90              
91             1;
92              
93              
94              
95             =pod
96              
97             =head1 NAME
98              
99             Net::SMS::RoutoMessaging - Send SMS messages via the RoutoMessaging HTTP API
100              
101             =head1 VERSION
102              
103             version 0.09
104              
105             =head1 SYNOPSIS
106              
107             # Create a testing sender
108             my $sms = Net::SMS::RoutoMessaging->new(
109             username => 'testuser', password => 'testpass'
110             );
111              
112             # Send a message
113             my ($sent, $status) = $sms->send_sms(
114             message => "All your base are belong to us",
115             number => '1234567890',
116             );
117              
118             $sent will contain a true / false if the sending worked,
119             $status will contain the status message from the provider.
120              
121             # If you just want a true / false if it workes, use :
122             my $sent = $sms->send_sms(
123             message => "All your base are belong to us",
124             number => '1234567890',
125             );
126              
127             # If you want a better description of the status message, use the
128             # long_status parameter
129             my ($sent, $status, $desc) = $sms->send_sms(
130             message => "All your base are belong to us",
131             number => '1234567890',
132             long_status => 1,
133             );
134              
135             if ($sent) {
136             # Success, message sent
137             }
138             else {
139             # Something failed
140             warn("Failed : $status");
141             }
142              
143             =head1 DESCRIPTION
144              
145             Perl module to send SMS messages through the HTTP API provided by RoutoMessaging
146             (routomessaging.com).
147              
148             =head1 METHODS
149              
150             =head2 new
151              
152             new( username => 'testuser', password => 'testpass' )
153              
154             Nothing fancy. You need to supply your username and password
155             in the constructor, or it will complain loudly.
156              
157             =head2 send_sms
158              
159             send_sms(number => $phone_number, message => $message)
160              
161             Uses the API to send a message given in C<$message> to
162             the phone number given in C<$phone_number>.
163              
164             Phone number should be given with only digits. No "+" or spaces, like this:
165              
166             =over 4
167              
168             =item C<1234567890>
169              
170             =back
171              
172             Returns a true / false value and a status message. The message is "success" if the server has accepted your query. It does not mean that the message has been delivered.
173             If the long_status argument is set, then it also returns a long description as the third value.
174              
175             =head1 SEE ALSO
176              
177             RoutoMessaging website, http://www.routomessaging.com/
178              
179             =head1 AUTHOR
180              
181             Terje Kristensen
182              
183             =head1 COPYRIGHT AND LICENSE
184              
185             This software is Copyright (c) 2011 by Opera Software ASA.
186              
187             This is free software, licensed under:
188              
189             The (three-clause) BSD License
190              
191             =cut
192              
193              
194             __END__