Microservice/Time.pm

filter: Statementstmt 39.3% 39 / 99 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 52.3% 11 / 21 Podpod 75.0% 6 / 8 totaltotal 31.8% 56 / 176 CC 19 SCAR 37.5
CC
19
Cov
28%
GET_datetime_span
132.0
POST_datetime
20.0
GET_sleep
20.0
CRAP 42.5
↑ Summary Microservice.pm »
1 package Async::Microservice::Time;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 with qw(Async::Microservice);
10  
11 1 our $VERSION = '0.09';
12  
13 1 has '+jsonp' => ( default => '_cb' );
14  
15 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use AnyEvent::Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 0 ▶sub service_name {
Subroutine service_name: not called CC=1 SCAR=6.9
Pod documented
22 0 return 'async-microservice-time';
23 }
24  
25 0 ▶sub get_routes {
Subroutine get_routes: not called CC=1 SCAR=6.9
Pod documented
26 return (
27 0 'datetime' => {
28 defaults => {
29 GET => 'GET_datetime',
30 POST => 'POST_datetime',
31 },
32 },
33 'datetime/:time_zone' => {
34 defaults => {
35 GET => 'GET_datetime',
36 POST => 'POST_datetime',
37 },
38 },
39 'datetime/:time_zone_part1/:time_zone_part2' => {
40 defaults => { GET => 'GET_datetime_capture', },
41 validations => {
42 time_zone_part1 => qr{^\w+$},
43 time_zone_part2 => qr{^\w+$},
44 },
45 },
46 'datetime/span/:s_date' =>
47 { defaults => { GET => 'GET_datetime_span' } },
48 'epoch' => { defaults => { GET => 'GET_epoch' } },
49 'sleep' => { defaults => { GET => 'GET_sleep' } },
50 );
51 }
52  
53 0 ▶sub GET_datetime {
Subroutine GET_datetime: not called CC=2 SCAR=17.9
Pod documented
54 0 my ( $self, $this_req ) = @_;
55 0 ▶ my $time_zone = $this_req->params->{time_zone} // 'UTC';
Condition0%0/2
$this_req->params->{"time_zone"} // "UTC"
l!l
00
MC/DC0%0/1
$this_req->params->{"time_zone"} // "UTC"
$this_req->params->{"time_zone"}
Truth table0%0/2
A
A= $this_req->params->{"time_zone"}
Aresult
11
00
56 0 return $self->_get_datetime_time_zone( $this_req, $time_zone );
57 }
58  
59 0 ▶sub GET_datetime_capture {
Subroutine GET_datetime_capture: not called CC=1 SCAR=6.9
Pod undocumented
60 0 my ( $self, $this_req, $match ) = @_;
61 my $time_zone = $this_req->params->{time_zone_part1} . '/'
62 0 . $this_req->params->{time_zone_part2};
63 0 return $self->_get_datetime_time_zone( $this_req, $time_zone );
64 }
65  
66 0 ▶sub _get_datetime_time_zone {
Subroutine _get_datetime_time_zone: not called CC=2 SCAR=17.9
67 0 my ( $self, $this_req, $time_zone ) = @_;
68 0 my $time_dt = eval { DateTime->now( time_zone => $time_zone ); };
69 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
70 return [
71 0 405,
72 [],
73 { err_status => 405,
74 err_msg => $@,
75 }
76 ];
77 }
78 0 return [ 200, [], _datetime_as_data($time_dt) ];
79 }
80  
81 0 ▶sub POST_datetime {
Subroutine POST_datetime: not called CC=4 SCAR=30.0
Pod documented
82 0 my ( $self, $this_req ) = @_;
83 0 my $epoch = eval { $this_req->json_content->{epoch} };
84 0 ▶ if ( !defined($epoch) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $epoch) 0 0 0
85 return [
86 0 ▶ 405,
Condition0%0/2
$@ || 'epoch data missing'
l!l
00
MC/DC0%0/1
$@ || 'epoch data missing'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
87 [],
88 { err_status => 405,
89 err_msg => $@ || 'epoch data missing',
90 }
91 ];
92 }
93 0 ▶ if ( $epoch !~ m/^-?[0-9]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($epoch =~ /^-?[0-9]+$/) 0 0 0
94 return [
95 0 405,
96 [],
97 { err_status => 405,
98 err_msg => 'epoch not a number',
99 }
100 ];
101 }
102 return [
103 0 200, [], _datetime_as_data( DateTime->from_epoch( epoch => $epoch ) )
104 ];
105 }
106  
107 0 ▶sub GET_epoch {
Subroutine GET_epoch: not called CC=1 SCAR=6.9
Pod documented
108 0 my ( $self, $this_req ) = @_;
109 0 return [ 200, [], { epoch => time() } ];
110 }
111  
112 0 ▶async sub GET_sleep {
Subroutine GET_sleep: not called CC=4 SCAR=30.0
Pod documented
113 0 my ( $self, $this_req ) = @_;
114 0 my $start_time = time();
115 0 ▶ my $sleep_time = ( $this_req->params->{duration} // rand(10) ) + 0;
Condition0%0/2
$this_req->params->{"duration"} // rand 10
l!l
00
MC/DC0%0/1
$this_req->params->{"duration"} // rand 10
$this_req->params->{"duration"}
Truth table0%0/2
A
A= $this_req->params->{"duration"}
Aresult
11
00
116 0 ▶ if ( ( $sleep_time <= 0 ) || ( $sleep_time > 60 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sleep_time <= 0 or $sleep_time > 60) 0 0 0
Condition0%0/3
$sleep_time <= 0 or $sleep_time > 60
l!l&&r!l&&!r
000
MC/DC0%0/2
$sleep_time <= 0 or $sleep_time > 60
$sleep_time <= 0 $sleep_time > 60
Truth table0%0/3
A or B
A= $sleep_time <= 0B= $sleep_time > 60
ABresult
1X1
011
000
117 return [
118 0 405,
119 [],
120 { err_status => 405,
121 err_msg => 'invalid sleep duration',
122 }
123 ];
124 }
125  
126 0 await AnyEvent::Future->new_delay( after => $sleep_time );
127 0 my $stop_time = time();
128 return [
129 0 200,
130 [],
131 { start => $start_time,
132 stop => $stop_time,
133 duration => ( $stop_time - $start_time ),
134 }
135 ];
136 1 }
137  
138 0 ▶async sub GET_datetime_span {
Subroutine GET_datetime_span: not called CC=11 SCAR=48.8
Pod undocumented
139 0 my ( $self, $this_req ) = @_;
140  
141 0 my $now = DateTime->now( time_zone => 'UTC' )->truncate( to => 'day' );
142 0 my $s_date_dt;
143  
144 0 ▶ my $s_date = $this_req->params->{s_date} // '';
Condition0%0/2
$this_req->params->{"s_date"} // ""
l!l
00
MC/DC0%0/1
$this_req->params->{"s_date"} // ""
$this_req->params->{"s_date"}
Truth table0%0/2
A
A= $this_req->params->{"s_date"}
Aresult
11
00
145 0 ▶ if ( $s_date =~ m/^([0-9]{4})-?([0-9]{2})-?([0-9]{2})$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($s_date =~ /^([0-9]{4})-?([0-9]{2})-?([0-9]{2})$/) { } 0 0 0
elsif ($s_date eq 'now') { } 0 0 0
146 0 $s_date_dt = eval {
147 0 DateTime->new(
148 time_zone => 'UTC',
149 day => $3,
150 month => $2,
151 year => $1
152 );
153 };
154 }
155 elsif ( $s_date eq 'now' ) {
156 0 $s_date_dt = $now->clone;
157 }
158 0 ▶ unless ($s_date_dt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s_date_dt) 0 0 0
159 return [
160 0 405,
161 [],
162 { err_status => 405,
163 err_msg => 'invalid date format '
164 . $s_date
165 . ', use YYYYMMDD or "now"',
166 }
167 ];
168 }
169 0 ▶ my $r_age = int( $this_req->params->{r_age} // 65 );
Condition0%0/2
$this_req->params->{"r_age"} // 65
l!l
00
MC/DC0%0/1
$this_req->params->{"r_age"} // 65
$this_req->params->{"r_age"}
Truth table0%0/2
A
A= $this_req->params->{"r_age"}
Aresult
11
00
170 0 ▶ unless ( $r_age >= 1 && $r_age <= 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r_age >= 1 and $r_age <= 200) 0 0 0
Condition0%0/3
$r_age >= 1 and $r_age <= 200
!ll&&!rl&&r
000
MC/DC0%0/2
$r_age >= 1 and $r_age <= 200
$r_age >= 1 $r_age <= 200
Truth table0%0/3
A and B
A= $r_age >= 1B= $r_age <= 200
ABresult
0X0
100
111
171 return [
172 0 405,
173 [],
174 { err_status => 405,
175 err_msg => 'invalid age',
176 }
177 ];
178 }
179 0 ▶ my $m_income = int( $this_req->params->{m_income} // 0 );
Condition0%0/2
$this_req->params->{"m_income"} // 0
l!l
00
MC/DC0%0/1
$this_req->params->{"m_income"} // 0
$this_req->params->{"m_income"}
Truth table0%0/2
A
A= $this_req->params->{"m_income"}
Aresult
11
00
180  
181 0 my $r_age_dt = $s_date_dt->clone->add( years => $r_age );
182 0 ▶ $r_age_dt = $now->clone
Branch: 1 branch
BranchTrueFalseTotal
if $r_age_dt < $now 0 0 0
183 if $r_age_dt < $now;
184  
185 0 my $counter_dt = $r_age_dt->subtract_datetime($now);
186 0 my $days_counter = $r_age_dt->delta_days($now)->in_units('days');
187 0 my $week_counter = int( $days_counter / 7 );
188 0 my $months_counter = $counter_dt->in_units('months');
189 0 my $years_counter = $counter_dt->in_units('years');
190 0 my $date_from_str = $s_date_dt->strftime('%Y-%m-%d');
191 0 my $date_to_str = $r_age_dt->strftime('%Y-%m-%d');
192  
193 return {
194 0 ▶ msg => sprintf(
Branch: 1 branch
BranchTrueFalseTotal
$m_income ? : 0 0 0
195 '%d weeks left until date span age of %d (date from %s to %s)',
196 $week_counter, $r_age, $date_from_str, $date_to_str,
197 ),
198 days => $days_counter,
199 weeks => $week_counter,
200 months => $months_counter,
201 years => $years_counter,
202 date_from => $date_from_str,
203 date_to => $date_to_str,
204 ( $m_income ? ( income => $months_counter * $m_income ) : () ),
205 };
206 1 }
207  
208 0 ▶sub _datetime_as_data {
Subroutine _datetime_as_data: not called CC=1 SCAR=6.9
209 0 my ($dt) = @_;
210 return {
211 0 datetime => $dt->strftime('%Y-%m-%d %H:%M:%S %z'),
212 date => $dt->strftime('%Y-%m-%d'),
213 time => $dt->strftime('%H:%M:%S'),
214 time_zone => $dt->strftime('%z'),
215 time_zone_name => $dt->strftime('%Z'),
216 day => $dt->strftime('%d'),
217 month => $dt->strftime('%m'),
218 year => $dt->strftime('%Y'),
219 hour => $dt->strftime('%H'),
220 minute => $dt->strftime('%M'),
221 second => $dt->strftime('%S'),
222 epoch => $dt->epoch,
223 };
224 }
225  
226 1 __PACKAGE__->meta->make_immutable;
227  
228 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
229  
230 1 1;
231  
232 __END__
↑ Summary Microservice.pm »