File Coverage

blib/lib/WWW/DoingItWrongCom/RandImage.pm
Criterion Covered Total %
statement 27 51 52.9
branch 2 12 16.6
condition n/a
subroutine 7 10 70.0
pod 3 3 100.0
total 39 76 51.3


line stmt bran cond sub pod time code
1             package WWW::DoingItWrongCom::RandImage;
2              
3 1     1   103020 use warnings;
  1         5  
  1         63  
4 1     1   11 use strict;
  1         3  
  1         92  
5              
6             our $VERSION = '1.01';
7              
8 1     1   9 use Carp;
  1         9  
  1         113  
9 1     1   7 use URI;
  1         3  
  1         27  
10 1     1   7 use LWP::UserAgent;
  1         2  
  1         30  
11 1     1   9 use HTML::TokeParser::Simple;
  1         3  
  1         709  
12              
13             sub new {
14 1     1 1 354 my $class = shift;
15 1 50       6 croak "Must have event number of arguments to ->new()"
16             if @_ & 1;
17              
18 1         2 my %args = @_;
19 1         4 $args{ lc $_ } = delete $args{ $_ } for keys %args;
20              
21 1 50       5 unless ( exists $args{ua_args}{timeout} ) {
22 1         5 $args{ua_args}{timeout} = 30;
23             }
24              
25 1         4 my $self = bless \%args, $class;
26              
27 1         5 $self->{site_uri} = 'http://www.doingitwrong.com/';
28              
29 1         3 return $self;
30             }
31              
32             sub fetch {
33 0     0 1   my $self = shift;
34              
35 0           $self->err_msg( undef );
36              
37 0 0         my $ua = LWP::UserAgent->new( %{ $self->{ua_args} || {} } );
  0            
38              
39 0           my $response = $ua->get( $self->{site_uri} );
40              
41 0 0         if ( $response->is_success ) {
42 0           return $self->_parse_response( $response->content );
43             }
44             else {
45 0           $self->err_msg( $response->status_line );
46 0           return undef;
47             }
48              
49 0           undef;
50             }
51              
52             sub _parse_response {
53 0     0     my $self = shift;
54 0           my $content = shift;
55              
56 0           my $parser = HTML::TokeParser::Simple->new( \$content );
57 0           while ( my $token = $parser->get_token ) {
58 0 0         if ( $token->is_start_tag('img') ) {
59 0           my $return_uri = URI->new( $self->{site_uri} );
60 0           $return_uri->path( $token->get_attr('src') );
61              
62 0           return $return_uri;
63             }
64             }
65 0           $self->err_msg('Parser could not find the image');
66 0           return undef;
67             }
68              
69             sub err_msg {
70 0     0 1   my $self = shift;
71 0 0         if ( @_ ) {
72 0           $self->{ ERR_MSG } = shift;
73             }
74 0           return $self->{ ERR_MSG };
75             }
76              
77             1;
78             __END__