blib/lib/URI/ToDisk.pm

filter: Statementstmt 98.4% 62 / 63 Branchbran 64.2% 18 / 28 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 19 / 19 Podpod 100% 7 / 7 totaltotal 87.7% 107 / 122 CC 16 SCAR 11.8
CC
16
Cov
86%
new
7.6
param
4.1
__eq
4.0
CRAP 3.2
↑ Summary
1 package URI::ToDisk;
2  
3 =pod
4  
5 =head1 NAME
6  
7 URI::ToDisk - An object for mapping a URI to an on-disk storage directory
8  
9 =head1 SYNOPSIS
10  
11 # We have a directory on disk that is accessible via a web server
12 my $authors = URI::ToDisk->new( '/var/www/AUTHORS', 'http://ali.as/AUTHORS' );
13
14 # We know where a particular generated file needs to go
15 my $about = $authors->catfile( 'A', 'AD', 'ADAMK', 'about.html' );
16
17 # Save the file to disk
18 my $file = $about->path;
19 open( FILE, ">$file" ) or die "open: $!";
20 print FILE, $content;
21 close FILE;
22
23 # Show the user where to see the file
24 my $uri = $about->uri;
25 print "Author information is at $uri\n";
26  
27 =head1 DESCRIPTION
28  
29 In several process relating to working with the web, we may need to keep
30 track of an area of disk that maps to a particular URL. From this location,
31 we should be able to derived both a filesystem path and URL for any given
32 directory or file under this location that we might need to work with.
33  
34 =head2 Implementation
35  
36 Internally each C<URI::ToDisk> object contains both a filesystem path,
37 which is altered using L<File::Spec>, and a L<URI> object. When making a
38 change, the path section of the URI is altered using <File::Spec::Unix>.
39  
40 =head2 Method Calling Conventions
41  
42 The main functional methods, such as C<catdir> and C<catfile>, do B<not>
43 modify the original object, instead returning a new object containing the
44 new location.
45  
46 This means that it should be used in a somewhat similar way to L<File::Spec>.
47  
48 # The File::Spec way
49 my $path = '/some/path';
50 $path = File::Spec->catfile( $path, 'some', 'file.txt' );
51
52 # The URI::ToDisk way
53 my $location = URI::ToDisk->new( '/some/path', 'http://foo.com/blah' );
54 $location = $location->catfile( 'some', 'file.txt' );
55  
56 OK, well it's not exactly THAT close, but you get the idea. It also allows you
57 to do method chaining, which is basically
58  
59 URI::ToDisk->new( '/foo', 'http://foo.com/' )->catfile( 'bar.txt' )->uri
60  
61 Which may seem a little trivial now, but I expect it to get more useful later.
62 It also means you can do things like this.
63  
64 my $base = URI::ToDisk->new( '/my/cache', 'http://foo.com/' );
65 foreach my $path ( @some_files ) {
66 my $file = $base->catfile( $path );
67 print $file->path . ': ' . $file->uri . "\n";
68 }
69  
70 In the above example, you don't have to be continuously cloning the location,
71 because all that stuff happens internally as needed.
72  
73 =head1 METHODS
74  
75 =cut
76  
77 2 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 2 ▶use base 'Clone';
Subroutine BEGIN: called CC=1 SCAR=0.0
80 2 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
81 2 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
82 2 ▶use File::Spec::Unix ();
Subroutine BEGIN: called CC=1 SCAR=0.0
83 2 ▶use Params::Util '_INSTANCE',
Subroutine BEGIN: called CC=1 SCAR=0.0
84 2 '_ARRAY';
85  
86 # Overload stringification to the string form of the URL.
87 2 ▶use overload 'bool' => sub () { 1 },
Subroutine BEGIN: called CC=1 SCAR=0.0
88 2 '""' => 'uri',
89 2 'eq' => '__eq';
90  
91 2 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
92 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
93 2 $VERSION = '1.12';
94 }
95  
96  
97  
98  
99  
100 #####################################################################
101 # Constructors
102  
103 =pod
104  
105 =head2 new $path, $http_url
106  
107 The C<new> constructor takes as argument a filesystem path and a http(s)
108 URL. Both are required, and the method will return C<undef> is either is
109 illegal. The URL is not required to have protocol, host or port sections,
110 and as such allows for host-relative URL to be used.
111  
112 Returns a new C<URI::ToDisk> object on success, or C<undef> on failure.
113  
114 =cut
115  
116 6 ▶sub new {
Subroutine new: called CC=6 SCAR=20.3
Pod documented
117 6 my $class = shift;
118  
119 # Get the base file system path
120 6 ▶ my $path = File::Spec->canonpath(shift) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $path = "File::Spec"->canonpath(shift()) or return undef 6 0 6
121  
122 # Get the base URI. We only accept HTTP(s) URLs
123 6 ▶ return undef unless defined $_[0] and ! ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] and not ref $_[0] 0 6 6
Condition33%1/3
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
006
MC/DC0%0/2
defined $_[0] and not ref $_[0]
defined $_[0] not ref $_[0]
Truth table33%1/3
A and not(B)
A= defined $_[0]B= not ref $_[0]
ABresult
0X0
100
111
124 6 ▶ my $URI = URI->new( shift, 'http' ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $URI = "URI"->new(shift(), "http") or return undef 6 0 6
125 6 ▶ $URI->path( '/' ) unless length $URI->path;
Branch: 1 branch
BranchTrueFalseTotal
unless length $URI->path 6 0 6
126  
127 # Create the object
128 6 bless { path => $path, URI => $URI }, $class;
129 }
130  
131 =pod
132  
133 =head2 param $various
134  
135 C<param> is provided as a mechanism for higher order modules to flexibly
136 accept URI::ToDisk's as parameters. In this case, it accepts either
137 an existing URI::ToDisk object, two arguments ($path, $http_url), or
138 a reference to an array containing the same two arguments.
139  
140 Returns a URI::ToDisk if possible, or C<undef> if one cannot be provided.
141  
142 =cut
143  
144 3 ▶sub param {
Subroutine param: called CC=4 SCAR=14.0
Pod documented
145 3 my $class = shift;
146 3 ▶ return shift if _INSTANCE($_[0], 'URI::ToDisk');
Branch: 1 branch
BranchTrueFalseTotal
if &_INSTANCE($_[0], "URI::ToDisk") 1 2 3
147 2 ▶ return URI::ToDisk->new(@_) if @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 1 1 2
148 1 ▶ return URI::ToDisk->new(@{$_[0]}) if _ARRAY($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if &_ARRAY($_[0]) 1 0 1
149 0 return undef;
150 }
151  
152  
153  
154  
155  
156 #####################################################################
157 # Accessors
158  
159 =pod
160  
161 =head2 uri
162  
163 The C<uri> method gets and returns the current URI of the location, in
164 string form.
165  
166 =cut
167  
168 62 ▶sub uri {
Subroutine uri: called CC=1 SCAR=0.0
Pod documented
169 62 $_[0]->{URI}->as_string;
170 }
171  
172 =pod
173  
174 =head2 URI
175  
176 The capitalised C<URI> method gets and returns a copy of the raw L<URI>,
177 held internally by the location. Note that only a copy is returned, and
178 as such as safe to further modify yourself without effecting the location.
179  
180 =cut
181  
182 32 ▶sub URI {
Subroutine URI: called CC=1 SCAR=0.0
Pod documented
183 32 Clone::clone $_[0]->{URI};
184 }
185  
186 =pod
187  
188 =head2 path
189  
190 The C<path> method returns the filesystem path componant of the location.
191  
192 =cut
193  
194 44 ▶sub path { $_[0]->{path} }
Subroutine path: called CC=1 SCAR=0.0
Pod documented
195  
196  
197  
198  
199  
200 #####################################################################
201 # Manipulate Locations
202  
203 =pod
204  
205 =head2 catdir 'dir', 'dir', ...
206  
207 A L<File::Spec> workalike, the C<catdir> method acts in the same way as for
208 L<File::Spec>, modifying both componants of the location. The C<catdir> method
209 returns a B<new> URI::ToDisk object representing the new location, or
210 C<undef> on error.
211  
212 =cut
213  
214 1 ▶sub catdir {
Subroutine catdir: called CC=3 SCAR=11.1
Pod documented
215 1 my $self = shift;
216 1 my @args = @_;
217  
218 # Alter the URI and local paths
219 1 ▶ my $new_uri = File::Spec::Unix->catdir( $self->{URI}->path, @args ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $new_uri = "File::Spec::Unix"->catdir($self->{"URI"}->path, @args) or return undef 1 0 1
220 1 ▶ my $new_path = File::Spec->catdir( $self->{path}, @args ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $new_path = "File::Spec"->catdir($self->{"path"}, @args) or return undef 1 0 1
221  
222 # Clone and set the new values
223 1 my $changed = $self->clone;
224 1 $changed->{URI}->path( $new_uri );
225 1 $changed->{path} = $new_path;
226  
227 1 $changed;
228 }
229  
230 =pod
231  
232 =head2 catfile [ 'dir', ..., ] $file
233  
234 Like C<catdir>, the C<catfile> method acts in the same was as for
235 L<File::Spec>, and returns a new URI::ToDisk object representing
236 the file, or C<undef> on error.
237  
238 =cut
239  
240 1 ▶sub catfile {
Subroutine catfile: called CC=3 SCAR=11.1
Pod documented
241 1 my $self = shift;
242 1 my @args = @_;
243  
244 # Alter the URI and local paths
245 1 ▶ my $uri = File::Spec::Unix->catfile( $self->{URI}->path, @args ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $uri = "File::Spec::Unix"->catfile($self->{"URI"}->path, @args) or return undef 1 0 1
246 1 ▶ my $fs = File::Spec->catfile( $self->{path}, @args ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $fs = "File::Spec"->catfile($self->{"path"}, @args) or return undef 1 0 1
247  
248 # Set both and return
249 1 my $changed = $self->clone;
250 1 $changed->{URI}->path( $uri );
251 1 $changed->{path} = $fs;
252  
253 1 $changed;
254 }
255  
256  
257  
258  
259  
260 #####################################################################
261 # Additional Overload Methods
262  
263 14 ▶sub __eq {
Subroutine __eq: called CC=4 SCAR=13.9
264 14 ▶ my $left = _INSTANCE(shift, 'URI::ToDisk') or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $left = &_INSTANCE(shift(), "URI::ToDisk") or return "" 14 0 14
265 14 ▶ my $right = _INSTANCE(shift, 'URI::ToDisk') or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $right = &_INSTANCE(shift(), "URI::ToDisk") or return "" 10 4 14
266 10 ▶ ($left->path eq $right->path) and ($left->uri eq $right->uri);
Branch: 1 branch
BranchTrueFalseTotal
$left->path eq $right->path and $left->uri eq $right->uri 7 3 10
267 }
268  
269  
270  
271  
272  
273 #####################################################################
274 # Coercion Support
275  
276 1 ▶sub __as_URI { shift->URI }
Subroutine __as_URI: called CC=1 SCAR=0.0
277  
278 2 1;
279  
280 =pod
281  
282 =head1 TO DO
283  
284 Add more File::Spec-y methods as needed. Ask if you need one.
285  
286 =head1 SUPPORT
287  
288 Bugs should be reported via the CPAN bug tracker at
289  
290 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=URI-ToDisk>
291  
292 For other issues, or commercial enhancement or support, contact the author.
293  
294 =head1 AUTHORS
295  
296 Adam Kennedy L<http://ali.as/>, cpan@ali.as
297  
298 =head1 COPYRIGHT
299  
300 Copyright (c) 2003 - 2006 Adam Kennedy.
301  
302 This program is free software; you can redistribute
303 it and/or modify it under the same terms as Perl itself.
304  
305 The full text of the license can be found in the
306 LICENSE file included with this module.
307  
308 =cut
↑ Summary