File Coverage

blib/lib/WWW/Pastebin/PastebinCa/Create.pm
Criterion Covered Total %
statement 59 63 93.6
branch 15 26 57.6
condition 3 10 30.0
subroutine 14 15 93.3
pod 7 7 100.0
total 98 121 80.9


line stmt bran cond sub pod time code
1             package WWW::Pastebin::PastebinCa::Create;
2              
3 1     1   265806 use warnings;
  1         2  
  1         31  
4 1     1   5 use strict;
  1         2  
  1         45  
5              
6             our $VERSION = '0.004';
7 1     1   4 use Carp;
  1         6  
  1         62  
8 1     1   5 use URI;
  1         1  
  1         54  
9 1     1   4 use WWW::Mechanize;
  1         2  
  1         329  
10              
11             my %Valid_Langs = valid_langs();
12             my %Valid_Expires = map { $_ => $_ } valid_expires();
13 1     1   6 use overload q|""| => sub { shift->paste_uri };
  1     0   1  
  1         13  
  0         0  
14              
15             sub new {
16 1     1 1 313 my $self = bless {}, shift;
17 1 50       5 croak "Must have even number of arguments to new()"
18             if @_ & 1;
19              
20 1         3 my %args = @_;
21 1         5 $args{ +lc } = delete $args{ $_ } for keys %args;
22              
23 1   50     9 $args{timeout} ||= 30;
24 1   33     16 $args{mech} ||= WWW::Mechanize->new(
25             autocheck => 0,
26             timeout => $args{timeout},
27             agent => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.12)'
28             .' Gecko/20080207 Ubuntu/7.10 (gutsy) Firefox/2.0.0.12',
29             );
30              
31 1         40037 $self->mech( $args{mech} );
32              
33 1         7 return $self;
34             }
35              
36             sub paste {
37 1     1 1 34 my ( $self, $content ) = splice @_, 0, 2;
38              
39 1         8 $self->$_(undef) for qw(paste_uri error);
40              
41 1 50 0     20 defined $content
42             or carp "first argument to paste() is not defined" and return;
43              
44 1 50       36 croak "Must have even number of optional arguments to paste()"
45             if @_ & 1;
46              
47 1         5 my %args = @_;
48 1         10 $args{ +lc } = delete $args{ $_ } for keys %args;
49 1         11 %args = (
50             content => $content,
51             name => '',
52             desc => '',
53             tags => '',
54             lang => 1,
55             expire => '',
56             %args,
57             );
58              
59 1 50       8 croak q|Invalid 'lang' was specified to paste(). |
60             . q|print Dumper { WWW::Pastebin::PastebinCa::Create::valid_langs }|
61             unless exists $Valid_Langs{ $args{lang} };
62              
63 1 50 33     15 croak q|Invalid 'expire' was specified to paste(). |
64             . q|print Dumper { WWW::Pastebin::PastebinCa::Create::valid_expires }|
65             if length $args{expire}
66             and not exists $Valid_Expires{ $args{expire} };
67              
68 1         5 my $mech = $self->mech;
69 1         10 $mech->get('http://pastebin.ca');
70              
71 1 50       377415 return $self->_set_error('Network error: ' . $mech->res->status_line)
72             unless $mech->success;
73              
74 1 50       32 $mech->form_with_fields( 'content' )
75             or return $self->_set_error('Paste form was not found');
76              
77 1         41785 my $set = $mech->set_visible(
78             $args{content},
79             [ text => $args{name} ],
80             [ textarea => $args{desc} ],
81             [ text => $args{tags} ],
82             [ option => $args{lang} ],
83             [ option => $args{expire} ],
84             );
85              
86 1 50       341 $set == 6
87             or return $self->_set_error("Failed to set all fields (only $set)");
88              
89 1 50       8 $mech->click('s')->is_success
90             or return $self->_set_error(
91             'Network error: ' . $mech->res->status_line
92             );
93              
94 0         0 my ( $uri ) = $mech->content
95             =~ m|
96              
97 0 0       0 defined $uri
98             or return $self->_set_error('Failed to locate link to paste');
99              
100 0         0 return $self->paste_uri( URI->new($uri) );
101             }
102              
103             sub _set_error {
104 1     1   268428 my ( $self, $error ) = @_;
105 1         7 $self->error( $error );
106 1         9 return;
107             }
108              
109             sub valid_langs {
110             return (
111 1     1 1 37 1 => 'Raw',
112             2 => 'Asterisk Configuration',
113             3 => 'C Source',
114             4 => 'C++ Source',
115             5 => 'PHP Source',
116             6 => 'Perl Source',
117             7 => 'Java Source',
118             8 => 'Visual Basic Source',
119             9 => 'C# Source',
120             10 => 'Ruby Source',
121             11 => 'Python Source',
122             12 => 'Pascal Source',
123             13 => 'mIRC Script',
124             14 => 'PL/I Source',
125             15 => 'XML Document',
126             16 => 'SQL Statement',
127             17 => 'Scheme Source',
128             18 => 'Action Script',
129             19 => 'Ada Source',
130             20 => 'Apache Configuration',
131             21 => 'Assembly (NASM)',
132             22 => 'ASP',
133             23 => 'BASH Script',
134             24 => 'CSS',
135             25 => 'Delphi Source',
136             26 => 'HTML 4.0 Strict',
137             27 => 'JavaScript',
138             28 => 'LISP Source',
139             29 => 'Lua Source',
140             30 => 'Microprocessor ASM',
141             31 => 'Objective C',
142             32 => 'Visual Basic .NET',
143             33 => 'Script Log',
144             34 => 'Diff / Patch',
145             );
146             }
147              
148             sub valid_expires {
149             return (
150 1     1 1 5 '2 hours',
151             '4 hours',
152             '1 year',
153             '2 weeks',
154             '45 minutes',
155             '2 months',
156             '30 minutes',
157             '1 week',
158             '1 hour',
159             '15 minutes',
160             '10 minutes',
161             '3 days',
162             '5 months',
163             '4 months',
164             '5 minutes',
165             '8 hours',
166             '2 days',
167             '3 months',
168             '1 day',
169             '12 hours',
170             '3 weeks',
171             '6 months',
172             '1 month',
173             );
174             }
175              
176             sub mech {
177 3     3 1 1963 my $self = shift;
178 3 100       13 if ( @_ ) { $self->{MECH} = shift };
  1         53  
179 3         12 return $self->{MECH};
180             }
181              
182             sub paste_uri {
183 2     2 1 5 my $self = shift;
184 2 100       11 if ( @_ ) { $self->{PASTE_URI} = shift };
  1         3  
185 2         9 return $self->{PASTE_URI};
186             }
187              
188             sub error {
189 6     6 1 790 my $self = shift;
190 6 100       16 if ( @_ ) { $self->{ERROR} = shift };
  2         8  
191 6         19 return $self->{ERROR};
192             }
193              
194             1;
195             __END__