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

filter: Statementstmt 83.0% 49 / 59 Branchbran 40.0% 4 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 80.7% 67 / 83 SCAR 18.6
CC
6
Cov
77%
send_sms
6.0
log
2.3
new
2.0
CRAP 6.4
« Build ↑ Summary
1 #!/usr/bin/perl
2  
3 package SMS::Send::CZ::Konzulta;
4  
5 # ABSTRACT: SMS::Send driver for Konzulta - 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.000";
12 2 $VERSION = eval $VERSION;
13  
14 2 ▶use base 'SMS::Send::Driver';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Log::LogLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use DateTime qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 sub new {
21 1 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
22 1 my %params = @_;
23  
24 1 my $LOG_FILE = "/var/log/konzulta.log";
25 1 my $ERROR_LOG_LEVEL = 6;
26  
27 1 open HANDLE, ">>$LOG_FILE";
28 1 close HANDLE;
29  
30 1 my $dt = DateTime->now(time_zone => 'Europe/Prague');
31 my $self = bless {
32 login => $params{_login},
33 password => $params{_password},
34 1 ▶ stamp => $dt->strftime('%Y%m%dT%H%M%S'),
Branch: 1 branch
BranchTrueFalseTotal
-w $LOG_FILE ? : 0 1 1
35 log => (-w $LOG_FILE) ? new Log::LogLite($LOG_FILE, $ERROR_LOG_LEVEL) : 0
36 }, $class;
37  
38 1 $self->log("Driver Konzulta created", 4);
39
40 1 $self;
41 }
42  
43 sub log {
44 4 ▶ my ($self, $msg, $level) = @_;
Subroutine log: called CC=2 SCAR=8.1
Pod documented
45  
46 4 ▶ if ($self->{'log'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"log"}) 0 4 4
47 0 $self->{'log'}->write($msg, $level);
48 }
49 }
50  
51 sub send_sms {
52 1 ▶ my ($self, %args) = @_;
Subroutine send_sms: called CC=4 SCAR=17.9
Pod documented
53 1 my $url = 'https://www.sms-operator.cz/webservices/webservice.aspx';
54
55 1 $self->log("TEXT: " . $args{'text'} . ", TO: " . $args{'to'}, 4);
56
57 1 my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
58 1 my $message = "<SmsServices>
59 <DataHeader>
60 <DataType>SMS</DataType>
61 <UserName>$self->{'login'}</UserName>
62 <Password>$self->{'password'}</Password>
63 </DataHeader>
64 <DataArray>
65 <DataTemplate>
66 <Text>$args{'text'}</Text>
67 <DataItem>
68 <MobileTerminate>$args{'to'}</MobileTerminate>
69 <SmsId>$self->{'stamp'}</SmsId>
70 </DataItem>
71 </DataTemplate>
72 </DataArray>
73 </SmsServices>
74 ";
75
76 1 my $res = $ua->post($url, Content_Type => 'text/xml', Content => $message);
77  
78 1 ▶ if( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 1 0 1
79 1 $self->log("HTTP SUCCESS", 4);
80 1 ▶ if ( $res->decoded_content ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->decoded_content) { } 0 1 1
81 0 my $parser = new XML::Simple;
82 0 my $data = $parser->XMLin($res->decoded_content);
83 0 ▶ if ($data->{'DataArray'}->{'DataItem'}->{'Status'} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{'DataArray'}{'DataItem'}{'Status'} == 0) { } 0 0 0
84 0 $self->log("SMS #" . $data->{'DataArray'}->{'DataItem'}->{'SmsId'} . " sent", 4);
85
86 0 return 1;
87 }
88 else {
89 0 $self->log("SMS processing error: " . $data->{'DataArray'}->{'DataItem'}->{'Status'}, 4);
90 0 return 0;
91 }
92 }
93 else {
94 1 $self->log("SMS processing error: invalid credentials?", 4);
95 1 return 0;
96 }
97 }
98 else {
99 0 $self->log("HTTP error #" . $res->code() . ": " . $res->message(), 4);
100 0 return 0;
101 }
102 }
103  
104 __END__
105  
106 =pod
107  
108 =encoding UTF-8
109  
110 =head1 NAME
111  
112 SMS::Send::CZ::Konzulta - SMS::Send driver for Konzulta - Czech Republic
113  
114 =head1 VERSION
115  
116 version 1.000
117  
118 =head1 SYNOPSIS
119  
120 use SMS::Send;
121  
122 my $sender = SMS::Send->new('CZ::Konzulta',
123 _login => 'who',
124 _password => 'secret',
125 );
126
127 my $sent = $sender->send_sms(
128 text => 'Test SMS',
129 to => '604111111',
130 );
131
132 # Did it send?
133 if ( $sent ) {
134 print "Sent test message\n";
135 } else {
136 print "Test message failed\n";
137 }
138  
139 =head1 METHODS
140  
141 =head2 log
142  
143 Logs message to /var/log/konzulta.log if this file is accessible and writable
144  
145 =head2 send_sms
146  
147 Sends the message using provider's API at https://www.sms-operator.cz/webservices/webservice.aspx and takes additional arguments:
148 'text' containgin the message itself and 'to' with recipient's number.
149  
150 Processing information is automatically logged to /var/log/konzulta.log to allow tracking of possible problems.
151  
152 Returns true if the msssage was successfully sent
153  
154 Returns false if an error occured
155  
156 =cut
157  
158 =head1 AUTHOR
159  
160 Radek Šiman <rbit@rbit.cz>
161  
162 =head1 COPYRIGHT AND LICENSE
163  
164 This software is copyright (c) 2017 by R-Bit Technology, s.r.o.
165  
166 This is free software; you can redistribute it and/or modify it under
167 the same terms as the Perl 5 programming language system itself.
168  
169 =cut
« Build ↑ Summary