File Coverage

blib/lib/WWW/Pastebin/Base/Retrieve.pm
Criterion Covered Total %
statement 30 52 57.6
branch 1 10 10.0
condition 2 8 25.0
subroutine 8 14 57.1
pod 0 2 0.0
total 41 86 47.6


line stmt bran cond sub pod time code
1             package WWW::Pastebin::Base::Retrieve;
2              
3 1     1   80227 use warnings;
  1         2  
  1         23  
4 1     1   5 use strict;
  1         2  
  1         33  
5              
6             our $VERSION = '0.002';
7              
8 1     1   5 use Carp;
  1         5  
  1         43  
9 1     1   4 use URI;
  1         2  
  1         15  
10 1     1   3 use LWP::UserAgent;
  1         2  
  1         25  
11 1     1   3 use base 'Class::Data::Accessor';
  1         2  
  1         126  
12              
13             __PACKAGE__->mk_classaccessors( qw(
14             ua
15             uri
16             id
17             content
18             error
19             results
20             ));
21              
22 1     1   4 use overload q|""| => sub { shift->content };
  1     0   7  
  1         14  
  0         0  
23              
24             sub new {
25 1     1 0 213 my $class = shift;
26 1 50       4 croak "Must have even number of arguments to new()"
27             if @_ & 1;
28              
29 1         3 my %args = @_;
30 1         4 $args{ +lc } = delete $args{ $_ } for keys %args;
31              
32 1   50     7 $args{timeout} ||= 30;
33 1   33     14 $args{ua} ||= LWP::UserAgent->new(
34             timeout => $args{timeout},
35             agent => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.12)'
36             .' Gecko/20080207 Ubuntu/7.10 (gutsy) Firefox/2.0.0.12',
37             );
38              
39 1         3169 my $self = bless {}, $class;
40 1         7 $self->ua( $args{ua} );
41              
42 1         72 return $self;
43             }
44              
45             sub retrieve {
46 0     0 0   my $self = shift;
47 0           my $id = shift;
48              
49 0           $self->$_(undef) for qw(error uri id results);
50            
51 0 0 0       return $self->_set_error('Missing or empty paste ID or URI')
52             unless defined $id and length $id;
53              
54 0 0         ( my $uri, $id ) = $self->_make_uri_and_id( $id, @_ )
55             or return;
56              
57 0           $self->id( $id );
58 0           $self->uri( $uri );
59              
60 0           my $ua = $self->ua;
61 0           my $response = $ua->get( $uri );
62 0 0         if ( $response->is_success ) {
63 0           return $self->_get_was_successful( $response->content );
64             }
65             else {
66 0           return $self->_set_error('Network error: ' . $response->status_line);
67             }
68             }
69              
70             sub _get_was_successful {
71 0     0     my ( $self, $content ) = @_;
72 0           return $self->results( $self->_parse( $content ) );
73             }
74              
75             sub _set_error {
76 0     0     my ( $self, $error_or_response_obj, $is_net_error ) = @_;
77 0 0         if ( defined $is_net_error ) {
78 0           $self->error( 'Network error: ' . $error_or_response_obj->status_line
79             );
80             }
81             else {
82 0           $self->error( $error_or_response_obj );
83             }
84 0           return;
85             }
86              
87             sub _parse {
88 0     0     croak "Looks like the author of the module forgot to override the"
89             . "_parse() methods";
90             }
91              
92             sub _make_uri_and_id {
93 0     0     croak "Looks like the author of the module forgot to override the"
94             . "_make_uri_and_id() methods";
95             }
96              
97             1;
98             __END__