blib/lib/SMS/Send/CZ/Smseagle.pm

filter: Statementstmt 59.4% 47 / 79 Branchbran 14.2% 2 / 14 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 92.8% 13 / 14 Podpod 100% 3 / 3 totaltotal 56.0% 65 / 116 CC 10 SCAR 32.5
CC
10
Cov
51%
send_sms
72.0
log
2.3
new
2.0
CRAP 25.9
↑ Summary
1 #!/usr/bin/perl
2  
3 package SMS::Send::CZ::Smseagle;
4  
5 # ABSTRACT: SMS::Send driver for SMSEagle - Czech Republic
6  
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = "1.001";
12 2 $VERSION = eval $VERSION;
13  
14 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use DateTime qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use base 'SMS::Send::Driver';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Log::LogLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Text::Unidecode;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
24 1 my $class = shift;
25 1 my %params = @_;
26  
27 1 my $LOG_FILE = "/var/log/smseagle.log";
28 1 my $ERROR_LOG_LEVEL = 6;
29  
30 1 open HANDLE, ">>$LOG_FILE";
31 1 close HANDLE;
32  
33 # Create our LWP::UserAgent object
34 1 my $ua = LWP::UserAgent->new;
35  
36 # Create the object, saving any private params for later
37 my $self = bless {
38 ua => $ua,
39 login => $params{_login},
40 password => $params{_password},
41 api_url => $params{_api_url},
42 1 ▶ private => \%params,
Branch: 1 branch
BranchTrueFalseTotal
-w $LOG_FILE ? : 0 1 1
43 log => (-w $LOG_FILE) ? new Log::LogLite($LOG_FILE, $ERROR_LOG_LEVEL) : 0
44 }, $class;
45 1 $self->log("Driver Smseagle created", 4);
46
47 1 $self;
48 }
49  
50 1 ▶sub log {
Subroutine log: called CC=2 SCAR=8.1
Pod documented
51 1 my ($self, $msg, $level) = @_;
52  
53 1 ▶ if ($self->{'log'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"log"}) 0 1 1
54 0 $self->{'log'}->write($msg, $level);
55 }
56 }
57  
58 0 ▶sub send_sms {
Subroutine send_sms: not called CC=8 SCAR=42.8
Pod documented
59 0 my ($self, %args) = @_;
60 0 my $url = $self->{'api_url'};
61
62 0 $args{'text'} = unidecode($args{'text'});
63 0 $self->log("TEXT: " . $args{'text'} . ", TO: " . $args{'to'}, 4);
64  
65 my %params = (
66 'to' => $args{'to'} || '',
67 'message' => $args{'text'} || '',
68 'login' => $self->{'login'},
69 0 ▶ 'pass' => $self->{'password'},
Condition0%0/4
$args{'to'} || ''
l!l
00
$args{'text'} || ''
l!l
00
MC/DC0%0/1
$args{'to'} || ''
$args{'to'}
MC/DC0%0/1
$args{'text'} || ''
$args{'text'}
Truth table0%0/2
A
A= $args{'to'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'text'}
Aresult
11
00
70 'responsetype' => 'xml'
71 );
72  
73 # cleanup
74 0 $params{'to'} =~ s{\D}{}g; # remove non-digits
75 0 ▶ if (length($params{'to'}) == 9) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $params{"to"} == 9) 0 0 0
76 0 $params{'to'} = '00420' . $params{'to'};
77 0 $self->log("Auto-prefix: " . $args{'to'} . " => " . $params{'to'}, 4);
78 }
79
80 # send away
81 0 my $uri = join( '&', map { $_ . '=' . uri_escape_utf8( $params{ $_ } ) } keys %params );
82
83 0 my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
84 0 $ua->protocols_allowed( ['https'] );
85  
86 0 my $res = $ua->get($url . "?" . $uri);
87  
88 0 ▶ if( $res->{'_rc'} == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{'_rc'} == 200) { } 0 0 0
89 0 $params{'password'} = "------"; # hide password in logs
90 0 my $successLog = $url . "?" . join( '&', map { $_ . '=' . uri_escape_utf8( $params{ $_ } ) } keys %params );
91 0 $self->log("HTTP SUCCESS: " . $successLog, 4);
92
93 0 my $parser = new XML::Simple;
94 0 my $data = $parser->XMLin($res->decoded_content);
95
96 0 ▶ if ($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) 0 0 0
97 0 my $logMsg;
98 0 my $result = 0;
99
100 0 ▶ if ($data->{'status'} eq 'ok' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data->{'status'} eq 'ok') { } 0 0 0
elsif ($data->{'status'} eq 'error') { } 0 0 0
101 0 $logMsg = "SMS #" . $data->{'message_id'} . " sent";
102 0 $result = 1;
103 }
104 elsif ($data->{'status'} eq 'error') {
105 0 $logMsg = "SMS processing error: " . $data->{'error_text'};
106 }
107
108 0 $self->log($logMsg, 4);
109
110 0 return $result;
111 }
112 }
113 else {
114 0 return 0;
115 }
116 }
117  
118 __END__
↑ Summary