DAIA/Availability.pm

filter: Statementstmt 87.0% 121 / 139 Branchbran 71.1% 64 / 90 Conditioncond 65.3% 32 / 49 MC/DCmcdc 43.3% 13 / 30 Subroutinesub 94.1% 16 / 17 Podpod 100% 5 / 5 totaltotal 76.0% 251 / 330 CC 66 SCAR 31.6
CC
66
Cov
78%
rdfhash
90.0
normalize_duration
30.6
_buildargs
13.1
CRAP 23.5
« DAIA/Object.pm ↑ Summary DAIA/Response.pm »
1 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package DAIA::Availability;
4 #ABSTRACT: Abstract base class of availability information
5 12 our $VERSION = '0.43'; #VERSION
6  
7 12 ▶use base 'DAIA::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 12 ▶use Carp::Clan;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use Data::Validate::URI qw(is_uri);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use DateTime::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use DateTime::Format::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 12 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 our @EXPORT_OK = qw(parse_duration normalize_duration date_or_datetime);
17  
18  
19 our %PROPERTIES = (
20 service => {
21 # default => sub { croak 'DAIA::Availability->service is required' },
22 2 ▶ default => sub { undef }, # TODO: configure whether mandatory
Subroutine __ANON__: called CC=1 SCAR=0.0
23 32 ▶ filter => sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
24 32 my $s = $_[0];
25 32 ▶ return $s if $DAIA::Availability::SERVICES{$s};
Branch: 1 branch
BranchTrueFalseTotal
if $DAIA::Availability::SERVICES{$s} 26 6 32
26 6 ▶ return $DAIA::Availability::SECIVRES{$s} if $DAIA::Availability::SECIVRES{$s};
Branch: 1 branch
BranchTrueFalseTotal
if $DAIA::Availability::SECIVRES{$s} 5 1 6
27 0 ▶ return $s if is_uri($s); return;
Branch: 1 branch
BranchTrueFalseTotal
if is_uri($s) 1 0 1
28 }
29 },
30 href => $DAIA::Object::COMMON_PROPERTIES{href},
31 message => $DAIA::Object::COMMON_PROPERTIES{message},
32 12 limitation => {
33 type => 'DAIA::Limitation',
34 repeatable => 1,
35 }
36 );
37  
38 # known services
39 12 our %SERVICES = (
40 'presentation' => 'http://purl.org/ontology/daia/Service/Presentation',
41 'loan' => 'http://purl.org/ontology/daia/Service/Loan',
42 'interloan' => 'http://purl.org/ontology/daia/Service/Interloan',
43 'openaccess' => 'http://purl.org/ontology/daia/Service/Openaccess',
44 );
45  
46 our %SECIVRES = (
47 12 map { $SERVICES{$_} => $_ } keys %SERVICES
48 );
49  
50  
51 31 ▶sub _buildargs {
Subroutine _buildargs: called CC=13 SCAR=25.7
52 31 my $self = shift;
53 31 my %args = ();
54  
55 31 ▶ if ( not (@_ % 2) ) { # even number
Branch: 2 branches
BranchTrueFalseTotal
if (not @_ % 2) { } 13 18 31
elsif (@_) { } 18 0 18
56 13 %args = @_;
57 13 ▶ if ( not defined $args{status} ) { # $service => $status
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"status"}) 11 2 13
58 11 foreach ( keys %DAIA::Availability::SERVICES ) {
59 44 ▶ if ( defined $args{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{$_}) 3 41 44
60 3 $args{status} = $args{$_};
61 3 $args{service} = $_;
62 3 delete $args{$_};
63 }
64 }
65 }
66 } elsif ( @_ ) { # non empty, uneven number
67 18 ▶ if ( @_ == 1 and UNIVERSAL::isa( $_[0], 'DAIA::Availability' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and UNIVERSAL::isa($_[0], 'DAIA::Availability')) { } 3 15 18
elsif ($DAIA::Availability::SERVICES{$_[0]} or is_uri($_[0])) { } 15 0 15
Condition100%3/3
@_ == 1 and UNIVERSAL::isa($_[0], "DAIA::Availability")
!ll&&!rl&&r
3123
MC/DC100%2/2
@_ == 1 and UNIVERSAL::isa($_[0], "DAIA::Availability")
@_ == 1 UNIVERSAL::isa($_[0], "DAIA::Availability")
Truth table100%3/3
A and B
A= @_ == 1B= UNIVERSAL::isa($_[0], "DAIA::Availability")
ABresult
0X0
100
111
68 3 %args = %{ $_[0]->struct };
69 3 $self = $_[0];
70 ▶ } elsif ( $DAIA::Availability::SERVICES{$_[0]} or is_uri($_[0]) ) {
Condition66%2/3
$DAIA::Availability::SERVICES{$_[0]} or is_uri($_[0])
l!l&&r!l&&!r
1050
MC/DC0%0/2
$DAIA::Availability::SERVICES{$_[0]} or is_uri($_[0])
$DAIA::Availability::SERVICES{$_[0]} is_uri($_[0])
Truth table66%2/3
A or B
A= $DAIA::Availability::SERVICES{$_[0]}B= is_uri($_[0])
ABresult
1X1
011
000
71 15 %args = ( service => @_ );
72 } else {
73 0 croak( "could not parse parameters to " . ref($self) );
74 }
75 }
76
77 31 ▶ if ( not defined $args{status} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"status"}) 26 5 31
78 26 ▶ if ( ref($self) eq 'DAIA::Available' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self eq 'DAIA::Available') { } 17 9 26
elsif (ref $self eq 'DAIA::Unavailable') { } 7 2 9
79 17 $args{status} = 1;
80 } elsif ( ref($self) eq 'DAIA::Unavailable' ) {
81 7 $args{status} = 0;
82 }
83 }
84  
85 31 return %args;
86 }
87  
88  
89 39 ▶sub status {
Subroutine status: called CC=5 SCAR=16.1
Pod documented
90 39 my $self = shift;
91 39 my $class = ref($self);
92 39 my $status;
93  
94 39 ▶ if ( @_ > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0) { } 35 4 39
95 35 $status = shift;
96 35 ▶ if ( $status ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) { } 23 12 35
97 23 ▶ if ( $class eq 'DAIA::Unavailable' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "DAIA::Unavailable") 2 21 23
98 2 $self->expected( undef );
99 2 $self->queue( undef );
100 }
101 23 bless $self, 'DAIA::Available';
102  
103 } else {
104 12 ▶ if ( $class eq 'DAIA::Available' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "DAIA::Available") 2 10 12
105 2 $self->delay( undef );
106 }
107 12 bless $self, 'DAIA::Unavailable';
108 }
109 } else {
110 4 $status = $class eq 'DAIA::Available';
111 }
112  
113 39 return $status;
114 }
115  
116 0 ▶sub rdfhash {
Subroutine rdfhash: not called CC=9 SCAR=45.0
Pod documented
117 0 my $self = shift;
118 0 my $me = { };
119  
120 my $servicetype = $DAIA::Availability::SERVICES{ $self->{service} }
121 0 ▶ || $self->{service};
Condition0%0/3
$DAIA::Availability::SERVICES{$self->{'service'}} || $self->{'service'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$DAIA::Availability::SERVICES{$self->{'service'}} || $self->{'service'}
$DAIA::Availability::SERVICES{$self->{'service'}} $self->{'service'}
Truth table0%0/3
A || B
A= $DAIA::Availability::SERVICES{$self->{'service'}}B= $self->{'service'}
ABresult
1X1
011
000
122  
123 0 ▶ $me->{'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'} = [{
Branch: 1 branch
BranchTrueFalseTotal
if $servicetype 0 0 0
124 value => $servicetype, type => "uri"
125 }] if $servicetype;
126  
127 $me->{'http://xmlns.com/foaf/0.1/page'} = [{
128 value => $self->{href}, type => "uri"
129 0 ▶ }] if $self->{href};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"href"} 0 0 0
130  
131 $me->{'http://purl.org/dc/terms/description'} = [
132 0 map { $_->rdfhash } @{$self->{message}}
133 0 ▶ ] if $self->{message};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"message"} 0 0 0
134  
135 $me->{'http://purl.org/ontology/daia/expected'} = [{
136 value => $self->{expected}, type => 'literal',
137 # xsd:date or xsd:dateTime to 'unknown'
138 0 ▶ }] if $self->{expected};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"expected"} 0 0 0
139  
140 $me->{'http://purl.org/ontology/daia/delay'} = [{
141 value => $self->{delay}, type => 'literal',
142 # TODO: how to treat delay = "unknown"?
143 # datatype => 'http://www.w3.org/2001/XMLSchema#duration',
144 0 ▶ }] if $self->{delay};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"delay"} 0 0 0
145  
146 $me->{'http://purl.org/ontology/daia/delay'} = [{
147 value => $self->{queue}, type => 'literal',
148 datatype => 'http://www.w3.org/2001/XMLSchema#integer',
149 0 ▶ }] if defined $self->{queue};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"queue"} 0 0 0
150  
151 0 ▶ if ($self->{limitation}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"limitation"}) 0 0 0
152 # TODO
153 # some limitations may be literal, some may be uri/blank
154 # 'limitedBy'
155 }
156
157 0 my $rdf = { };
158  
159 0 $rdf->{ $self->rdfuri } = $me;
160  
161 0 return $rdf;
162 }
163  
164  
165 4 ▶sub parse_duration {
Subroutine parse_duration: called CC=12 SCAR=25.5
Pod documented
166 4 ▶ return $_[0] if UNIVERSAL::isa( $_[0], 'DateTime::Duration' );
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], "DateTime::Duration") 0 4 4
167 4 my $duration = "$_[0]";
168  
169 4 my ($neg, $year, $mounth, $day, $hour, $min, $sec, $fsec);
170 4 ▶ if ($duration =~ /^(-)?
Branch: 1 branch
BranchTrueFalseTotal
if ($duration =~ /^(-)? P ((\d+)Y)? ((\d+)M)? ((\d+)D)? ( T ((\d+)H)? ((\d+)M)? (((\d+)(\.(\d+))?)S)? )? $/x) { } 4 0 4
171 P
172 ((\d+)Y)?
173 ((\d+)M)?
174 ((\d+)D)?
175 (
176 T
177 ((\d+)H)?
178 ((\d+)M)?
179 (((\d+)(\.(\d+))?)S)?
180 )?
181 $/x) {
182 4 ($neg, $year, $mounth, $day, $hour, $min, $sec, $fsec) =
183 ($1, $3, $5, $7, $10, $12, $15, $17);
184 4 ▶ return unless (grep {defined} ($year, $mounth, $day, $hour, $min, $sec));
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $_;} $year, $mounth, $day, $hour, $min, $sec 0 4 4
185 } else {
186 0 return;
187 }
188 4 ▶ $duration = DateTime::Duration->new(
Branch: 1 branch
BranchTrueFalseTotal
$fsec ? : 0 4 4
Condition91%11/12
$year || 0
l!l
13
$mounth || 0
l!l
13
$day || 0
l!l
31
$hour || 0
l!l
13
$min || 0
l!l
13
$sec || 0
l!l
04
MC/DC100%1/1
$year || 0
$year
MC/DC100%1/1
$mounth || 0
$mounth
MC/DC100%1/1
$day || 0
$day
MC/DC100%1/1
$hour || 0
$hour
MC/DC100%1/1
$min || 0
$min
MC/DC0%0/1
$sec || 0
$sec
Truth table100%2/2
A
A= $year
Aresult
11
00
Truth table100%2/2
A
A= $mounth
Aresult
11
00
Truth table100%2/2
A
A= $day
Aresult
11
00
Truth table100%2/2
A
A= $hour
Aresult
11
00
Truth table100%2/2
A
A= $min
Aresult
11
00
Truth table50%1/2
A
A= $sec
Aresult
11
00
189 years => $year || 0,
190 months => $mounth || 0,
191 days => $day || 0,
192 hours => $hour || 0,
193 minutes => $min || 0,
194 seconds => $sec || 0,
195 nanoseconds => ($fsec ? "0.$fsec" * 1E9 : 0),
196 );
197 4 ▶ $duration = $duration->inverse if $neg;
Branch: 1 branch
BranchTrueFalseTotal
if $neg 1 3 4
198 4 return $duration;
199 }
200  
201  
202 4 ▶sub normalize_duration {
Subroutine normalize_duration: called CC=23 SCAR=34.2
Pod documented
203 4 my $duration = $_[0];
204 4 ▶ $duration = parse_duration( $duration )
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($duration, "DateTime::Duration") 3 1 4
205 unless UNIVERSAL::isa( $duration, 'DateTime::Duration' );
206 4 ▶ return unless defined $duration;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $duration 0 4 4
207  
208 4 ▶ return "P0D" if $duration->is_zero;
Branch: 1 branch
BranchTrueFalseTotal
if $duration->is_zero 1 3 4
209  
210 # TODO: replace this
211 3 my $fmt = DateTime::Format::Duration->new(
212 pattern => '%PP%YY%mM%dDT%HH%MM%S.%NS',
213 normalize => 1,
214 );
215  
216 3 my %d = $fmt->normalize( $duration );
217 3 ▶ if (exists $d{seconds} or exists $d{nanoseconds}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $d{"seconds"} or exists $d{"nanoseconds"}) 3 0 3
Condition33%1/3
exists $d{"seconds"} or exists $d{"nanoseconds"}
l!l&&r!l&&!r
300
MC/DC0%0/2
exists $d{"seconds"} or exists $d{"nanoseconds"}
exists $d{"seconds"} exists $d{"nanoseconds"}
Truth table33%1/3
A or B
A= exists $d{"seconds"}B= exists $d{"nanoseconds"}
ABresult
1X1
011
000
218 $d{seconds} = ($d{seconds} || 0)
219 3 ▶ + (exists $d{nanoseconds} ? $d{nanoseconds} / 1E9 : 0);
Branch: 1 branch
BranchTrueFalseTotal
exists $d{'nanoseconds'} ? : 3 0 3
Condition50%1/2
$d{'seconds'} || 0
l!l
03
MC/DC0%0/1
$d{'seconds'} || 0
$d{'seconds'}
Truth table50%1/2
A
A= $d{'seconds'}
Aresult
11
00
220 }
221 3 ▶ my $str = $d{negative} ? "-P" : "P";
Branch: 1 branch
BranchTrueFalseTotal
$d{'negative'} ? : 1 2 3
222 3 ▶ $str .= "$d{years}Y" if exists $d{years} and $d{years} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if exists $d{"years"} and $d{"years"} > 0 1 2 3
Condition66%2/3
exists $d{"years"} and $d{"years"} > 0
!ll&&!rl&&r
021
MC/DC50%1/2
exists $d{"years"} and $d{"years"} > 0
exists $d{"years"} $d{"years"} > 0
Truth table66%2/3
A and B
A= exists $d{"years"}B= $d{"years"} > 0
ABresult
0X0
100
111
223 3 ▶ $str .= "$d{months}M" if exists $d{months} and $d{months} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if exists $d{"months"} and $d{"months"} > 0 1 2 3
Condition66%2/3
exists $d{"months"} and $d{"months"} > 0
!ll&&!rl&&r
021
MC/DC50%1/2
exists $d{"months"} and $d{"months"} > 0
exists $d{"months"} $d{"months"} > 0
Truth table66%2/3
A and B
A= exists $d{"months"}B= $d{"months"} > 0
ABresult
0X0
100
111
224 3 ▶ $str .= "$d{days}D" if exists $d{days} and $d{days} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if exists $d{"days"} and $d{"days"} > 0 3 0 3
Condition33%1/3
exists $d{"days"} and $d{"days"} > 0
!ll&&!rl&&r
003
MC/DC0%0/2
exists $d{"days"} and $d{"days"} > 0
exists $d{"days"} $d{"days"} > 0
Truth table33%1/3
A and B
A= exists $d{"days"}B= $d{"days"} > 0
ABresult
0X0
100
111
225 3 ▶ $str .= "T" if grep {exists $d{$_} and $d{$_} > 0} qw(hours minutes seconds);
Branch: 1 branch
BranchTrueFalseTotal
if grep {exists $d{$_} and $d{$_} > 0;} "hours", "minutes", "seconds" 2 1 3
Condition66%2/3
exists $d{$_} && $d{$_} > 0
!ll&&!rl&&r
063
MC/DC50%1/2
exists $d{$_} && $d{$_} > 0
exists $d{$_} $d{$_} > 0
Truth table66%2/3
A && B
A= exists $d{$_}B= $d{$_} > 0
ABresult
0X0
100
111
226 3 ▶ $str .= "$d{hours}H" if exists $d{hours} and $d{hours} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if exists $d{"hours"} and $d{"hours"} > 0 2 1 3
Condition66%2/3
exists $d{"hours"} and $d{"hours"} > 0
!ll&&!rl&&r
012
MC/DC50%1/2
exists $d{"hours"} and $d{"hours"} > 0
exists $d{"hours"} $d{"hours"} > 0
Truth table66%2/3
A and B
A= exists $d{"hours"}B= $d{"hours"} > 0
ABresult
0X0
100
111
227 3 ▶ $str .= "$d{minutes}M" if exists $d{minutes} and $d{minutes} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if exists $d{"minutes"} and $d{"minutes"} > 0 1 2 3
Condition66%2/3
exists $d{"minutes"} and $d{"minutes"} > 0
!ll&&!rl&&r
021
MC/DC50%1/2
exists $d{"minutes"} and $d{"minutes"} > 0
exists $d{"minutes"} $d{"minutes"} > 0
Truth table66%2/3
A and B
A= exists $d{"minutes"}B= $d{"minutes"} > 0
ABresult
0X0
100
111
228 3 ▶ $str .= "$d{seconds}S" if exists $d{seconds} and $d{seconds} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if exists $d{"seconds"} and $d{"seconds"} > 0 0 3 3
Condition33%1/3
exists $d{"seconds"} and $d{"seconds"} > 0
!ll&&!rl&&r
030
MC/DC0%0/2
exists $d{"seconds"} and $d{"seconds"} > 0
exists $d{"seconds"} $d{"seconds"} > 0
Truth table33%1/3
A and B
A= exists $d{"seconds"}B= $d{"seconds"} > 0
ABresult
0X0
100
111
229  
230 3 return $str;
231 }
232  
233  
234 9 ▶sub date_or_datetime {
Subroutine date_or_datetime: called CC=6 SCAR=17.9
Pod documented
235 9 my $dt = $_[0];
236 9 ▶ if ( not UNIVERSAL::isa( $dt, 'DateTime' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($dt, "DateTime")) 8 1 9
237 return unless
238 8 ▶ $dt =~ /^(-?\d\d\d\d+-\d\d-\d\d)(T\d\d:\d\d:\d\d(\.\d+)?)?([+-]\d\d:\d\d|Z)?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $dt =~ /^(-?\d\d\d\d+-\d\d-\d\d)(T\d\d:\d\d:\d\d(\.\d+)?)?([+-]\d\d:\d\d|Z)?$/ 1 7 8
239 7 my ($date,$time,$tz) = ($1,$2,$4);
240 7 $date =~ /(-?\d\d\d\d+)-(\d\d)-(\d\d)/;
241 7 my %p = (year=>$1,month=>$2,day=>$3);
242 7 ▶ if ($time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time) 4 3 7
243 4 $time =~ /T(\d\d):(\d\d):(\d\d)(\.\d+)?/;
244 4 ($p{hour},$p{minute},$p{second})=($1,$2,$3);
245 }
246 7 ▶ if ($tz) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tz) 1 6 7
247 1 $tz =~ s/://; $tz =~ s/Z/UTC/;
248 1 $p{time_zone} = $tz;
249 }
250 7 ▶ $dt = eval { DateTime->new(%p) } || return;
Condition100%2/2
eval { do { 'DateTime'->new(%p) } } || (return)
l!l
61
MC/DC100%1/1
eval { do { 'DateTime'->new(%p) } } || (return)
eval { do { 'DateTime'->new(%p) } }
Truth table100%2/2
A
A= eval { do { 'DateTime'->new(%p) } }
Aresult
11
00
251 }
252 7 $dt->set_time_zone('floating');
253  
254 7 my $date = $dt->strftime("%FT%T");
255 7 $dt =~ s/T00:00:00$//; # remove time part if is zero
256 7 return $dt;
257 }
258  
259 12 1;
260  
261 __END__
« DAIA/Object.pm ↑ Summary DAIA/Response.pm »