File Coverage

blib/lib/WWW/Pastebin/Bot/Pastebot/Create.pm
Criterion Covered Total %
statement 34 49 69.3
branch 2 12 16.6
condition 1 3 33.3
subroutine 10 13 76.9
pod 3 3 100.0
total 50 80 62.5


line stmt bran cond sub pod time code
1             package WWW::Pastebin::Bot::Pastebot::Create;
2              
3 1     1   102376 use warnings;
  1         3  
  1         42  
4 1     1   5 use strict;
  1         2  
  1         55  
5              
6             our $VERSION = '0.002';
7              
8 1     1   5 use Carp;
  1         8  
  1         75  
9 1     1   5 use URI;
  1         2  
  1         21  
10 1     1   5 use LWP::UserAgent;
  1         2  
  1         29  
11 1     1   6 use Devel::TakeHashArgs;
  1         2  
  1         133  
12 1     1   5 use base 'Class::Data::Accessor';
  1         2  
  1         352  
13              
14             __PACKAGE__->mk_classaccessors(qw(
15             ua
16             uri
17             error
18             ));
19              
20 1     1   11 use overload q|""| => sub { shift->uri };
  1     0   2  
  1         18  
  0         0  
21              
22             sub new {
23 1     1 1 399 my $self = bless {}, shift;
24 1 50       14 get_args_as_hash( \@_, \ my %args, {
25             timeout => 30,
26             site => 'http://p3m.org/pfn',
27             }
28             ) or croak $@;
29              
30 1   33     52 $args{ua} ||= LWP::UserAgent->new(
31             timeout => $args{timeout},
32             agent => 'Mozilla/5.0 (X11; Ubuntu; Linux i686; '
33             . 'rv:21.0) Gecko/20100101 Firefox/21.0',
34             );
35              
36 1         3815 $self->$_( $args{ $_ } ) for qw(ua site);
37              
38 1         6 return $self;
39             }
40              
41             sub paste {
42 0     0 1 0 my $self = shift;
43 0         0 my $content = shift;
44 0 0       0 get_args_as_hash( \@_, \ my %args, {
45             channel => '',
46             nick => '',
47             summary => '',
48             paste => $content,
49             }, [],
50             [ qw(channel nick summary paste) ],
51             ) or croak $@;
52              
53 0         0 $self->$_(undef) for qw(error uri);
54              
55 0 0       0 defined $args{paste}
56             or return $self->_set_error('Paste content is not defined');
57              
58 0         0 my $uri = URI->new( $self->site . '/paste' );
59              
60 0         0 my $response = $self->ua->post($uri, \%args);
61              
62 0 0       0 $response->code == 303
63             or return $self->_set_error(
64             'Failed to find link to created paste. Are you sure the site'
65             . ' you are using is a correct one? If so, please be kind'
66             . ' and send an email to zoffix@cpan.org so I could fix this'
67             . ' bug. Thank you!');
68            
69 0         0 return $self->uri( URI->new( $response->header('location') ) );
70             }
71              
72             sub site {
73 1     1 1 92 my $self = shift;
74              
75 1 50       6 if ( @_ ) {
76 1         3 $self->{SITE} = shift;
77 1         19 $self->{SITE} =~ s|/$||g;
78             }
79              
80 1         5 return $self->{SITE};
81             }
82              
83             sub _set_error {
84 0     0     my ( $self, $error_or_response, $is_net ) = @_;
85 0 0         if ( $is_net ) {
86 0           $self->error( 'Network error: ' . $error_or_response->status_line );
87             }
88             else {
89 0           $self->error( $error_or_response );
90             }
91 0           return;
92             }
93              
94             1;
95             __END__