File Coverage

blib/lib/Mo/utils/URI.pm
Criterion Covered Total %
statement 54 54 100.0
branch 21 22 95.4
condition 26 36 72.2
subroutine 12 12 100.0
pod 4 4 100.0
total 117 128 91.4


line stmt bran cond sub pod time code
1             package Mo::utils::URI;
2              
3 6     6   251688 use base qw(Exporter);
  6         20  
  6         879  
4 6     6   57 use strict;
  6         15  
  6         206  
5 6     6   34 use warnings;
  6         22  
  6         452  
6              
7 6     6   3681 use Data::Validate::URI qw(is_uri);
  6         469870  
  6         530  
8 6     6   5101 use Error::Pure qw(err);
  6         58634  
  6         165  
9 6     6   448 use Readonly;
  6         13  
  6         257  
10 6     6   8384 use URI;
  6         57648  
  6         3851  
11              
12             Readonly::Array our @EXPORT_OK => qw(check_location check_uri check_url
13             check_urn);
14              
15             our $VERSION = 0.04;
16              
17             sub check_location {
18 10     10 1 474824 my ($self, $key) = @_;
19              
20 10 100       31 _check_key($self, $key) && return;
21              
22 8         18 my $value = $self->{$key};
23 8         36 my $uri = URI->new($value);
24 8 50 66     30389 if (! $uri->can('scheme') || ! $uri->can('host') || ! $uri->scheme || ! $uri->host) {
      66        
      33        
25 5 100 100     30 if (! $uri->can('path_segments') || ! $uri->path_segments) {
26 2 100 66     40 if (! $uri->can('query') || ! $uri->query) {
27 1         7 err "Parameter '".$key."' doesn't contain valid location.",
28             'Value', $value,
29             ;
30             }
31             }
32             }
33              
34 7         603 return;
35             }
36              
37             sub check_uri {
38 15     15 1 412889 my ($self, $key) = @_;
39              
40 15 100       90 _check_key($self, $key) && return;
41              
42 13         28 my $value = $self->{$key};
43 13 100       535 if (! is_uri($value)) {
44 2         125 err "Parameter '".$key."' doesn't contain valid URI.",
45             'Value', $value,
46             ;
47             }
48              
49 11         3391 return;
50             }
51              
52             sub check_url {
53 12     12 1 417852 my ($self, $key) = @_;
54              
55 12 100       40 _check_key($self, $key) && return;
56              
57 10         24 my $value = $self->{$key};
58 10         53 my $uri = URI->new($value);
59 10 100 66     30045 if (! $uri->can('scheme') || ! $uri->can('host') || ! $uri->scheme || ! $uri->host) {
      66        
      66        
60 5         164 err "Parameter '".$key."' doesn't contain valid URL.",
61             'Value', $value,
62             ;
63             }
64              
65 5         958 return;
66             }
67              
68             sub check_urn {
69 10     10 1 261680 my ($self, $key) = @_;
70              
71 10 100       25 _check_key($self, $key) && return;
72              
73 8         12 my $value = $self->{$key};
74 8         30 my $uri = URI->new($value);
75 8 100 66     14105 if (! $uri->can('nid') || ! $uri->can('nss') || ! $uri->nid || ! $uri->nss) {
      66        
      100        
76 4         59 err "Parameter '".$key."' doesn't contain valid URN.",
77             'Value', $value,
78             ;
79             }
80              
81 4         173 return;
82             }
83              
84             sub _check_key {
85 47     47   142 my ($self, $key) = @_;
86              
87 47 100 100     302 if (! exists $self->{$key} || ! defined $self->{$key}) {
88 8         29 return 1;
89             }
90              
91 39         108 return 0;
92             }
93              
94             1;
95              
96             __END__
97              
98             =pod
99              
100             =encoding utf8
101              
102             =head1 NAME
103              
104             Mo::utils::URI - Mo utilities for URI.
105              
106             =head1 SYNOPSIS
107              
108             use Mo::utils::URI qw(check_location check_uri check_url);
109              
110             check_location($self, $key);
111             check_uri($self, $key);
112             check_url($self, $key);
113             check_urn($self, $key);
114              
115             =head1 DESCRIPTION
116              
117             Mo utilities for URI checking of data objects.
118              
119             =head1 SUBROUTINES
120              
121             =head2 C<check_location>
122              
123             check_location($self, $key);
124              
125             I<Since version 0.01. Described functionality since version 0.03.>
126              
127             Check parameter defined by C<$key> which is valid location. Could be URL or
128             absolute or relative path. Value is valid if it is undefined or key doesn't exist.
129              
130             Put error if check isn't ok.
131              
132             Returns undef.
133              
134             =head2 C<check_uri>
135              
136             check_uri($self, $key);
137              
138             I<Since version 0.01. Described functionality since version 0.03.>
139              
140             Check parameter defined by C<$key> which is valid URI.
141             Value is valid if it is undefined or key doesn't exist.
142              
143             Put error if check isn't ok.
144              
145             Returns undef.
146              
147             =head2 C<check_url>
148              
149             check_url($self, $key);
150              
151             I<Since version 0.01. Described functionality since version 0.03.>
152              
153             Check parameter defined by C<$key> which is valid URL.
154             Value is valid if it is undefined or key doesn't exist.
155              
156             Put error if check isn't ok.
157              
158             Returns undef.
159              
160             =head2 C<check_urn>
161              
162             check_urn($self, $key);
163              
164             I<Since version 0.01. Described functionality since version 0.03.>
165              
166             Check parameter defined by C<$key> which is valid URN.
167             Value is valid if it is undefined or key doesn't exist.
168              
169             Put error if check isn't ok.
170              
171             Returns undef.
172              
173             =head1 ERRORS
174              
175             check_location():
176             Parameter '%s' doesn't contain valid location.
177             Value: %s
178              
179             check_uri():
180             Parameter '%s' doesn't contain valid URI.
181             Value: %s
182              
183             check_url():
184             Parameter '%s' doesn't contain valid URL.
185             Value: %s
186              
187             check_urn():
188             Parameter '%s' doesn't contain valid URN.
189             Value: %s
190              
191             =head1 EXAMPLE1
192              
193             =for comment filename=check_location_ok.pl
194              
195             use strict;
196             use warnings;
197              
198             use Mo::utils::URI qw(check_location);
199              
200             my $self = {
201             'key' => 'https://skim.cz',
202             };
203             check_location($self, 'key');
204              
205             # Print out.
206             print "ok\n";
207              
208             # Output:
209             # ok
210              
211             =head1 EXAMPLE2
212              
213             =for comment filename=check_location_fail.pl
214              
215             use strict;
216             use warnings;
217              
218             use Error::Pure;
219             use Mo::utils::URI qw(check_location);
220              
221             $Error::Pure::TYPE = 'Error';
222              
223             my $self = {
224             'key' => 'urn:isbn:9788072044948',
225             };
226             check_location($self, 'key');
227              
228             # Print out.
229             print "ok\n";
230              
231             # Output like:
232             # #Error [..utils.pm:?] Parameter 'key' doesn't contain valid location.
233              
234             =head1 EXAMPLE3
235              
236             =for comment filename=check_uri_ok.pl
237              
238             use strict;
239             use warnings;
240              
241             use Mo::utils::URI qw(check_uri);
242              
243             my $self = {
244             'key' => 'https://skim.cz',
245             };
246             check_uri($self, 'key');
247              
248             # Print out.
249             print "ok\n";
250              
251             # Output:
252             # ok
253              
254             =head1 EXAMPLE4
255              
256             =for comment filename=check_uri_fail.pl
257              
258             use strict;
259             use warnings;
260              
261             use Error::Pure;
262             use Mo::utils::URI qw(check_uri);
263              
264             $Error::Pure::TYPE = 'Error';
265              
266             my $self = {
267             'key' => 'bad_uri',
268             };
269             check_uri($self, 'key');
270              
271             # Print out.
272             print "ok\n";
273              
274             # Output like:
275             # #Error [..utils.pm:?] Parameter 'key' doesn't contain valid URI.
276              
277             =head1 EXAMPLE5
278              
279             =for comment filename=check_url_ok.pl
280              
281             use strict;
282             use warnings;
283              
284             use Mo::utils::URI qw(check_url);
285              
286             my $self = {
287             'key' => 'https://skim.cz',
288             };
289             check_url($self, 'key');
290              
291             # Print out.
292             print "ok\n";
293              
294             # Output:
295             # ok
296              
297             =head1 EXAMPLE6
298              
299             =for comment filename=check_url_fail.pl
300              
301             use strict;
302             use warnings;
303              
304             use Error::Pure;
305             use Mo::utils::URI qw(check_url);
306              
307             $Error::Pure::TYPE = 'Error';
308              
309             my $self = {
310             'key' => 'bad_uri',
311             };
312             check_uri($self, 'key');
313              
314             # Print out.
315             print "ok\n";
316              
317             # Output like:
318             # #Error [..utils.pm:?] Parameter 'key' doesn't contain valid URL.
319              
320             =head1 EXAMPLE7
321              
322             =for comment filename=check_urn_ok.pl
323              
324             use strict;
325             use warnings;
326              
327             use Mo::utils::URI qw(check_urn);
328              
329             my $self = {
330             'key' => 'urn:isbn:0451450523',
331             };
332             check_urn($self, 'key');
333              
334             # Print out.
335             print "ok\n";
336              
337             # Output:
338             # ok
339              
340             =head1 EXAMPLE8
341              
342             =for comment filename=check_urn_fail.pl
343              
344             use strict;
345             use warnings;
346              
347             use Error::Pure;
348             use Mo::utils::URI qw(check_urn);
349              
350             $Error::Pure::TYPE = 'Error';
351              
352             my $self = {
353             'key' => 'bad_urn',
354             };
355             check_urn($self, 'key');
356              
357             # Print out.
358             print "ok\n";
359              
360             # Output like:
361             # #Error [..utils.pm:?] Parameter 'key' doesn't contain valid URN.
362              
363             =head1 DEPENDENCIES
364              
365             L<Data::Validate::URI>,
366             L<Error::Pure>,
367             L<Exporter>,
368             L<Readonly>,
369             L<URI>.
370              
371             =head1 SEE ALSO
372              
373             =over
374              
375             =item L<Mo>
376              
377             Micro Objects. Mo is less.
378              
379             =item L<Mo::utils::CSS>
380              
381             Mo CSS utilities.
382              
383             =item L<Mo::utils::Date>
384              
385             Mo date utilities.
386              
387             =item L<Mo::utils::Language>
388              
389             Mo language utilities.
390              
391             =item L<Mo::utils::Email>
392              
393             Mo utilities for email.
394              
395             =item L<Wikibase::Datatype::Utils>
396              
397             Wikibase datatype utilities.
398              
399             =back
400              
401             =head1 REPOSITORY
402              
403             L<https://github.com/michal-josef-spacek/Mo-utils-URI>
404              
405             =head1 AUTHOR
406              
407             Michal Josef Špaček L<mailto:skim@cpan.org>
408              
409             L<http://skim.cz>
410              
411             =head1 LICENSE AND COPYRIGHT
412              
413             © 2024-2025 Michal Josef Špaček
414              
415             BSD 2-Clause License
416              
417             =head1 VERSION
418              
419             0.04
420              
421             =cut