Validator/URI.pm

filter: Statementstmt 97.3% 73 / 75 Branchbran 78.5% 33 / 42 Conditioncond 50.0% 9 / 18 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 91.6% 11 / 12 Podpod 100% 6 / 6 totaltotal 82.8% 135 / 163 CC 29 SCAR 22.1
CC
29
Cov
85%
to_abs
16.7
to_unsafe_string
12.0
to_string
6.0
CRAP 9.1
« Validator/Schema/OpenAPIv2.pm ↑ Summary Validator/Store.pm »
1 package JSON::Validator::URI;
2 81 ▶use Mojo::Base 'Mojo::URL';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 81 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 81 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 81 ▶use Mojo::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 81 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 81 use constant UUID_NAMESPACE => do {
10 81 my $uuid = '1bab225d-1ca6-4cc5-9c53-a37cc7527848'; # UUIDv4
11 81 $uuid =~ tr/-//d;
12 81 pack 'H*', $uuid;
13 81 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 81 our @EXPORT_OK = qw(uri);
16  
17 81 has nid => undef;
18 81 has nss => undef;
19  
20 sub from_data {
21 583 ▶ my $self = shift->scheme('urn')->nid('uuid');
Subroutine from_data: called CC=1 SCAR=0.0
Pod documented
22 583 state $d = Digest::SHA->new(1);
23 583 $d->reset->add(UUID_NAMESPACE)->add(Mojo::JSON::encode_json(shift));
24 583 my $uuid = substr $d->digest, 0, 16;
25 583 substr $uuid, 6, 1, chr(ord(substr $uuid, 6, 1) & 0x0f | 0x50); # set version 5
26 583 substr $uuid, 8, 1, chr(ord(substr $uuid, 8, 1) & 0x3f | 0x80); # set variant 2
27 583 return $self->nss(sprintf '%s-%s-%s-%s-%s', map { unpack 'H*', $_ } map { substr $uuid, 0, $_, '' } 4, 2, 2, 2, 6);
28 }
29  
30 sub parse {
31 4600 ▶ my ($self, $url) = @_;
Subroutine parse: called CC=4 SCAR=13.9
Pod documented
32  
33 # URL
34 4600 ▶ return $self->SUPER::parse($url) unless $url =~ m!^urn:(.*)$!i;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ /^urn:(.*)$/iu 4588 12 4600
35  
36 # URN
37 12 $self->scheme('urn');
38  
39 # TODO This regex is not 100% correct according to the 1997 changes regarding "?"
40 12 ▶ return $self unless $1 =~ m/^([a-z0-9][a-z0-9-]{0,31}):([^#]+)(#(.*))?/;
Branch: 1 branch
BranchTrueFalseTotal
unless $1 =~ /^([a-z0-9][a-z0-9-]{0,31}):([^#]+)(#(.*))?/u 0 12 12
41 12 ▶ $self->fragment($4) if defined $3;
Branch: 1 branch
BranchTrueFalseTotal
if defined $3 4 8 12
42 12 return $self->nid($1)->nss($2);
43 }
44  
45 sub to_abs {
46 3135 ▶ my $self = shift;
Subroutine to_abs: called CC=14 SCAR=28.2
Pod documented
47  
48 3135 my $abs = $self->clone;
49 3135 ▶ return $abs if $abs->is_abs;
Branch: 1 branch
BranchTrueFalseTotal
if $abs->is_abs 60 3075 3135
50  
51 # Scheme
52 3075 ▶ my $base = shift || $abs->base;
Condition33%1/3
shift() || $abs->base
l!l&&r!l&&!r
307500
MC/DC0%0/2
shift() || $abs->base
shift() $abs->base
Truth table33%1/3
A || B
A= shift()B= $abs->base
ABresult
1X1
011
000
53 3075 $abs->base($base)->scheme($base->scheme);
54  
55 3075 ▶ my $scheme = lc($base->scheme // $abs->scheme // '');
Condition40%2/5
($base->scheme // $abs->scheme) // ""
l!l
30750
$base->scheme // $abs->scheme
l!l&&r!l&&!r
307500
MC/DC0%0/2
($base->scheme // $abs->scheme) // ""
$base->scheme $abs->scheme
Truth table33%1/3
A // B
A= $base->schemeB= $abs->scheme
ABresult
1X1
011
000
56  
57 3075 ▶ if ($scheme eq 'urn') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme eq 'urn') { } 49 3026 3075
58 49 ▶ return $abs->scheme('urn') if $abs->nid and $abs->nss;
Branch: 1 branch
BranchTrueFalseTotal
if $abs->nid and $abs->nss 0 49 49
Condition33%1/3
$abs->nid and $abs->nss
!ll&&!rl&&r
4900
MC/DC0%0/2
$abs->nid and $abs->nss
$abs->nid $abs->nss
Truth table33%1/3
A and B
A= $abs->nidB= $abs->nss
ABresult
0X0
100
111
59 49 $abs->nid($base->nid)->nss($base->nss);
60 }
61 else {
62 3026 ▶ return $abs if $abs->host;
Branch: 1 branch
BranchTrueFalseTotal
if $abs->host 0 3026 3026
63 3026 $abs->host($base->host)->port($base->port);
64 }
65  
66 3075 ▶ $abs->fragment($base->fragment) unless $abs->fragment;
Branch: 1 branch
BranchTrueFalseTotal
unless $abs->fragment 388 2687 3075
67 3075 ▶ $abs->userinfo($base->userinfo) unless $abs->userinfo;
Branch: 1 branch
BranchTrueFalseTotal
unless $abs->userinfo 3075 0 3075
68  
69 # Absolute path
70 3075 my $path = $abs->path;
71 3075 ▶ return $abs if $path->leading_slash;
Branch: 1 branch
BranchTrueFalseTotal
if $path->leading_slash 0 3075 3075
72  
73 # Inherit path
74 3075 ▶ if (!@{$path->parts}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @{$path->parts;}) { } 2952 123 3075
75 2952 $abs->path($base->path->clone->canonicalize);
76  
77 # Query
78 2952 ▶ $abs->query($base->query->clone) unless length $abs->query->to_string;
Branch: 1 branch
BranchTrueFalseTotal
unless length $abs->query->to_string 2952 0 2952
79 }
80  
81 # Merge paths
82 123 else { $abs->path($base->path->clone->merge($path)->canonicalize) }
83  
84 3075 return $abs;
85 }
86  
87 sub to_string {
88 8760 ▶ my $self = shift;
Subroutine to_string: called CC=6 SCAR=17.9
Pod documented
89  
90 # URL
91 8760 ▶ return $self->SUPER::to_string unless 'urn' eq ($self->scheme // '');
Branch: 1 branch
BranchTrueFalseTotal
unless "urn" eq ($self->scheme // "") 6576 2184 8760
Condition100%2/2
$self->scheme // ""
l!l
872832
MC/DC100%1/1
$self->scheme // ""
$self->scheme
Truth table100%2/2
A
A= $self->scheme
Aresult
11
00
92  
93 # URN
94 2184 my $urn = sprintf 'urn:%s:%s', $self->nid, $self->nss;
95 2184 my $p = $self->path_query;
96 2184 ▶ $urn .= $p =~ m!^/! ? $p : "/$p" if length $p;
Branch: 2 branches
BranchTrueFalseTotal
if length $p 1 2183 2184
$p =~ m[^/]u ? : 0 1 1
97 2184 ▶ $urn .= "#$p" if defined($p = $self->fragment);
Branch: 1 branch
BranchTrueFalseTotal
if defined($p = $self->fragment) 50 2134 2184
98 2184 return $urn;
99 }
100  
101 sub to_unsafe_string {
102 0 ▶ my $self = shift;
Subroutine to_unsafe_string: not called CC=3 SCAR=24.8
Pod documented
103 0 ▶ return 'urn' eq ($self->scheme // '') ? $self->to_string : $self->SUPER::to_unsafe_string;
Branch: 1 branch
BranchTrueFalseTotal
'urn' eq ($self->scheme // '') ? : 0 0 0
Condition0%0/2
$self->scheme // ""
l!l
00
MC/DC0%0/1
$self->scheme // ""
$self->scheme
Truth table0%0/2
A
A= $self->scheme
Aresult
11
00
104 }
105  
106 sub uri {
107 6753 ▶ my ($uri, $base) = @_;
Subroutine uri: called CC=6 SCAR=17.9
Pod documented
108 6753 ▶ return __PACKAGE__->new unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 84 6669 6753
109 6669 ▶ $uri = __PACKAGE__->new($uri) unless blessed $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($uri) 3638 3031 6669
110 6669 ▶ $base = __PACKAGE__->new($base) if $base and !blessed $base;
Branch: 1 branch
BranchTrueFalseTotal
if $base and not builtin::blessed($base) 954 5715 6669
Condition100%3/3
$base and not builtin::blessed($base)
!ll&&!rl&&r
35372178954
MC/DC100%2/2
$base and not builtin::blessed($base)
$base not builtin::blessed($base)
Truth table100%3/3
A and not(B)
A= $baseB= not builtin::blessed($base)
ABresult
0X0
100
111
111 6669 ▶ return $base ? $uri->to_abs($base) : $uri->clone;
Branch: 1 branch
BranchTrueFalseTotal
$base ? : 3132 3537 6669
112 }
113  
114 81 1;
115  
116 =encoding utf8
117  
118 =head1 NAME
119  
120 JSON::Validator::URI - Uniform Resource Identifier
121  
122 =head1 SYNOPSIS
123  
124 use JSON::Validator::URI;
125  
126 my $urn = JSON::Validator::URI->new('urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f');
127 my $url = JSON::Validator::URI->new('/foo');
128 my $url = JSON::Validator::URI->new('https://mojolicious.org');
129  
130 =head1 DESCRIPTION
131  
132 L<JSON::Validator::URI> is a class for presenting both URL and URN.
133  
134 This class is currently EXPERIMENTAL.
135  
136 =head1 EXPORTED FUNCTIONS
137  
138 =head2 uri
139  
140 $uri = uri;
141 $uri = uri $orig, $base;
142  
143 Returns a new L<JSON::Validator::URI> object from C<$orig> and C<$base>. Both
144 variables can be either a string or a L<JSON::Validator::URI> object.
145  
146 =head1 ATTRIBUTES
147  
148 L<JSON::Validator::URI> inherits all attributes from L<Mojo::URL> and
149 implements the following ones.
150  
151 =head2 nid
152  
153 $str = $uri->nid;
154  
155 Returns the NID part of a URN. Example "uuid" or "iban".
156  
157 =head2 nss
158  
159 $str = $uri->nss;
160  
161 Returns the NSS part of a URN. Example "6e8bc430-9c3a-11d9-9669-0800200c9a66".
162  
163 =head1 METHODS
164  
165 L<JSON::Validator::URI> inherits all methods from L<Mojo::URL> and implements
166 the following ones.
167  
168 =head2 from_data
169  
170 $str = $uri->from_data($data);
171  
172 This method will generate a URN for C<$data>. C<$data> will be serialized
173 using L<Mojo::JSON/encode_json> before being used to generate an UUIDv5.
174  
175 This method is EXPERIMENTAL and subject to change!
176  
177 =head2 parse
178  
179 See L<Mojo::URL/parse>.
180  
181 =head2 to_abs
182  
183 See L<Mojo::URL/to_abs>.
184  
185 =head2 to_string
186  
187 See L<Mojo::URL/to_string>.
188  
189 =head2 to_unsafe_string
190  
191 See L<Mojo::URL/to_unsafe_string>.
192  
193 =head1 SEE ALSO
194  
195 L<JSON::Validator>.
196  
197 =cut
« Validator/Schema/OpenAPIv2.pm ↑ Summary Validator/Store.pm »