lib/Badger/URL.pm

filter: Statementstmt 92.9% 131 / 141 Branchbran 73.4% 47 / 64 Conditioncond 48.9% 24 / 49 MC/DCmcdc 16.1% 5 / 31 Subroutinesub 91.3% 21 / 23 Podpod 100% 20 / 20 totaltotal 75.6% 248 / 328 CC 50 SCAR 17.7
CC
50
Cov
80%
set
11.6
join_url
10.5
join_authority
6.8
CRAP 5.9
« lib/Badger/Config.pm ↑ Summary lib/Badger.pm »
1 package Badger::URL;
2  
3 use Badger::Class
4 1 ▶ version => 0.02,
Subroutine BEGIN: called CC=1 SCAR=0.0
5 debug => 0,
6 base => 'Badger::Base',
7 import => 'CLASS class',
8 utils => 'textlike is_object',
9 codec => 'uri', # we use URI encoding for parameters
10 as_text => \&text,
11 is_true => 1, # not sure about this
12 constants => 'HASH BLANK',
13 constant => {
14 SLASH => '/',
15 TEXT => 0,
16 SCHEME => 1,
17 AUTHORITY => 2,
18 USER => 3,
19 HOST => 4,
20 PORT => 5,
21 PATH => 6,
22 QUERY => 7,
23 FRAGMENT => 8,
24 PARAMS => 9,
25 },
26 alias => {
27 url => \&text,
28 },
29 exports => {
30 any => 'URL',
31 1 };
32  
33  
34 #------------------------------------------------------------------------
35 # Example URL:
36 #
37 # scheme authority path query fragment
38 # __ ___________________ _________ _________ __
39 # / \ / \/ \ / \ / \
40 # http://user@example.com:8042/over/there?name=ferret#nose
41 # \__/ \_________/ \__/
42 # user host port
43 #
44 #------------------------------------------------------------------------
45  
46 1 our @ELEMENTS = qw(
47 scheme authority user host port path query fragment params
48 );
49 1 our $N_ELEMS = 1; # slot 0 holds source text, so slot 1 is first field
50 our $ELEMENT = {
51 1 map { $_ => $N_ELEMS++ }
52 @ELEMENTS
53 };
54  
55 # regexen to match basic tokens
56 1 our $MATCH_SCHEME = qr{ ( [a-zA-Z][a-zA-Z0-9.+\-]* ) : }x;
57 1 our $MATCH_USER = qr{ ([^@]*) @ }x;
58 1 our $MATCH_HOST = qr{ ( [^:\/]* ) }x;
59 1 our $MATCH_PORT = qr{ : (\d*) }x;
60 1 our $MATCH_PATH = qr{ ( [^ \? \#]* ) }x;
61 1 our $MATCH_QUERY = qr{ \? ( [^ \#]* ) }x;
62 1 our $MATCH_FRAGMENT = qr{ \# ( .* ) }x;
63  
64 # compound regexen to match authority
65 1 our $MATCH_AUTHORITY = qr{
66 // ( # $1 - authority
67 (?: $MATCH_USER )? # $2 - user
68 $MATCH_HOST # $3 - host
69 (?: $MATCH_PORT )? # $4 - port
70 )
71 }x;
72  
73 # compound regexen to match complete URL
74 1 our $MATCH_URL = qr{
75 ^ (?: $MATCH_SCHEME )? # $1 - scheme
76 (?: $MATCH_AUTHORITY )? # $2,$3,$4,$5 - authority,user,host,port
77 $MATCH_PATH # $6 - path
78 (?: $MATCH_QUERY )? # $7 - query
79 (?: $MATCH_FRAGMENT )? # $8 - fragment
80 }x;
81  
82  
83  
84 #------------------------------------------------------------------------
85 # Constructor function and methods.
86 #------------------------------------------------------------------------
87  
88 4 ▶sub URL {
Subroutine URL: called CC=4 SCAR=15.2
Pod documented
89 4 ▶ return CLASS unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 3 1 4
90 1 ▶ return @_ == 1 && is_object(CLASS, $_[0])
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && &is_object(scalar CLASS(), $_[0]) ? : 0 1 1
Condition33%1/3
@_ == 1 and &is_object(scalar CLASS(), $_[0])
!ll&&!rl&&r
010
MC/DC0%0/2
@_ == 1 and &is_object(scalar CLASS(), $_[0])
@_ == 1 &is_object(scalar CLASS(), $_[0])
Truth table33%1/3
A and B
A= @_ == 1B= &is_object(scalar CLASS(), $_[0])
ABresult
0X0
100
111
91 ? $_[0]->copy # copy existing URL object
92 : CLASS->new(@_); # or construct a new one
93 }
94  
95  
96 4 ▶sub new {
Subroutine new: called CC=5 SCAR=16.6
Pod documented
97 4 my $class = shift;
98 4 ▶ my $args = @_ == 1 ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 3 1 4
99 4 my $self;
100  
101 4 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
102  
103 4 ▶ if (textlike $args) {
Branch: 2 branches
BranchTrueFalseTotal
if (&textlike($args)) { } 3 1 4
elsif (ref $args eq 'HASH') { } 1 0 1
104 3 $self = bless [$args, $args =~ $MATCH_URL], $class;
105 }
106 elsif (ref $args eq HASH) {
107 1 $self = bless [ ], $class;
108 1 $self->set($args);
109  
110 }
111 else {
112 0 return $class->error("Invalid URL specification: $_[0]");
113 }
114  
115 4 return $self;
116 }
117  
118  
119 6 ▶sub copy {
Subroutine copy: called CC=2 SCAR=6.9
Pod documented
120 6 my $self = shift;
121 6 my $copy = bless [ @$self ], ref $self;
122 return @_
123 6 ▶ ? $copy->set(@_)
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 4 2 6
124 : $copy;
125 }
126  
127  
128 5 ▶sub set {
Subroutine set: called CC=11 SCAR=24.5
Pod documented
129 5 my $self = shift;
130 5 ▶ my $args = @_ && ref $_[0] eq HASH ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'HASH' ? : 1 4 5
Condition66%2/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
041
MC/DC50%1/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
131 5 my ($k, $v, $n);
132  
133 5 ▶ while (($k, $v) = each %$args) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$args) 11 5 16
134 11 ▶ $n = $ELEMENT->{ $k } || next;
Condition50%1/2
$Badger::URL::ELEMENT->{$k} || (next)
l!l
110
MC/DC0%0/1
$Badger::URL::ELEMENT->{$k} || (next)
$Badger::URL::ELEMENT->{$k}
Truth table50%1/2
A
A= $Badger::URL::ELEMENT->{$k}
Aresult
11
00
135 11 $self->[$n] = $v;
136 }
137  
138 # The authority is comprised of the user, host and port fields.
139 # We need to split any authority specified, or merge together the user,
140 # host and port if any of them have been changed
141 $self->split_authority
142 5 ▶ if exists $args->{ authority };
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"authority"} 0 5 5
143  
144 $self->join_authority
145 if exists $args->{ user }
146 or exists $args->{ host }
147 5 ▶ or exists $args->{ port };
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"user"} or exists $args->{"host"} or exists $args->{"port"} 1 4 5
Condition66%4/6
exists $args->{"user"} or exists $args->{"host"} or exists $args->{"port"}
l!l&&r!l&&!r
104
exists $args->{"user"} or exists $args->{"host"}
l!l&&r!l&&!r
104
MC/DC33%1/3
exists $args->{"user"} or exists $args->{"host"} or exists $args->{"port"}
exists $args->{"user"} exists $args->{"host"} exists $args->{"port"}
Truth table50%2/4
A or B or C
A= exists $args->{"user"}B= exists $args->{"host"}C= exists $args->{"port"}
ABCresult
1XX1
01X1
0011
0000
148  
149 # similar thing for query/params
150 $self->split_query
151 5 ▶ if exists $args->{ query };
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"query"} 1 4 5
152  
153 $self->join_query
154 5 ▶ if exists $args->{ params };
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"params"} 0 5 5
155  
156 # finally reconstruct the complete url
157 5 $self->join_url;
158  
159 5 return $self;
160 }
161  
162  
163 #-----------------------------------------------------------------------
164 # split/join methods
165 #-----------------------------------------------------------------------
166  
167 0 ▶sub split_authority {
Subroutine split_authority: not called CC=2 SCAR=17.9
Pod documented
168 0 my $self = shift;
169 0 ▶ $self->[AUTHORITY] = BLANK
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[2] 0 0 0
170 unless defined $self->[AUTHORITY];
171  
172 # this regex shouldn't ever fail as everything is optional
173 0 @$self[AUTHORITY,USER,HOST,PORT]
174 = $self->[AUTHORITY] =~ $MATCH_AUTHORITY;
175 }
176  
177  
178 5 ▶sub join_authority {
Subroutine join_authority: called CC=6 SCAR=19.1
Pod documented
179 5 my $self = shift;
180 5 my ($user, $host, $port) = @$self[USER,HOST,PORT];
181  
182 5 ▶ $user = (defined $user && length $user) ? $user . '@' : BLANK;
Branch: 1 branch
BranchTrueFalseTotal
defined $user && length $user ? : 4 1 5
Condition66%2/3
defined $user and length $user
!ll&&!rl&&r
104
MC/DC50%1/2
defined $user and length $user
defined $user length $user
Truth table66%2/3
A and B
A= defined $userB= length $user
ABresult
0X0
100
111
183 5 ▶ $port = (defined $port && length $port) ? ':' . $port : BLANK;
Branch: 1 branch
BranchTrueFalseTotal
defined $port && length $port ? : 5 0 5
Condition33%1/3
defined $port and length $port
!ll&&!rl&&r
005
MC/DC0%0/2
defined $port and length $port
defined $port length $port
Truth table33%1/3
A and B
A= defined $portB= length $port
ABresult
0X0
100
111
184 5 ▶ $host = BLANK unless defined $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 0 5 5
185  
186 5 return ($self->[AUTHORITY] = $user.$host.$port);
187 }
188  
189  
190 4 ▶sub split_query {
Subroutine split_query: called CC=2 SCAR=7.0
Pod documented
191 4 my $self = shift;
192 4 ▶ $self->[QUERY] = ''
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[7] 0 4 4
193 unless defined $self->[QUERY];
194  
195 return ($self->[PARAMS] = {
196 map {
197 4 map { decode($_) }
198 split(/=/, $_, 2)
199 }
200 split(/[&;]/, $self->[QUERY])
201 });
202 }
203  
204  
205 2 ▶sub join_query {
Subroutine join_query: called CC=2 SCAR=7.0
Pod documented
206 2 my $self = shift;
207 2 ▶ my $params = $self->[PARAMS] || { } ; # should we call split_query()?
Condition50%1/2
$self->[9] || {}
l!l
20
MC/DC0%0/1
$self->[9] || {}
$self->[9]
Truth table50%1/2
A
A= $self->[9]
Aresult
11
00
208  
209 return ($self->[QUERY] = join(
210 '&',
211 2 map { $_ . '=' . encode( $params->{ $_ } ) }
212 sort keys %$params # sorted makes debugging easier
213 ));
214 }
215  
216  
217 17 ▶sub join_url {
Subroutine join_url: called CC=9 SCAR=23.5
Pod documented
218 17 my $self = shift;
219 17 my $scheme = $self->[SCHEME];
220 17 my $auth = $self->[AUTHORITY];
221 17 my $query = $self->[QUERY];
222 17 my $frag = $self->[FRAGMENT];
223  
224 17 ▶ $scheme = (defined $scheme && length $scheme) ? $scheme . ':' : BLANK;
Branch: 1 branch
BranchTrueFalseTotal
defined $scheme && length $scheme ? : 17 0 17
Condition33%1/3
defined $scheme and length $scheme
!ll&&!rl&&r
0017
MC/DC0%0/2
defined $scheme and length $scheme
defined $scheme length $scheme
Truth table33%1/3
A and B
A= defined $schemeB= length $scheme
ABresult
0X0
100
111
225 17 ▶ $auth = (defined $auth && length $auth) ? '//' . $auth : BLANK;
Branch: 1 branch
BranchTrueFalseTotal
defined $auth && length $auth ? : 17 0 17
Condition33%1/3
defined $auth and length $auth
!ll&&!rl&&r
0017
MC/DC0%0/2
defined $auth and length $auth
defined $auth length $auth
Truth table33%1/3
A and B
A= defined $authB= length $auth
ABresult
0X0
100
111
226 17 ▶ $query = (defined $query && length $query) ? '?' . $query : BLANK;
Branch: 1 branch
BranchTrueFalseTotal
defined $query && length $query ? : 15 2 17
Condition66%2/3
defined $query and length $query
!ll&&!rl&&r
2015
MC/DC50%1/2
defined $query and length $query
defined $query length $query
Truth table66%2/3
A and B
A= defined $queryB= length $query
ABresult
0X0
100
111
227 17 ▶ $frag = (defined $frag && length $frag) ? '#' . $frag : BLANK;
Branch: 1 branch
BranchTrueFalseTotal
defined $frag && length $frag ? : 13 4 17
Condition66%2/3
defined $frag and length $frag
!ll&&!rl&&r
4013
MC/DC50%1/2
defined $frag and length $frag
defined $frag length $frag
Truth table66%2/3
A and B
A= defined $fragB= length $frag
ABresult
0X0
100
111
228  
229 17 return ($self->[TEXT] = $scheme.$auth.$self->[PATH].$query.$frag);
230 }
231  
232  
233  
234 #-----------------------------------------------------------------------
235 # accessor/mutator methods
236 #-----------------------------------------------------------------------
237  
238 17 ▶sub text {
Subroutine text: called CC=1 SCAR=0.0
Pod documented
239 17 $_[0]->[TEXT];
240 }
241  
242  
243 2 ▶sub scheme {
Subroutine scheme: called CC=2 SCAR=6.9
Pod documented
244 2 my $self = shift;
245 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
246 1 $self->[SCHEME] = shift;
247 1 $self->join_url;
248 }
249 2 return $self->[SCHEME];
250 }
251  
252  
253 2 ▶sub authority {
Subroutine authority: called CC=2 SCAR=9.2
Pod documented
254 2 my $self = shift;
255 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 2 2
256 0 $self->[AUTHORITY] = shift;
257 0 $self->split_authority;
258 0 $self->join_url;
259 }
260 2 return $self->[AUTHORITY];
261 }
262  
263  
264 3 ▶sub query {
Subroutine query: called CC=2 SCAR=6.9
Pod documented
265 3 my $self = shift;
266 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 1 3
267 2 $self->[QUERY] = shift;
268 2 $self->split_query;
269 2 $self->join_url;
270 }
271 3 return $self->[QUERY];
272 }
273  
274  
275 3 ▶sub params {
Subroutine params: called CC=3 SCAR=11.0
Pod documented
276 3 my $self = shift;
277 3 ▶ my $params = $self->[PARAMS] || $self->split_query;
Condition66%2/3
$self->[9] || $self->split_query
l!l&&r!l&&!r
210
MC/DC0%0/2
$self->[9] || $self->split_query
$self->[9] $self->split_query
Truth table66%2/3
A || B
A= $self->[9]B= $self->split_query
ABresult
1X1
011
000
278 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 1 3
279 2 my $extra = Badger::Utils::params(@_);
280 # NOTE: this doesn't account for multi-valued params
281 2 @$params{ keys %$extra } = values %$extra;
282 2 $self->join_query;
283 2 $self->join_url;
284 }
285 3 return $params;
286 }
287  
288  
289 8 ▶sub server {
Subroutine server: called CC=5 SCAR=18.4
Pod documented
290 8 my $self = shift;
291 8 my $scheme = $self->[SCHEME];
292 8 my $auth = $self->[AUTHORITY];
293  
294 8 ▶ $scheme = (defined $scheme && length $scheme) ? $scheme.':' : BLANK;
Branch: 1 branch
BranchTrueFalseTotal
defined $scheme && length $scheme ? : 8 0 8
Condition33%1/3
defined $scheme and length $scheme
!ll&&!rl&&r
008
MC/DC0%0/2
defined $scheme and length $scheme
defined $scheme length $scheme
Truth table33%1/3
A and B
A= defined $schemeB= length $scheme
ABresult
0X0
100
111
295 8 ▶ $auth = (defined $auth && length $auth) ? '//'.$auth : BLANK;
Branch: 1 branch
BranchTrueFalseTotal
defined $auth && length $auth ? : 8 0 8
Condition33%1/3
defined $auth and length $auth
!ll&&!rl&&r
008
MC/DC0%0/2
defined $auth and length $auth
defined $auth length $auth
Truth table33%1/3
A and B
A= defined $authB= length $auth
ABresult
0X0
100
111
296  
297 8 return $scheme.$auth;
298 }
299  
300  
301 6 ▶sub service {
Subroutine service: called CC=1 SCAR=0.0
Pod documented
302 6 my $self = shift;
303 6 return $self->server
304 . $self->[PATH];
305 }
306  
307  
308 2 ▶sub request {
Subroutine request: called CC=3 SCAR=11.8
Pod documented
309 2 my $self = shift;
310 2 my $service = $self->service;
311 2 my $query = $self->[QUERY];
312  
313 2 ▶ $query = (defined $query && length $query) ? '?'.$query : BLANK;
Branch: 1 branch
BranchTrueFalseTotal
defined $query && length $query ? : 2 0 2
Condition33%1/3
defined $query and length $query
!ll&&!rl&&r
002
MC/DC0%0/2
defined $query and length $query
defined $query length $query
Truth table33%1/3
A and B
A= defined $queryB= length $query
ABresult
0X0
100
111
314  
315 2 return $self->service
316 . $query;
317 }
318  
319  
320 # This is a quick-hack implementation of relative() that bodges the paths.
321 # This should be replaced with a more robust implementation. It also needs
322 # to be integrated with the work on Badger::Filesystem::Universal.
323  
324 2 ▶sub relative {
Subroutine relative: called CC=2 SCAR=6.9
Pod documented
325 2 my $self = shift;
326 2 my $path = join(SLASH, @_);
327 2 my $base = $self->[PATH];
328 2 $base =~ s{/$}{};
329 2 ▶ $path = join(SLASH, $base, $path)
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[^/] 1 1 2
330 unless $path =~ m{^/};
331 2 return $self->copy( path => $path );
332 }
333  
334  
335 2 ▶sub absolute {
Subroutine absolute: called CC=1 SCAR=0.0
Pod documented
336 2 my $self = shift;
337 2 my $path = shift;
338 2 $path =~ s{^/*}{/};
339 2 return $self->copy( path => $path );
340 }
341  
342  
343 0 ▶sub dump {
Subroutine dump: not called CC=2 SCAR=17.9
Pod documented
344 0 my $self = shift;
345 0 ▶ return '[URL:' . join('|', map { defined($_) ? $_ : '' } @$self) . ']';
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
346 }
347  
348  
349  
350 #-----------------------------------------------------------------------
351 # generated accessor/mutator methods for those with similar functionality
352 #-----------------------------------------------------------------------
353  
354 class->methods(
355 map {
356 1 my ($name, $slot) = @$_;
357 7 ▶ $name => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
358 7 my $self = shift;
359 7 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 3 7
360 # if any of user, host or port are updated then we must
361 # reconstruct the authority and complete URL
362 4 $self->[$slot] = shift;
363 4 $self->join_authority;
364 4 $self->join_url;
365 }
366 7 return $self->[$slot];
367 }
368 3 }
369 [user => USER],
370 [host => HOST],
371 [port => PORT],
372 );
373  
374 class->methods(
375 map {
376 1 my ($name, $slot) = @$_;
377 5 ▶ $name => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
378 5 my $self = shift;
379 5 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 2 5
380 # if either of the path or fragment are updated then we
381 # must regenerate the complete URL
382 3 $self->[$slot] = shift;
383 3 $self->join_url;
384 }
385 5 return $self->[$slot];
386 }
387 2 }
388 [path => PATH],
389 [fragment => FRAGMENT],
390 );
391  
392  
393 1 1;
394 __END__
« lib/Badger/Config.pm ↑ Summary lib/Badger.pm »