File Coverage

blib/lib/URI/CrawlableHash.pm
Criterion Covered Total %
statement 70 73 95.8
branch 14 22 63.6
condition 2 4 50.0
subroutine 16 16 100.0
pod 1 7 14.2
total 103 122 84.4


line stmt bran cond sub pod time code
1             package URI::CrawlableHash;
2              
3 3     3   72 use strict;
  3         5  
  3         105  
4 3     3   14 use warnings;
  3         5  
  3         144  
5             our $VERSION = '0.02';
6              
7 3     3   1853 use URI;
  3         15080  
  3         92  
8 3     3   25 use URI::Escape;
  3         5  
  3         246  
9 3     3   2409 use Guard;
  3         1997  
  3         279  
10              
11             sub convert_always {
12 2     2 1 18 my ( $class, $option ) = @_;
13 3     3   18 no warnings 'redefine';
  3         7  
  3         252  
14              
15 2   50     7 $option ||= "query";
16              
17 2         16 my $orig = URI->can('new');
18 2         14 my $conv = $class->can("convert_to_" . $option);
19 2 50       7 unless ($conv) { die "unknown option: " . $option }
  0         0  
20            
21 3     3   13 no strict "refs";
  3         6  
  3         1870  
22              
23 2         11 *{'URI::new'} = sub {
24 2     2   14 my $self = $orig->(@_);
25 2 50       9981 if ($self) {
26 2         23 $conv->($self);
27             }
28 2         9 };
29              
30 2 50       7 if ( defined wantarray ) {
31             return guard {
32 2     2   22 *{"URI::new"} = $orig;
  2         43  
33 2         14 };
34             }
35             }
36              
37             sub convert_to_query {
38 1     1 0 3 my $self = shift;
39 1 50       10 if ($self->has_crawlable_hash) {
40 1         25 $self->fragment_to_query
41             } else {
42 0         0 $self
43             }
44             }
45              
46             sub convert_to_hash {
47 1     1 0 2 my $self = shift;
48 1 50       10 if ($self->has_escaped_fragment) {
49 1         10 $self->query_to_fragment
50             } else {
51 0         0 $self
52             }
53             }
54              
55             sub URI::has_crawlable_hash {
56 5     5 0 8 my ($uri) = @_;
57 5   50     17 ($uri->fragment || "") =~/^!/
58             }
59              
60             sub URI::has_escaped_fragment {
61 4     4 0 8 my ($uri) = @_;
62 4         24 my %hash = $uri->query_form;
63 4 50       388 $hash{_escaped_fragment_} ? 1 : undef;
64             }
65              
66             sub URI::fragment_to_query {
67 4     4 0 177 my $self = shift;
68 4 50       18 return $self unless $self->has_crawlable_hash;
69              
70 4         59 my $fragment = $self->fragment;
71 4         47 $fragment =~s/^!//;
72 4         18 my $q = "_escaped_fragment_=" . uri_escape($fragment, "\x00-\x20\x23\x25\x26\x2b\x7f-\xff");
73 4 100       453 if ($self->query) {
74 2         25 my $old = $self->query;
75 2         22 $old =~s/_escaped_fragment_=[^\&]*$//;
76 2 100       6 my $delimiter = $old ? "&" : "";
77 2         8 $self->query( $old . $delimiter . $q );
78             } else {
79 2         26 $self->query($q);
80             }
81 4         104 $self->fragment(undef);
82 4         109 $self;
83             }
84              
85             sub URI::query_to_fragment {
86 3     3 0 9671 my $self = shift;
87 3 50       16 return $self unless $self->has_escaped_fragment;
88              
89 3         12 my %query = $self->query_form;
90 3         159 my $escaped = delete $query{_escaped_fragment_};
91              
92 3         12 my $old = $self->query;
93 3         46 $old =~s/\&?_escaped_fragment_=[^\&]*$//;
94              
95 3 100       11 if ($old) {
96 1         4 $self->query($old)
97             } else {
98 2         9 $self->query(undef);
99             }
100              
101 3         69 $self->fragment('!' . uri_unescape($escaped));
102 3         146 $self;
103             }
104              
105             1;
106             __END__