Flute/UriAdjust.pm untested

filter: Statementstmt 0.0% 0 / 38 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 58 CC 7 SCAR 34.4
CC
7
Cov
0%
result
56.0
BEGIN
2.0
BEGIN
2.0
CRAP 31.1
1 package Template::Flute::UriAdjust;
2  
3 use strict;
4 use warnings;
5  
6 use URI;
7 use URI::Escape;
8 use URI::Escape (qw/uri_unescape/);
9  
10 use Moo;
11  
12 =head1 NAME
13  
14 Template::Flute::UriAdjust - URI adjust class for Template::Flute
15  
16 =head1 SYNOPSIS
17  
18 $new_path = Template::Flute::UriAdjust->new(
19 uri => 'test',
20 adjust => '/t/',
21 );
22  
23 =head1 DESCRIPTION
24  
25 Adjusts relative URIs to a base path.
26  
27 =head1 ATTRIBUTES
28  
29 =head2 adjust
30  
31 Base path.
32  
33 =cut
34  
35 has adjust => (
36 is => 'rw',
37 required => 1,
38 );
39  
40 =head2 uri
41  
42 URI to be adjusted.
43  
44 =cut
45  
46 has uri => (
47 is => 'rw',
48 required => 1,
49 );
50  
51 =head2 scheme
52  
53 URI scheme (defaults to C<http>).
54  
55 =cut
56  
57 has scheme => (
58 is => 'rw',
59 default => 'http',
60 );
61  
62 =head1 METHODS
63  
64 =head2 result
65  
66 Returns new URI if it has adjusted. Otherwise it returns undef.
67  
68 =cut
69  
70 sub result {
71 my ($self) = @_;
72 my $uri = URI->new($self->uri);
73  
74 # set scheme if necessary
75 if (! $uri->scheme) {
76 $uri->scheme($self->scheme);
77 }
78 return unless $uri->scheme =~ /\Ahttps?\z/;
79  
80 my $result = $uri->clone;
81  
82 if (! $uri->host) {
83 # add prefix to link
84 my $adjust = $self->adjust;
85  
86 if ($uri->path =~ m%^/%) {
87 $adjust =~ s%/$%%;
88 }
89 elsif ($adjust !~ m%/$%) {
90 $adjust .= '/';
91 }
92  
93 $result->path($adjust . $uri->path);
94  
95 # unescape the resulting path
96 $result = uri_unescape($result->path);
97  
98 if ($uri->fragment) {
99 $result .= "#" . uri_unescape($uri->fragment);
100 }
101  
102 return $result;
103 }
104  
105 return;
106 };
107  
108 =head1 AUTHOR
109  
110 Stefan Hornburg (Racke), <racke@linuxia.de>
111  
112 =head1 LICENSE AND COPYRIGHT
113  
114 Copyright 2014-2019 Stefan Hornburg (Racke) <racke@linuxia.de>.
115  
116 This program is free software; you can redistribute it and/or modify it
117 under the terms of either: the GNU General Public License as published
118 by the Free Software Foundation; or the Artistic License.
119  
120 See http://dev.perl.org/licenses/ for more information.
121  
122 =cut
123  
124 1;