blib/lib/Net/SMS/ArxMobile.pm

filter: Statementstmt 42.4% 42 / 99 Branchbran 6.2% 1 / 16 Conditioncond 8.3% 1 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 81.2% 13 / 16 Podpod 100% 3 / 3 totaltotal 38.9% 60 / 154 CC 13 SCAR 31.9
CC
13
Cov
35%
send_sms
72.0
query_smsid
20.0
new
3.6
CRAP 24.3
↑ Summary
1 package Net::SMS::ArxMobile;
2 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 $Net::SMS::ArxMobile::VERSION = '0.01';
4 }
5  
6 # ABSTRACT: Send SMS messages via the ArXMobile HTTP API
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 #se Data::Dumper ();
14 2 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use LWP::UserAgent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use XML::Simple ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 XML_DECL => q{<?xml version="1.0" ?>}, # Ugh
20 API_URL_SEND => q{https://invenue.com/api/message.php},
21 API_URL_QUERY => q{https://invenue.com/api/query.php},
22 API_TIMEOUT => 30,
23 2 };
24  
25 1 ▶sub new {
Subroutine new: called CC=3 SCAR=12.7
Pod documented
26 1 my ($class, %args) = @_;
27  
28 1 ▶ if (! exists $args{_auth_code} || ! $args{_auth_code}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"_auth_code"} and $args{"_auth_code"}) 0 1 1
Condition33%1/3
exists $args{"_auth_code"} and $args{"_auth_code"}
!ll&&!rl&&r
001
MC/DC0%0/2
exists $args{"_auth_code"} and $args{"_auth_code"}
exists $args{"_auth_code"} $args{"_auth_code"}
Truth table33%1/3
A and B
A= exists $args{"_auth_code"}B= $args{"_auth_code"}
ABresult
0X0
100
111
29 0 Carp::croak("${class}->new() requires the ArXMobile '_auth_code' parameter\n");
30 }
31  
32 1 my $self = \%args;
33 1 bless $self, $class;
34 }
35  
36 0 ▶sub _useragent {
Subroutine _useragent: not called CC=1 SCAR=6.9
37 0 my ($self) = @_;
38  
39 0 my $ua = LWP::UserAgent->new();
40 0 $ua->timeout(API_TIMEOUT);
41 0 $ua->agent("Net::SMS::ArxMobile/$Net::SMS::ArxMobile::VERSION");
42  
43 0 return $ua;
44 }
45  
46 1 ▶sub _query_smsid_xml {
Subroutine _query_smsid_xml: called CC=1 SCAR=0.0
47 1 my ($self, %args) = @_;
48  
49 1 my $smsid = $args{smsid};
50 1 my $auth_code = $args{_auth_code};
51  
52 1 my $xml = _to_xml({
53 query => {
54 auth_code => $auth_code,
55 smsid => $smsid,
56 },
57 });
58  
59 1 return $xml;
60 }
61  
62 1 ▶sub _send_sms_xml {
Subroutine _send_sms_xml: called CC=1 SCAR=0.0
63 1 my ($self, %args) = @_;
64  
65 1 my $body = $args{text};
66 1 my $phone = $args{to};
67 1 my $auth_code = $args{_auth_code};
68  
69 1 my $xml = _to_xml({
70 message => {
71 auth_code => $auth_code,
72 body => $body,
73 user => {
74 phone => $phone,
75 },
76 },
77 });
78  
79 1 return $xml;
80 }
81  
82 2 ▶sub _to_xml {
Subroutine _to_xml: called CC=1 SCAR=0.0
83 2 my ($data_struct) = @_;
84 2 my %xml_opts = _xml_out_opts();
85 2 return XML::Simple::XMLout($data_struct, %xml_opts);
86 }
87  
88 2 ▶sub _xml_out_opts {
Subroutine _xml_out_opts: called CC=1 SCAR=0.0
89 return (
90 2 KeepRoot => 1,
91 NoAttr => 1,
92 XMLDecl => XML_DECL,
93 );
94 }
95  
96 0 ▶sub query_smsid {
Subroutine query_smsid: not called CC=4 SCAR=30.0
Pod documented
97 0 my ($self, %args) = @_;
98  
99 0 my $ua = _useragent();
100 0 my $api_url = API_URL_QUERY;
101 my $req_xml = $self->_query_smsid_xml(
102 _auth_code => $self->{_auth_code},
103 smsid => $args{smsid},
104 0 );
105  
106 0 my $req = HTTP::Request->new(POST => $api_url);
107 0 $req->header("Content-Type" => "text/xml");
108 0 $req->content($req_xml);
109  
110 0 my $resp = $ua->request($req);
111 0 my $as_string = $resp->as_string;
112 0 my $resp_body = $resp->content;
113  
114 0 ▶ if (! $resp->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 0 0
115 0 my $status = $resp->status_line;
116 0 warn "HTTP request failed: $status\n$resp_body\n";
117 0 return;
118 }
119  
120 #warn "HTTP Request: " . $req->as_string . "\n";
121 #warn "HTTP Response: $as_string\n";
122 #warn "HTTP Response content: $resp_body\n";
123  
124 0 my $api_data = XML::Simple::XMLin($resp_body, SuppressEmpty => '');
125 0 ▶ if (! $api_data || ref $api_data ne "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (not $api_data or ref $api_data ne "HASH") 0 0 0
Condition0%0/3
not $api_data or ref $api_data ne "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $api_data or ref $api_data ne "HASH"
not $api_data ref $api_data ne "HASH"
Truth table0%0/3
not(A) or B
A= not $api_dataB= ref $api_data ne "HASH"
ABresult
1X1
011
000
126 0 warn "No result (or invalid XML?) from API\n";
127 0 return;
128 }
129  
130 0 my $sms_data = $api_data->{result};
131 0 return $sms_data;
132 }
133  
134 0 ▶sub send_sms {
Subroutine send_sms: not called CC=8 SCAR=42.8
Pod documented
135 0 my ($self, %args) = @_;
136  
137 0 $args{to} =~ s{^\+}{};
138 0 $args{to} =~ s{[- ]}{}g;
139  
140 0 my $ua = $self->_useragent();
141 0 my $api_url = API_URL_SEND;
142 my $req_xml = $self->_send_sms_xml(
143 _auth_code => $self->{_auth_code},
144 text => $args{text},
145 to => $args{to},
146 0 );
147  
148 0 my $req = HTTP::Request->new(POST => $api_url);
149 0 $req->header("Content-Type" => "text/xml");
150 0 $req->content($req_xml);
151  
152 0 my $resp = $ua->request($req);
153 0 my $as_string = $resp->as_string;
154  
155 #warn "HTTP Request: " . $req->as_string . "\n";
156 #warn "HTTP Response: $as_string\n";
157 #warn "HTTP Response content: " . $resp->content . "\n";
158  
159 0 ▶ if (! $resp->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 0 0
160 0 my $status = $resp->status_line;
161 0 warn "HTTP request failed: $status\n$as_string\n";
162 0 return 0;
163 }
164  
165 0 my $xml = $resp->content;
166 0 my $api_data = XML::Simple::XMLin($xml, SuppressEmpty => '');
167  
168 0 ▶ if (! $api_data || ref $api_data ne "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (not $api_data or ref $api_data ne "HASH") 0 0 0
Condition0%0/3
not $api_data or ref $api_data ne "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $api_data or ref $api_data ne "HASH"
not $api_data ref $api_data ne "HASH"
Truth table0%0/3
not(A) or B
A= not $api_dataB= ref $api_data ne "HASH"
ABresult
1X1
011
000
169 0 warn "No response (or invalid XML) from API\n";
170 0 return;
171 }
172  
173 #warn Data::Dumper::Dumper($api_data), "\n";
174  
175 0 my $sms = $api_data->{result};
176  
177 0 ▶ if (! $sms || ref $sms ne "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (not $sms or ref $sms ne "HASH") 0 0 0
Condition0%0/3
not $sms or ref $sms ne "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $sms or ref $sms ne "HASH"
not $sms ref $sms ne "HASH"
Truth table0%0/3
not(A) or B
A= not $smsB= ref $sms ne "HASH"
ABresult
1X1
011
000
178 0 warn "Couldn't find any sms result in the XML\n";
179 0 return;
180 }
181  
182 0 ▶ if ($sms->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sms->{"error"}) 0 0 0
183 0 warn "Failed: $sms->{error}\n";
184 0 return;
185 }
186  
187 0 my $smsid = $sms->{smsid};
188 0 ▶ if (! $smsid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($smsid) 0 0 0
189 0 warn "smsid not found in ArxMobile response:\n$xml\n";
190 }
191  
192 0 return $smsid;
193 }
194  
195 2 1;
196  
197  
198 __END__
↑ Summary