blib/lib/Net/SMS/Mach.pm

filter: Statementstmt 45.7% 27 / 59 Branchbran 8.3% 1 / 12 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 81.8% 9 / 11 Podpod 66.6% 2 / 3 totaltotal 44.4% 40 / 90 CC 8 SCAR 27.2
CC
8
Cov
39%
send_sms
42.0
new
3.6
encode_ucs
2.0
CRAP 15.2
↑ Summary
1 package Net::SMS::Mach;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $Net::SMS::Mach::VERSION = '0.02';
4 }
5  
6 # ABSTRACT: Send SMS messages via the Mach HTTP API
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Encode qw(encode decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 PROVIDER1 => "http://gw1.promessaging.com/sms.php",
18 PROVIDER2 => "http://gw2.promessaging.com/sms.php",
19 TIMEOUT => 10
20 1 };
21  
22 1 ▶sub new {
Subroutine new: called CC=3 SCAR=12.7
Pod documented
23 1 my ($class, %args) = @_;
24  
25 1 ▶ if (! exists $args{userid} || ! exists $args{password}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"userid"} and exists $args{"password"}) 0 1 1
Condition33%1/3
exists $args{"userid"} and exists $args{"password"}
!ll&&!rl&&r
001
MC/DC0%0/2
exists $args{"userid"} and exists $args{"password"}
exists $args{"userid"} exists $args{"password"}
Truth table33%1/3
A and B
A= exists $args{"userid"}B= exists $args{"password"}
ABresult
0X0
100
111
26 0 Carp::croak("${class}->new() requires username and password as parameters\n");
27 }
28  
29 1 my $self = \%args;
30 1 bless $self, $class;
31 }
32  
33 0 ▶sub send_sms {
Subroutine send_sms: not called CC=6 SCAR=37.4
Pod documented
34 0 my ($self, %args) = @_;
35  
36 0 my $ua = LWP::UserAgent->new();
37 0 $ua->timeout(TIMEOUT);
38 0 $ua->agent("Net::SMS::Mach/$Net::SMS::Mach::VERSION");
39  
40 0 $args{number} =~ s{\D}{}g;
41  
42 0 my $message = $args{message};
43 0 my $enc;
44 0 ▶ if ($args{encode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"encode"}) 0 0 0
45 0 $enc = "ucs";
46 0 $message = encode_ucs($message);
47 }
48  
49 my $hash = {
50 dnr => "+$args{number}",
51 snr => $args{sender},
52 0 msg => $message,
53 encoding => $enc,
54 };
55  
56 0 ▶ my $url = $args{backup_server} ? PROVIDER2 : PROVIDER1;
Branch: 1 branch
BranchTrueFalseTotal
$args{'backup_server'} ? : 0 0 0
57 0 my $resp = $ua->request(POST $url, [ id => $self->{userid}, pw => $self->{password}, %$hash ]);
58 0 my $as_string = $resp->as_string;
59  
60 0 ▶ if (! $resp->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 0 0
61 0 my $status = $resp->status_line;
62 0 warn "HTTP request failed: $status\n$as_string\n";
63 0 return 0;
64 }
65  
66 0 my $res = $resp->content;
67 0 chomp($res);
68  
69 0 my $return = 1;
70 0 ▶ unless ($res =~ /^\+OK/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res =~ /^\+OK/) 0 0 0
71 0 warn "Failed: $res\n";
72 0 $return = 0;
73 }
74  
75 0 ▶ return wantarray ? ($return, $res) : $return;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
76 }
77  
78  
79 sub encode_ucs
80 0 ▶{
Subroutine encode_ucs: not called CC=1 SCAR=6.9
Pod undocumented
81 0 my $message = shift;
82  
83 0 utf8::decode($message);
84 0 utf8::decode($message);
85 0 utf8::upgrade($message);
86  
87 0 my $encoded = unpack('H*', encode('UCS-2BE', $message));
88  
89 0 return $encoded;
90 }
91 1 1;
92  
93  
94  
95 =pod
96  
97 =head1 NAME
98  
99 Net::SMS::Mach - Send SMS messages via the Mach HTTP API
100  
101 =head1 VERSION
102  
103 version 0.02
104  
105 =head1 SYNOPSIS
106  
107 # Create a testing sender
108 my $sms = Net::SMS::Mach->new(
109 userid => '123456', 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 sender => '+441234567890',
117 );
118  
119 $sent will contain a true / false if the sending worked,
120 $status will contain the status message from the provider.
121  
122 # If you just want a true / false if it workes, use :
123 my $sent = $sms->send_sms(
124 message => "All your base are belong to us",
125 number => '1234567890',
126 );
127  
128 # If your message is utf8 encoded, or you are unsure if it's iso-8859-1,
129 # use the encode flag to get the message UCS encoded.
130 my ($sent, $status, $desc) = $sms->send_sms(
131 message => "All your base are belong to us",
132 number => '1234567890',
133 encode => 1,
134 );
135  
136 if ($sent) {
137 # Success, message sent
138 }
139 else {
140 # Something failed
141 warn("Failed : $status");
142 }
143  
144 =head1 DESCRIPTION
145  
146 Perl module to send SMS messages through the HTTP API provided by Mach
147 (mach.com).
148  
149 =head1 METHODS
150  
151 =head2 new
152  
153 new( userid => '123456', password => 'testpass' )
154  
155 Nothing fancy. You need to supply your username and password
156 in the constructor, or it will complain loudly.
157  
158 =head2 send_sms
159  
160 send_sms(number => $phone_number, message => $message, encode => 0, backup_server => 1)
161  
162 Uses the API to send a message given in C<$message> to
163 the phone number given in C<$phone_number>.
164  
165 Phone number should be given with only digits, or with a "+" prefix.
166  
167 =over 4
168  
169 =item C<1234567890>
170  
171 =back
172  
173 Returns a true / false value and a status message. The message is "success" if the server has accepted your query.
174 This does not mean that the message has been delivered.
175  
176 =head1 SEE ALSO
177  
178 Mach website, http://www.mach.com/
179  
180 =head1 AUTHOR
181  
182 Terje Kristensen <terjek@opera.com>
183  
184 =head1 COPYRIGHT AND LICENSE
185  
186 This software is Copyright (c) 2011 by Opera Software ASA.
187  
188 This is free software, licensed under:
189  
190 The (three-clause) BSD License
191  
192 =cut
193  
194  
195 __END__
↑ Summary