File Coverage

blib/lib/WWW/Pastebin/Sprunge/Retrieve.pm
Criterion Covered Total %
statement 33 71 46.4
branch 1 18 5.5
condition 2 8 25.0
subroutine 9 15 60.0
pod 2 2 100.0
total 47 114 41.2


line stmt bran cond sub pod time code
1             package WWW::Pastebin::Sprunge::Retrieve;
2 1     1   23419 use strict;
  1         3  
  1         36  
3 1     1   4 use warnings;
  1         2  
  1         40  
4             # ABSTRACT: retrieves pastes from the sprunge.us pastebin
5             our $VERSION = '0.010'; # VERSION
6 1     1   982 use URI;
  1         8214  
  1         32  
7 1     1   9 use Carp;
  1         1  
  1         100  
8 1     1   1113 use LWP::UserAgent;
  1         53974  
  1         34  
9 1     1   1154 use Encode;
  1         14508  
  1         131  
10 1     1   8 use base 'Class::Data::Accessor';
  1         2  
  1         963  
11             __PACKAGE__->mk_classaccessors(qw(
12             ua
13             uri
14             id
15             content
16             error
17             results
18             ));
19              
20 1     1   776 use overload q|""| => sub { shift->content };
  1     0   2  
  1         13  
  0         0  
21              
22              
23              
24             sub new {
25 1     1 1 507 my $class = shift;
26 1 50       5 croak 'new() takes an even number of arguments' if @_ & 1;
27              
28 1         3 my %args = @_;
29 1         4 $args{ +lc } = delete $args{ $_ } for keys %args;
30              
31 1   50     7 $args{timeout} ||= 30;
32 1   33     10 $args{ua} ||= LWP::UserAgent->new(
33             timeout => $args{timeout},
34             agent => 'WWW::Pastebin::Sprunge (+http://p3rl.org/WWW::Pastebin::Sprunge)',
35             );
36              
37 1         182 my $self = bless {}, $class;
38 1         6 $self->ua( $args{ua} );
39              
40 1         78 return $self;
41             }
42              
43              
44             sub retrieve {
45 0     0 1   my $self = shift;
46 0           my $id = shift;
47              
48 0           $self->$_(undef) for qw( error uri id results );
49              
50 0 0         return $self->_set_error('Missing or empty paste ID/URL')
51             unless $id;
52              
53 0 0         (my $uri, $id) = $self->_make_uri_and_id($id, @_) or return;
54              
55 0           $self->uri($uri);
56 0           $self->id($id);
57              
58 0           my $ua = $self->ua;
59 0           my $response = $ua->get($uri);
60 0 0         if ($response->is_success) {
61 0           return $self->_get_was_successful($response->content);
62             }
63             else {
64 0           return $self->_set_error('Network error: ' . $response->status_line);
65             }
66             }
67              
68             sub _get_was_successful {
69 0     0     my $self = shift;
70 0           my $content = shift;
71              
72 0           return $self->results( $self->_parse($content) );
73             }
74              
75             sub _set_error {
76 0     0     my $self = shift;
77 0           my $err_or_res = shift;
78 0           my $is_net_error = shift;
79              
80 0 0         if (defined $is_net_error) {
81 0           $self->error('Network error: ' . $err_or_res->status_line);
82             }
83             else {
84 0           $self->error($err_or_res);
85             }
86 0           return;
87             }
88              
89             sub _make_uri_and_id {
90 0     0     my $self = shift;
91 0           my $in = shift;
92              
93 0           my $id;
94 0 0         if ( $in =~ m{ (?:http://)? (?:www\.)? sprunge.us/ (\S+?) (?:\?\w+)? $}ix ) {
95 0           $id = $1;
96             }
97 0 0         $id = $in unless defined $id;
98              
99 0           return ( URI->new("http://sprunge.us/$id"), $id );
100             }
101              
102             sub _parse {
103 0     0     my $self = shift;
104 0           my $content = shift;
105 0           my $id = $self->id;
106              
107 0 0 0       if (!defined($content) or !length($content)) {
    0          
108 0           return $self->_set_error('Nothing to parse (empty document retrieved)');
109             }
110             elsif ($content =~ m{\A$id not found.\Z}) {
111 0           return $self->_set_error('No such paste');
112             }
113             else {
114 0           $self->results(decode_utf8($content));
115 0           return $self->content(decode_utf8($content));
116             }
117             }
118              
119              
120              
121             sub content {
122             my $self = shift;
123              
124             return $self->results;
125             }
126              
127             1;
128              
129             __END__