File Coverage

blib/lib/POE/Component/WWW/Cache/Google.pm
Criterion Covered Total %
statement 20 46 43.4
branch 0 14 0.0
condition 0 3 0.0
subroutine 8 9 88.8
pod 1 1 100.0
total 29 73 39.7


line stmt bran cond sub pod time code
1             package POE::Component::WWW::Cache::Google;
2              
3 1     1   28442 use warnings;
  1         3  
  1         36  
4 1     1   7 use strict;
  1         2  
  1         180  
5              
6             our $VERSION = '1.001004'; # VERSION
7              
8 1     1   845 use POE;
  1         60720  
  1         7  
9 1     1   164511 use base 'POE::Component::NonBlockingWrapper::Base';
  1         2  
  1         933  
10 1     1   36389 use WWW::Cache::Google;
  1         9060  
  1         36  
11 1     1   1134 use LWP::UserAgent;
  1         49091  
  1         445  
12              
13             sub _methods_define {
14 1     1   571 return ( cache => '_wheel_entry' );
15             }
16              
17             sub cache {
18 1     1 1 11536 $poe_kernel->post( shift->{session_id} => cache => @_ );
19             }
20              
21             sub _process_request {
22 0     0     my ( $self, $in_ref ) = @_;
23              
24 0           my $cache = WWW::Cache::Google->new( $in_ref->{uri} );
25              
26 0           $in_ref->{cache} = $cache->cache;
27              
28 0 0         if ( $in_ref->{fetch} ) {
29 0           my $ua = LWP::UserAgent->new(
30             agent => 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:21.0) '
31             . 'Gecko/20100101 Firefox/21.0',
32             # trick google into not giving us 403s
33             timeout => 30,
34             max_size => $in_ref->{max_size},
35             );
36              
37 0           my $response = $ua->get( $in_ref->{cache} );
38 0 0         if ( not $response->is_success ) {
39 0           $in_ref->{error} = $response->status_line;
40 0 0         if ( $in_ref->{error} eq '404 Not Found' ) {
41 0           $in_ref->{error} = q|Doesn't look like cache exists|;
42             }
43 0           return;
44             }
45              
46 0           $in_ref->{content} = $response->content;
47 0 0         if ( $in_ref->{content} !~ /
48 0           $in_ref->{error} = q|Doesn't look like cache exists|;
49 0           return;
50             }
51             }
52              
53 0 0         if ( ref $in_ref->{fetch} eq 'SCALAR' ) {
54              
55 0           my $filename = ${ $in_ref->{fetch} };
  0            
56              
57 0 0 0       if ( -e $filename and not $in_ref->{overwrite} ) {
58 0           $in_ref->{error} = "File `$filename` already exists";
59 0           return;
60             }
61              
62 0 0         if ( open my $fh, '>', $filename ) {
63 0           print $fh delete $in_ref->{content}, "\n";
64 0           close $fh;
65             }
66             else {
67 0           $in_ref->{error} = "Failed to open `$filename` for writing [$!]";
68             }
69             }
70              
71 0           return;
72             }
73              
74             1;
75             __END__