blib/lib/Net/SMS/MessageNet.pm

filter: Statementstmt 20.2% 18 / 89 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 55.5% 5 / 9 Podpod 100% 3 / 3 totaltotal 17.6% 26 / 147 CC 21 SCAR 46.1
CC
21
Cov
14%
new
132.0
send
132.0
__ANON__
2.0
CRAP 100.4
↑ Summary
1 package Net::SMS::MessageNet;
2  
3 1 ▶use LWP();
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use HTTP::Cookies();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use URI::Escape();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 our ($VERSION) = '0.65';
9 1 our (@ISA) = qw(Exporter);
10 1 our (@EXPORT) = qw(send_sms);
11  
12 0 ▶sub new {
Subroutine new: not called CC=11 SCAR=48.8
Pod documented
13 0 my ($class, $user_name, $password, $params) = @_;
14 0 my ($self) = {};
15 0 my ($timeout) = 60;
16 0 ▶ unless ($user_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user_name) 0 0 0
17 0 die("The user_name must be supplied\n");
18 }
19 0 ▶ if (ref $user_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $user_name) 0 0 0
20 0 die("The user_name must be a scalar\n");
21 }
22 0 $self->{user_name} = $user_name;
23 0 ▶ unless ($password) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($password) 0 0 0
24 0 die("The password must be supplied\n");
25 }
26 0 ▶ if (ref $password) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $password) 0 0 0
27 0 die("The password must be a scalar\n");
28 }
29 0 $self->{password} = $password;
30 0 ▶ if (exists $params->{timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"timeout"}) 0 0 0
31 0 ▶ if (($params->{timeout}) && ($params->{timeout} =~ /^\d+$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{'timeout'} and $params->{'timeout'} =~ /^\d+$/) { } 0 0 0
Condition0%0/3
$params->{"timeout"} and $params->{"timeout"} =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
$params->{"timeout"} and $params->{"timeout"} =~ /^\d+$/
$params->{"timeout"} $params->{"timeout"} =~ /^\d+$/
Truth table0%0/3
A and B
A= $params->{"timeout"}B= $params->{"timeout"} =~ /^\d+$/
ABresult
0X0
100
111
32 0 $timeout = $params->{timeout};
33 } else {
34 0 die("The 'timeout' parameter must be a number\n");
35 }
36 }
37 0 my ($name) = "Net::SMS::MessageNet $VERSION "; # a space causes the default LWP User Agent to be appended.
38 0 ▶ if (exists $params->{user_agent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"user_agent"}) 0 0 0
39 0 ▶ if (($params->{user_agent}) && ($params->{user_agent} =~ /\S/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"user_agent"} and $params->{"user_agent"} =~ /\S/) 0 0 0
Condition0%0/3
$params->{"user_agent"} and $params->{"user_agent"} =~ /\S/
!ll&&!rl&&r
000
MC/DC0%0/2
$params->{"user_agent"} and $params->{"user_agent"} =~ /\S/
$params->{"user_agent"} $params->{"user_agent"} =~ /\S/
Truth table0%0/3
A and B
A= $params->{"user_agent"}B= $params->{"user_agent"} =~ /\S/
ABresult
0X0
100
111
40 0 $name = $params->{user_agent};
41 }
42 }
43 0 my ($ua) = new LWP::UserAgent( timeout => $timeout,
44 keep_alive => 1 );
45 0 $ua->agent($name);
46 0 my ($cookieJar) = new HTTP::Cookies( hide_cookie2 => 1 );
47 0 $ua->cookie_jar($cookieJar);
48 0 $ua->requests_redirectable([ 'GET' ]);
49 0 $self->{_ua} = $ua;
50 0 bless $self, $class;
51 0 return ($self);
52 }
53  
54 0 ▶sub send {
Subroutine send: not called CC=11 SCAR=48.8
Pod documented
55 0 my ($self, $phone_number, $message) = @_;
56 0 ▶ if ((length ($message)) > 160) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $message > 160) 0 0 0
57 0 die("Message greater than 160 characters\n");
58 }
59 0 ▶ unless ($phone_number =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($phone_number =~ /^\d+$/) 0 0 0
60 0 die("Phone number must be all numbers. The country code should be included\n");
61 }
62 0 my ($url);
63 0 eval { require Net::HTTPS; };
64 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
65 0 ▶ if ($^W) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^W) 0 0 0
66 0 warn("Using insecure means to send sms to messagenet.com.au\n");
67 }
68 0 $url = 'http://www.messagenet.com.au/dotnet/Lodge.asmx/LodgeSMSMessage';
69 } else {
70 0 ▶ unless (defined $ENV{HTTPS_VERSION}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ENV{"HTTPS_VERSION"}) 0 0 0
71 0 $ENV{HTTPS_VERSION} = '3';
72 }
73 0 ▶ unless (defined $ENV{HTTPS_CA_DIR}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ENV{"HTTPS_CA_DIR"}) 0 0 0
74 0 $ENV{HTTPS_CA_DIR} = 'certs';
75 }
76 0 $url = 'https://www.messagenet.com.au/dotnet/Lodge.asmx/LodgeSMSMessage';
77 }
78 0 my ($request) = new HTTP::Request('POST' => $url);
79 0 $request->content_type('application/x-www-form-urlencoded');
80 0 my ($ua) = $self->{_ua};
81 0 my ($username) = $self->{user_name};
82 0 my ($password) = $self->{password};
83 0 $request->content("Username=" . URI::Escape::uri_escape($username) . '&Pwd=' . URI::Escape::uri_escape($password) . '&PhoneNumber=' . URI::Escape::uri_escape($phone_number) . '&PhoneMessage=' . URI::Escape::uri_escape($message));
84 0 my ($response);
85 0 eval {
86 0 ▶ local $SIG{'ALRM'} = sub { die("Timeout\n"); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 alarm $ua->timeout();
88 0 $response = $ua->request($request);
89 0 alarm 0;
90 };
91 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
92 0 die("Failed to get a response from '$url':$@\n");
93 }
94 0 ▶ unless ($response->is_success()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
95 0 die("Failed to get a successful response from sms attempt\n");
96 }
97 0 my ($response_as_string) = $response->as_string();
98 0 ▶ if ($response_as_string =~ /<string[^>]+>([^<]+)<\/string>/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response_as_string =~ m[<string[^>]+>([^<]+)</string>]) { } 0 0 0
99 0 my ($actual_message) = $1;
100 0 ▶ if ($actual_message eq 'Message sent successfully.') {
Branch: 1 branch
BranchTrueFalseTotal
if ($actual_message eq 'Message sent successfully.') { } 0 0 0
101 } else {
102 0 die("Failed to send sms:$actual_message\n");
103 }
104 } else {
105 0 die("Unrecognisable response\n");
106 }
107 }
108  
109 0 ▶sub send_sms {
Subroutine send_sms: not called CC=1 SCAR=6.9
Pod documented
110 0 my ($user_name, $password, $phone_number, $message) = @_;
111 0 __PACKAGE__->new($user_name, $password)->send($phone_number, $message);
112 }
↑ Summary