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

filter: Statementstmt 90.2% 65 / 72 Branchbran 42.8% 6 / 14 Conditioncond 28.5% 2 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 78.7% 89 / 113 CC 11 SCAR 18.2
CC
11
Cov
78%
send_sms
13.7
log
2.3
new
2.0
CRAP 6.2
↑ Summary Build »
1 #!/usr/bin/perl
2  
3 package SMS::Send::CZ::Bulkgate;
4  
5 # ABSTRACT: SMS::Send driver for Bulkgate - 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 = "2.000";
12 2 $VERSION = eval $VERSION;
13  
14 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use IO::Socket::SSL;
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 Text::Unidecode;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 sub new {
23 1 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
24 1 my %params = @_;
25  
26 1 my $LOG_FILE = "/var/log/bulkgate.log";
27 1 my $ERROR_LOG_LEVEL = 6;
28  
29 1 open HANDLE, ">>$LOG_FILE";
30 1 close HANDLE;
31  
32 # Create our LWP::UserAgent object
33 1 my $ua = LWP::UserAgent->new;
34  
35 # Create the object, saving any private params for later
36 my $self = bless {
37 ua => $ua,
38 login => $params{_login},
39 password => $params{_password},
40 1 ▶ private => \%params,
Branch: 1 branch
BranchTrueFalseTotal
-w $LOG_FILE ? : 0 1 1
41 log => (-w $LOG_FILE) ? new Log::LogLite($LOG_FILE, $ERROR_LOG_LEVEL) : 0
42 }, $class;
43 1 $self->log("Driver Bulkgate created", 4);
44
45 1 $self;
46 }
47  
48 sub log {
49 4 ▶ my ($self, $msg, $level) = @_;
Subroutine log: called CC=2 SCAR=8.1
Pod documented
50  
51 4 ▶ if ($self->{'log'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"log"}) 0 4 4
52 0 $self->{'log'}->write($msg, $level);
53 }
54 }
55  
56 sub send_sms {
57 1 ▶ my ($self, %args) = @_;
Subroutine send_sms: called CC=9 SCAR=26.2
Pod documented
58 1 my $url = 'https://portal.bulkgate.com/api/1.0/simple/transactional';
59
60 1 $args{'text'} = unidecode($args{'text'});
61 1 $self->log("TEXT: " . $args{'text'} . ", TO: " . $args{'to'}, 4);
62  
63 # example: 1111:gOwn:420111222333
64 1 my @id = split(':', $self->{'login'});
65 1 my $app_id = $id[0];
66 1 ▶ my $sender_id = defined $id[1] ? $id[1] : 'gText';
Branch: 1 branch
BranchTrueFalseTotal
defined $id[1] ? : 0 1 1
67 1 ▶ my $sender_id_value = defined $id[2] ? $id[2] : 'BulkGate';
Branch: 1 branch
BranchTrueFalseTotal
defined $id[2] ? : 0 1 1
68  
69 my %params = (
70 'application_id' => $id[0],
71 'application_token' => $self->{'password'},
72 'number' => $args{'to'} || '',
73 1 ▶ 'text' => $args{'text'} || '',
Condition50%2/4
$args{'to'} || ''
l!l
10
$args{'text'} || ''
l!l
10
MC/DC0%0/1
$args{'to'} || ''
$args{'to'}
MC/DC0%0/1
$args{'text'} || ''
$args{'text'}
Truth table50%1/2
A
A= $args{'to'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'text'}
Aresult
11
00
74 'unicode' => 0,
75 'sender_id' => $sender_id,
76 'sender_id_value' => $sender_id_value
77 );
78  
79 # cleanup
80 1 $params{'number'} =~ s{\D}{}g; # remove non-digits
81 1 ▶ if (length($params{'number'}) == 9) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $params{"number"} == 9) 1 0 1
82 1 $params{'number'} = '420' . $params{'number'};
83 1 $self->log("Auto-prefix: " . $args{'to'} . " => " . $params{'number'}, 4);
84 }
85
86 # send away
87 IO::Socket::SSL::set_ctx_defaults(
88 1 SSL_verifycn_scheme => 'www',
89 SSL_verify_mode => 0,
90 );
91  
92 1 my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
93 1 $ua->protocols_allowed( ['https'] );
94 1 my $res = $ua->post($url, \%params );
95  
96 1 ▶ if( $res->{'_rc'} == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{'_rc'} == 200) { } 0 1 1
97 0 my $json = decode_json($res->{'_content'});
98 0 ▶ if (defined $json->{data}->{status} && $json->{data}->{status} eq 'accepted') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $json->{'data'}{'status'} and $json->{'data'}{'status'} eq 'accepted') { } 0 0 0
Condition0%0/3
defined $json->{"data"}{"status"} and $json->{"data"}{"status"} eq "accepted"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $json->{"data"}{"status"} and $json->{"data"}{"status"} eq "accepted"
defined $json->{"data"}{"status"} $json->{"data"}{"status"} eq "accepted"
Truth table0%0/3
A and B
A= defined $json->{"data"}{"status"}B= $json->{"data"}{"status"} eq "accepted"
ABresult
0X0
100
111
99 0 $self->log("SMS sent to : " . $args{'to'} . ", text: " . $args{'text'}, 4);
100 0 return 1;
101 }
102 else {
103 0 $self->log("Unexpected response from SMS provider: " . $res->{'_content'}, 4);
104 0 return 0;
105 }
106 }
107 else {
108 1 my $json = decode_json($res->{'_content'});
109 1 $self->log("Error " . $res->{'_rc'} . ": " . $json->{error}, 4);
110 1 return 0;
111 }
112 }
113  
114 __END__
115  
116 =pod
117  
118 =encoding UTF-8
119  
120 =head1 NAME
121  
122 SMS::Send::CZ::Bulkgate - SMS::Send driver for Bulkgate - Czech Republic
123  
124 =head1 VERSION
125  
126 version 2.000
127  
128 =head1 SYNOPSIS
129  
130 use SMS::Send;
131  
132 # see https://help.bulkgate.com/docs/cs/http-simple-transactional.html
133 my $sender = SMS::Send->new('CZ::Bulkgate',
134 _login => '1111:gOwn:420111222333',
135 _password => 'secret',
136 );
137
138 my $sent = $sender->send_sms(
139 text => 'Test SMS',
140 to => '604111111',
141 );
142
143 # Did it send?
144 if ( $sent ) {
145 print "Sent test message\n";
146 } else {
147 print "Test message failed\n";
148 }
149  
150 =head1 METHODS
151  
152 =head2 log
153  
154 Logs message to /var/log/bulkgate.log if this file is accessible and writable
155  
156 =head2 send_sms
157  
158 Sends the message using prividers API at https://api.bulkgate.com/http and takes additional arguments:
159 'text' containgin the message itself and 'to' with recipient's number.
160  
161 Processing information is automatically logged to /var/log/bulkgate.log to allow tracking of possible problems.
162  
163 Returns true if the msssage was successfully sent
164  
165 Returns false if an error occured
166  
167 =cut
168  
169 =head1 AUTHOR
170  
171 Radek Šiman <rbit@rbit.cz>
172  
173 =head1 COPYRIGHT AND LICENSE
174  
175 This software is copyright (c) 2022 by R-Bit Technology, s.r.o.
176  
177 This is free software; you can redistribute it and/or modify it under
178 the same terms as the Perl 5 programming language system itself.
179  
180 =cut
↑ Summary Build »