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 83 ▶use Mojo::Base 'Mojo::URL';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 83 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 83 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 83 ▶use Mojo::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 83 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 83 ▶use constant UUID_NAMESPACE => do {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 83 my $uuid = '1bab225d-1ca6-4cc5-9c53-a37cc7527848'; # UUIDv4
11 83 $uuid =~ tr/-//d;
12 83 pack 'H*', $uuid;
13 83 };
14  
15 83 our @EXPORT_OK = qw(uri);
16  
17 83 has nid => undef;
18 83 has nss => undef;
19  
20 1084 ▶sub from_data {
Subroutine from_data: called CC=1 SCAR=0.0
Pod documented
21 1084 my $self = shift->scheme('urn')->nid('uuid');
22 1084 state $d = Digest::SHA->new(1);
23 1084 $d->reset->add(UUID_NAMESPACE)->add(Mojo::JSON::encode_json(shift));
24 1084 my $uuid = substr $d->digest, 0, 16;
25 1084 substr $uuid, 6, 1, chr(ord(substr $uuid, 6, 1) & 0x0f | 0x50); # set version 5
26 1084 substr $uuid, 8, 1, chr(ord(substr $uuid, 8, 1) & 0x3f | 0x80); # set variant 2
27 1084 return $self->nss(sprintf '%s-%s-%s-%s-%s', map { unpack 'H*', $_ } map { substr $uuid, 0, $_, '' } 4, 2, 2, 2, 6);
28 }
29  
30 65821 ▶sub parse {
Subroutine parse: called CC=4 SCAR=13.9
Pod documented
31 65821 my ($self, $url) = @_;
32  
33 # URL
34 65821 ▶ return $self->SUPER::parse($url) unless $url =~ m!^urn:(.*)$!i;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ /^urn:(.*)$/iu 65809 12 65821
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 62744 ▶sub to_abs {
Subroutine to_abs: called CC=14 SCAR=28.2
Pod documented
46 62744 my $self = shift;
47  
48 62744 my $abs = $self->clone;
49 62744 ▶ return $abs if $abs->is_abs;
Branch: 1 branch
BranchTrueFalseTotal
if $abs->is_abs 560 62184 62744
50  
51 # Scheme
52 62184 ▶ my $base = shift || $abs->base;
Condition33%1/3
shift() || $abs->base
l!l&&r!l&&!r
6218400
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 62184 $abs->base($base)->scheme($base->scheme);
54  
55 62184 ▶ my $scheme = lc($base->scheme // $abs->scheme // '');
Condition40%2/5
($base->scheme // $abs->scheme) // ""
l!l
621840
$base->scheme // $abs->scheme
l!l&&r!l&&!r
6218400
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 62184 ▶ if ($scheme eq 'urn') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme eq 'urn') { } 1049 61135 62184
58 1049 ▶ return $abs->scheme('urn') if $abs->nid and $abs->nss;
Branch: 1 branch
BranchTrueFalseTotal
if $abs->nid and $abs->nss 0 1049 1049
Condition33%1/3
$abs->nid and $abs->nss
!ll&&!rl&&r
104900
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 1049 $abs->nid($base->nid)->nss($base->nss);
60 }
61 else {
62 61135 ▶ return $abs if $abs->host;
Branch: 1 branch
BranchTrueFalseTotal
if $abs->host 0 61135 61135
63 61135 $abs->host($base->host)->port($base->port);
64 }
65  
66 62184 ▶ $abs->fragment($base->fragment) unless $abs->fragment;
Branch: 1 branch
BranchTrueFalseTotal
unless $abs->fragment 388 61796 62184
67 62184 ▶ $abs->userinfo($base->userinfo) unless $abs->userinfo;
Branch: 1 branch
BranchTrueFalseTotal
unless $abs->userinfo 62184 0 62184
68  
69 # Absolute path
70 62184 my $path = $abs->path;
71 62184 ▶ return $abs if $path->leading_slash;
Branch: 1 branch
BranchTrueFalseTotal
if $path->leading_slash 0 62184 62184
72  
73 # Inherit path
74 62184 ▶ if (!@{$path->parts}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @{$path->parts;}) { } 62061 123 62184
75 62061 $abs->path($base->path->clone->canonicalize);
76  
77 # Query
78 62061 ▶ $abs->query($base->query->clone) unless length $abs->query->to_string;
Branch: 1 branch
BranchTrueFalseTotal
unless length $abs->query->to_string 62061 0 62061
79 }
80  
81 # Merge paths
82 123 else { $abs->path($base->path->clone->merge($path)->canonicalize) }
83  
84 62184 return $abs;
85 }
86  
87 133886 ▶sub to_string {
Subroutine to_string: called CC=6 SCAR=17.9
Pod documented
88 133886 my $self = shift;
89  
90 # URL
91 133886 ▶ return $self->SUPER::to_string unless 'urn' eq ($self->scheme // '');
Branch: 1 branch
BranchTrueFalseTotal
unless "urn" eq ($self->scheme // "") 126696 7190 133886
Condition100%2/2
$self->scheme // ""
l!l
13385432
MC/DC100%1/1
$self->scheme // ""
$self->scheme
Truth table100%2/2
A
A= $self->scheme
Aresult
11
00
92  
93 # URN
94 7190 my $urn = sprintf 'urn:%s:%s', $self->nid, $self->nss;
95 7190 my $p = $self->path_query;
96 7190 ▶ $urn .= $p =~ m!^/! ? $p : "/$p" if length $p;
Branch: 2 branches
BranchTrueFalseTotal
if length $p 1 7189 7190
$p =~ m[^/]u ? : 0 1 1
97 7190 ▶ $urn .= "#$p" if defined($p = $self->fragment);
Branch: 1 branch
BranchTrueFalseTotal
if defined($p = $self->fragment) 1050 6140 7190
98 7190 return $urn;
99 }
100  
101 0 ▶sub to_unsafe_string {
Subroutine to_unsafe_string: not called CC=3 SCAR=24.8
Pod documented
102 0 my $self = shift;
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 126474 ▶sub uri {
Subroutine uri: called CC=6 SCAR=17.9
Pod documented
107 126474 my ($uri, $base) = @_;
108 126474 ▶ return __PACKAGE__->new unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 84 126390 126474
109 126390 ▶ $uri = __PACKAGE__->new($uri) unless blessed $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($uri) 64250 62140 126390
110 126390 ▶ $base = __PACKAGE__->new($base) if $base and !blessed $base;
Branch: 1 branch
BranchTrueFalseTotal
if $base and not builtin::blessed($base) 1563 124827 126390
Condition100%3/3
$base and not builtin::blessed($base)
!ll&&!rl&&r
63649611781563
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 126390 ▶ return $base ? $uri->to_abs($base) : $uri->clone;
Branch: 1 branch
BranchTrueFalseTotal
$base ? : 62741 63649 126390
112 }
113  
114 83 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 »