File Coverage

blib/lib/App/Nopaste/Service.pm
Criterion Covered Total %
statement 42 55 76.3
branch 7 20 35.0
condition 2 6 33.3
subroutine 11 12 91.6
pod 6 9 66.6
total 68 102 66.6


line stmt bran cond sub pod time code
1 11     11   267648 use strict;
  11         57  
  11         347  
2 11     11   56 use warnings;
  11         22  
  11         586  
3             package App::Nopaste::Service;
4             # ABSTRACT: base class for nopaste services
5              
6             our $VERSION = '1.013';
7              
8 11     11   8591 use WWW::Mechanize;
  11         1654018  
  11         7001  
9              
10 1     1 0 4 sub available { 1 }
11 0     0 0 0 sub forbid_in_default { 0 }
12              
13             # this wrapper is so we can canonicalize arguments, especially "lang"
14             sub nopaste {
15 2     2 1 6 my $self = shift;
16 2         18 $self->run(@_);
17             }
18              
19             sub run {
20 1     1 1 2 my $self = shift;
21 1         6 my $mech = WWW::Mechanize->new;
22              
23 1         14506 $self->get($mech => @_);
24 1         16 $self->fill_form($mech => @_);
25 1         3862 return $self->return($mech => @_);
26             }
27              
28             sub uri {
29 1   33 1 1 229 my $class = ref($_[0]) || $_[0];
30 1         194 Carp::croak "$class must provide a 'uri' method.";
31             }
32              
33             sub get {
34 1     1 1 2 my $self = shift;
35 1         3 my $mech = shift;
36              
37 1         6 my $res = $mech->get($self->uri);
38 1 50       24515 die "Unable to fetch ".$self->uri.": " . $res->status_line
39             unless $res->is_success;
40 1         11 return $res;
41             }
42              
43             sub fill_form {
44 1     1 1 3 my $self = shift;
45 1         2 my $mech = shift;
46 1         13 my %args = @_;
47              
48 1         6 $mech->form_number(1);
49 1         9931 $args{chan} = $self->canonicalize_chan($mech, $args{chan});
50              
51             $mech->submit_form(
52             fields => {
53             paste => $args{text},
54 1 50       4 do { $args{chan} ? (channel => $args{chan}) : () },
55 1 50       3 do { $args{desc} ? (summary => $args{desc}) : () },
56 1 50       12 do { $args{nick} ? (nick => $args{nick}) : () },
57 1 50 33     3 private => (exists $args{private} && $args{private} ? 1 : 0),
58             },
59             );
60             }
61              
62             sub canonicalize_chan {
63 1     1 0 3 my $self = shift;
64 1         2 my $mech = shift;
65 1         3 my $chan = shift;
66              
67 1 50       4 return $chan if !$chan;
68              
69 0         0 my @chans = grep { length }
  0         0  
70             $mech->current_form->find_input('channel')->possible_values;
71 0         0 my %is_valid = map { $_ => 1 } @chans;
  0         0  
72              
73 0 0       0 return $chan if $is_valid{$chan};
74              
75 0         0 my $orig = $chan;
76 0         0 $chan =~ s/^\#//;
77 0 0       0 return $chan if $is_valid{$chan};
78              
79 0         0 $chan = "#$chan";
80 0 0       0 return $chan if $is_valid{$chan};
81              
82 0         0 warn "Invalid channel '$orig'. Valid values are: " . join(', ', @chans);
83 0         0 return $orig;
84             }
85              
86             sub return {
87 1     1 1 3 my $self = shift;
88 1         2 my $mech = shift;
89              
90 1         10 my $link = $mech->find_link(url_regex => qr{/?(?:\d+)$});
91              
92 1 50       2845 return (0, "Could not find paste link.") if !$link;
93 1         13 return (1, $link->url);
94             }
95              
96             1;
97              
98             __END__