File Coverage

blib/lib/SMS/Send/RedOxygen.pm
Criterion Covered Total %
statement 25 40 62.5
branch 0 8 0.0
condition 7 13 53.8
subroutine 7 8 87.5
pod 2 2 100.0
total 41 71 57.7


line stmt bran cond sub pod time code
1             package SMS::Send::RedOxygen;
2            
3             =pod
4            
5             =head1 NAME
6            
7             SMS::Send::RedOxygen - SMS::Send driver for RedOxygen.com RedSMS
8            
9             =head1 SYNOPSIS
10            
11             # Create a RedOxygen sender. For now, only AccountID+Password authentication
12             # is supported; anonymous IP address based sending won't work.
13             #
14             my $send = SMS::Send->new(
15             'RedOxygen',
16             _accountid => 'RedOxygenAccountID',
17             _password => 'RedOxygenPassword',
18             _email => 'RegisteredEmail'
19             );
20            
21             # Send a message
22             $send->send_sms(
23             text => 'Hi there',
24             to => '+61 (4) 1234 5678',
25             );
26            
27             # An exception is thrown if the web API replies with a HTTP status
28             # code other than a 2xx OK or if the reply body error code is anything
29             # except 0000. The error can be found in the exception text.
30            
31             =head1 DESCRIPTION
32            
33             This SMS::Send driver bridges the SMS::Send API to RedOxygen's web API for SMS
34             sending. RedOxygen uses custom message formats and response codes and can't just
35             be used via simple JSON POST calls.
36            
37             To use this driver you must have a RedOxygen account, either a trial or full account.
38             RedOxygen's rates are not flat across all regions on most accounts though such accounts
39             may be negoated.
40            
41             This driver requires numbers to be in full international format.
42            
43             LWP::UserAgent must be available for this module to function. This is typically
44             packaged as libwww-perl on many systems, and can also be installed from CPAN.
45            
46             =head1 LICENSE
47            
48             The same as for Perl itself
49            
50             =cut
51            
52 2     2   43330 use 5.010;
  2         8  
  2         78  
53 2     2   11 use strict;
  2         3  
  2         70  
54            
55 2     2   20 use vars qw{$VERSION @ISA};
  2         4  
  2         130  
56             BEGIN {
57 2     2   4 $VERSION = '1.06';
58 2         86 @ISA = 'SMS::Send::Driver';
59             }
60            
61 2     2   11 use SMS::Send::Driver ();
  2         3  
  2         39  
62 2     2   4161 use LWP::UserAgent;
  2         160644  
  2         737  
63            
64            
65            
66            
67            
68             #####################################################################
69             # Constructor
70            
71             sub new {
72 4     4 1 2405 my $class = shift;
73 4         13 my %args = @_;
74            
75             # Create the object
76 4   66     216 my $accid = $args{'_accountid'} || Carp::croak("The _accountid parameter must be set to your RedOxygen account ID code");
77 3   66     168 my $pw = $args{'_password'} || Carp::croak("The _password parameter must be set to your RedOxygen account password");
78 2   66     258 my $email = $args{'_email'} || Carp::croak("The _email parameter must be set to the email address associated with your RedOxygen account");
79 1   50     6 my $url = $args{'_url'} || 'http://www.redoxygen.net/sms.dll?Action=SendSMS';
80 1         6 my $self = bless {
81             'accountid' => $accid,
82             'password' => $pw,
83             'email' => $email,
84             'url' => $url
85             }, $class;
86            
87 1         5 $self;
88             }
89            
90             # Reference: http://www.redoxygen.com/developers/perl/
91             #
92             # This is the "simple" HTTP Post format that uses url-encoded bodies. It supports
93             # one message per HTTP request. Parameters are passed as a hash.
94             sub send_sms
95             {
96 0     0 1   my $self = shift;
97            
98 0           my $browser = LWP::UserAgent->new;
99 0           my %args = @_;
100            
101             # RedOxygen doesn't check for errors very well and tends to return success
102             # when it hasn't done anything. Catch obvious problems.
103 0 0         if (!$args{text}) {
104 0           Carp::croak("No message supplied, need 'text' parameter");
105             }
106 0 0         if (!$args{to}) {
107 0           Carp::croak("No recipient specified, need 'to' parameter");
108             }
109             # RedOxygen doesn't like a leading +, spaces, parens, etc, so strip them
110 0           $args{to} =~ s/[^0-9]//g;
111            
112             # Note: We don't test for password. It's optional, as you can use IP Address
113             # based authentication instead.
114            
115             # You might expect this to be a hash, but RedOxygen cares about the *order*
116             # in which the parameters appear in the request. We must specify them precisely
117             # as given below.
118 0   0       my $request = [
119             'AccountID' => $self->{accountid},
120             'Email' => $self->{email},
121             'Password' => $self->{password} || '',
122             'Recipient' => $args{to},
123             'Message' => $args{text}
124             ];
125            
126 0           my $response = $browser->post($self->{url}, $request);
127 0 0         Carp::croak("HTTP error POSTing SMS to $self->{url}\n" . $response->status_line)
128             unless $response->is_success();
129            
130             # The RedOxygen API is pretty damn ugly; instead of returning HTTP codes, it returns
131             # numeric error codes in the request body as the first 4 bytes. Hopefully all the time.
132             # Extract the error code and return it as well as the full message.
133             #
134 0           my $redstatus = substr($response->content,0,4);
135 0 0         if (int($redstatus) != 0) {
136 0           Carp::croak("RedOxygen API returned error: " . $response->content);
137             }
138             # RedOxygen doesn't give us final delivery confirmation but by this point we know
139             # that submission worked. Probably.
140 0           return 1;
141             }
142            
143             =pod
144            
145             =head1 SUPPORT
146            
147             Bugs should be reported via the CPAN bug tracker at
148            
149             L
150            
151             For other issues, contact the author.
152            
153             =head1 AUTHOR
154            
155             Craig Ringer Ecraig@2ndquadrant.comE using SMS::Send by Adam Kennedy.
156            
157             =head1 COPYRIGHT
158            
159             Copyright 2012 Craig Ringer
160            
161             This program is free software; you can redistribute
162             it and/or modify it under the same terms as Perl itself.
163            
164             The full text of the license can be found in the
165             LICENSE file included with this module.
166            
167             =cut
168            
169             1;