blib/lib/HTTP/DAV/Browse.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package HTTP::DAV::Browse;
2  
3 =head1 NAME
4  
5 HTTP::DAV::Browse - browse the WebDAV tree
6  
7 =head1 SYNOPSIS
8  
9 my $browser = HTTP::DAV::Browse->new('base_uri' => $url);
10 my %lsd = $browser->ls_detailed('/');
11 my @files = $browser->ls('/');
12  
13 =head1 DESCRIPTION
14  
15 For the moment L<HTTP::DAV::Browse> allows to list WebDAV folders and
16 gather detailed information (properties) about the files inside.
17  
18 Can be used on Subversion WebDAV repositories.
19  
20 NOTE: for our https+password protected Subversion repository, I hat to set all
21 username+password+realm to make it work and not complain that the
22 requests are not authenticated.
23  
24 =cut
25  
26 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 0 ▶use HTTP::DAV 0.38;
Subroutine BEGIN: called CC=1 SCAR=2.6
30 use URI;
31  
32 our $VERSION = '0.05';
33  
34 =head1 PROPERTIES
35  
36 base_uri
37 username
38 password
39 realm
40  
41 =cut
42  
43 subtype 'Object.HTTP.DAV' => as class_type('HTTP::DAV');
44 subtype 'Object.URI' => as class_type('URI');
45 coerce 'Object.URI'
46 => from 'Str'
47 => via { URI->new($_) };
48  
49 has '_dav' => (is => 'ro', isa => 'Object.HTTP.DAV', lazy => 1, default => sub { $_[0]->_dav_init });
50 has 'base_uri' => (is => 'rw', isa => 'Object.URI', required => 1, coerce => 1 );
51 has 'username' => (is => 'rw', isa => 'Str|Undef');
52 has 'password' => (is => 'rw', isa => 'Str|Undef');
53 has 'realm' => (is => 'rw', isa => 'Str|Undef');
54  
55 =head1 METHODS
56  
57 =head2 new()
58  
59 Object constructor. Requires 'base_uri' argument.
60  
61 =cut
62  
63 sub _dav_init {
64 my $self = shift;
65  
66 my $dav = new HTTP::DAV;
67 $dav->credentials(
68 $self->username,
69 $self->password,
70 $self->base_uri,
71 $self->realm,
72 );
73
74 $dav->open( $self->base_uri )
75 or die("Couldn't open ".$self->base_uri.": " .$dav->message . "\n");
76
77 return $dav;
78 }
79 sub _base_path {
80 my $self = shift;
81 return $self->base_uri->path;
82 }
83  
84 =head2 ls($path)
85  
86 For given C<$path> (that is prepended by C<<$self->base_uri>>) returns
87 array of files.
88  
89 Throws exception for non existing paths.
90  
91 =cut
92  
93 sub ls {
94 my $self = shift;
95 return map { $_->{'rel_uri'} } $self->ls_detailed(@_);
96 }
97  
98  
99 =head2 ls_detailed($path)
100  
101 For given C<$path> (that is prepended by C<<$self->base_uri>>) returns
102 array of hashes with file details. Example:
103  
104 {
105 'baseline-relative-path' => 'trunk/SVGraph',
106 'version-name' => '69',
107 'version-controlled-configuration' => '<D:href>/svgraph/!svn/vcc/default</D:href>',
108 'creationdate' => 'Mon, 19 Nov 2007 08:01:47 GMT',
109 'short_ls' => 'Listing of http://svn.comsultia.com/svgraph/trunk/SVGraph/
110 ',
111 'getlastmodified' => 'Mon, 19 Nov 2007 08:01:47 GMT',
112 'lastmodifiedepoch' => 1195459307,
113 'short_props' => '<dir>',
114 'getcontenttype' => 'text/html; charset=UTF-8',
115 'checked-in' => '<D:href>/svgraph/!svn/ver/69/trunk/SVGraph</D:href>',
116 'repository-uuid' => '05c03c3c-be17-0410-b9f1-b57ecf2f02e2',
117 'display_date' => 'Nov 19 2007',
118 'resourcetype' => 'collection',
119 'creationepoch' => '1195459307.58319',
120 'creator-displayname' => 'rfordinal',
121 'long_ls' => 'URL: http://svn.comsultia.com/svgraph/trunk/SVGraph/
122 --- stripped ---
123 ',
124 'getetag' => 'W/"69//trunk/SVGraph"',
125 'rel_uri' => bless( do{\(my $o = 'SVGraph/')}, 'URI::http' ),
126 'deadprop-count' => '0',
127 'lastmodifieddate' => 'Mon, 19 Nov 2007 08:01:47 GMT'
128 }
129  
130 Throws exception for non existing paths.
131  
132 =cut
133 sub ls_detailed {
134 my $self = shift;
135 my $path = shift;
136
137 die 'path is required argument'
138 if not defined $path;
139
140 my $dav = $self->_dav;
141
142 # find propertis on $path
143 my $resources = $dav->propfind($self->_base_path.$path, 1);
144 die $dav->message
145 if not $resources;
146  
147 # get list of resources
148 my $resources_list = $resources->get_resourcelist;
149
150 # nothing to do if no resources found
151 return
152 if not $resources_list;
153  
154 # return array of { all properties }
155 return
156 map { $_->{'_properties'} }
157 $resources_list->get_resources
158 ;
159 }
160  
161 '..., nebo je po mně a já mám voskované boty, ráno co ráno stejné probuzení do nicoty.';
162  
163  
164 __END__
↑ Summary