File Coverage

blib/lib/PlackX/Framework/URIx.pm
Criterion Covered Total %
statement 64 100 64.0
branch 3 12 25.0
condition n/a
subroutine 12 17 70.5
pod 11 12 91.6
total 90 141 63.8


line stmt bran cond sub pod time code
1 2     2   437909 use v5.36;
  2         18  
2             package PlackX::Framework::URIx {
3 2     2   15 use parent 'URI::Fast';
  2         3  
  2         41  
4 2     2   5690 use Scalar::Util qw(blessed);
  2         4  
  2         133  
5 2     2   673 use URI ();
  2         7358  
  2         48  
6 2     2   14 use URI::Escape ();
  2         6  
  2         1676  
7              
8 0     0 0 0 sub new_from_pxfrequest ($class, $requ) {
  0         0  
  0         0  
  0         0  
9 0         0 my $uri = $requ->uri;
10 0         0 return $class->new("$uri")->normalize;
11             }
12              
13             # The below line causes a buffer overflow on github!
14             # URI::Fast->new('other.html')->absolute('http://www.somewebsite.com/somedir/somewhere');
15             # So to() uses URI.pm instead of URI::Fast
16             # Continue to use URI::Fast elsewhere, because
17             # - it is faster
18             # - it has more methods for query string manipulation than URI.pm
19             # - URI.pm is a pain to subclass, returning objects blessed into different
20             # classes depending on the argument
21              
22 6     6 1 5570 sub merge ($self, $rel) {
  6         69  
  6         13  
  6         11  
23 6 50       24 die 'Object method called as class method' unless ref $self;
24 6         72 my $new = URI->new_abs("$rel", "$self");
25 6         15903 return (ref $self)->new("$new");
26             }
27              
28 1     1 1 1089 sub merge_with_query ($self, $rel) {
  1         3  
  1         4  
  1         2  
29 1         5 my $new = $self->merge($rel);
30 1         20 $new->query(scalar $self->query);
31 1         43 return $new;
32             }
33              
34 2     2 1 1795 sub query_set ($self, @new) {
  2         5  
  2         8  
  2         5  
35 2         7 while (@new) {
36 3         23 my $key = shift @new;
37 3         24 my $val = shift @new;
38 3         36 $self->param($key => $val);
39             }
40 2         72 return $self;
41             }
42              
43 2     2 1 773 sub query_add ($self, @new) {
  2         6  
  2         7  
  2         44  
44 2         8 while (@new) {
45 3         70 my $key = shift @new;
46 3         7 my $val = shift @new;
47 3         16 $self->add_param($key => $val);
48             }
49 2         84 return $self;
50             }
51              
52 1     1 1 387 sub query_delete ($self, @keys) { $self->param($_ => undef) for @keys; $self }
  1         2  
  1         8  
  1         3  
  1         6  
  1         59  
53 2     2 1 777 sub query_delete_all ($self) { $self->query_hash({}); $self }
  2         21  
  2         4  
  2         15  
  2         63  
54              
55 0     0 1 0 sub query_delete_all_except ($self, @keys) {
  0         0  
  0         0  
  0         0  
56 0         0 my %keep = map { $_ => 1 } @keys;
  0         0  
57 0         0 foreach my $param ($self->query_keys) {
58 0 0       0 $self->param($param => undef) unless $keep{$param};
59             }
60 0         0 return $self;
61             }
62              
63 1     1 1 388 sub query_delete_keys_starting_with ($self, $string) {
  1         4  
  1         27  
  1         2  
64 1         10 foreach my $param ($self->query_keys) {
65 5 100       101 $self->param($param => undef) if substr($param, 0, length $string) eq $string;
66             }
67 1         5 return $self;
68             }
69              
70 0     0 1   sub query_delete_keys_ending_with ($self, $string) {
  0            
  0            
  0            
71 0           foreach my $param ($self->query_keys) {
72 0 0         $self->param($param => undef) if substr($param, 0 - (length $string), length $string) eq $string;
73             }
74 0           return $self;
75             }
76              
77 0     0 1   sub query_delete_keys_matching ($self, $pattern) {
  0            
  0            
  0            
78 0           foreach my $param ($self->query_keys) {
79 0 0         $self->param($param => undef) if $param =~ m/$pattern/;
80             };
81 0           return $self;
82             }
83              
84 0     0 1   sub query_delete_all_except_keys_matching ($self, $pattern) {
  0            
  0            
  0            
85 0           foreach my $param ($self->query_keys) {
86 0 0         $self->param($param => undef) unless $param =~ m/$pattern/;
87             };
88 0           return $self;
89             }
90             }
91              
92             1;
93              
94             =head1 NAME
95              
96             PlackX::Framework::URIx - Extended URI class.
97              
98              
99             =head1 DESCRIPTION
100              
101             PlackX::Framework::URIx is part of PlackX::Framework. This module is a subclass
102             of URI::Fast with extra features for manipulating query strings, namely setting,
103             adding, or deleting parameters; and creating absolute URLs from relative ones.
104              
105              
106             =head2 Rationale
107              
108             While it is true the URI module does offer URI::QueryParam which can add similar
109             features, that module was designed to replicate the CGI.pm interface. This one
110             does not. Method names are shorter and have been chosen to avoid conflicting
111             with the methods offered by URI::QueryParam. The other distinguishing
112             characteristic is that all of the added methods return the object so that method
113             calls may be chained.
114              
115              
116             =head2 Methods
117              
118             The following methods are those in addition to the ones contained in the
119             URI::Fast module.
120              
121             =head3 merge($rel_uri)
122              
123             Merge a URI with a relative URI, e.g. with a URIx object of
124             http://www.somewhere.com/fruit/apple and a $rel_uri of '../vegetable/carrot',
125             returns http://www.somewhere.com/vegetable/carrot.
126              
127             =head3 merge_with_query($rel_uri)
128              
129             Same as merge(), but preserves the query string.
130              
131             =head3 query_set(@pairs)
132              
133             Adds the list of key-value pairs to the query string. If any keys already exist,
134             they are removed, even if they key appears more than once in the existing query.
135             If you would like to preserve existing queys, use query_add instead.
136             The list must be key-values pairs; no references are accepted.
137              
138             =head3 query_add(@pairs)
139              
140             Adds the list of key-value pairs to the query string, even if the respective
141             keys already exist.
142              
143             =head3 query_delete(@keys)
144              
145             Deletes any parameters in the query string named by the list.
146              
147             =head3 query_delete_all
148              
149             Deletes all parameters from the query string.
150              
151             =head3 query_delete_all_except(@keys)
152              
153             Deletes all parameters from the query string except for the ones named by the
154             list.
155              
156             =head3 query_delete_keys_starting_with($string)
157              
158             =head3 query_delete_keys_ending_with($string)
159              
160             Deletes any parameters in the query string that start or end (respectively)
161             with the string $string.
162              
163             =head3 query_delete_keys_matching($pattern)
164              
165             =head3 query_delete_all_except_keys_matching($pattern)
166              
167             Deletes any parameters in the query string that match or don't match
168             (respectively) the pattern contained in $pattern.
169              
170              
171             =head1 EXPORTS
172              
173             None.
174              
175              
176             =head1 SEE ALSO
177              
178             =over 4
179              
180             =item URI::Fast
181              
182             =item URI
183              
184             =item URI::QueryParam
185              
186             =item Rose::URI
187              
188             =back
189