lib/WWW/Pastebin/Sprunge/Create.pm

filter: Statementstmt 90.7% 59 / 65 Branchbran 45.0% 9 / 20 Conditioncond 37.5% 6 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 92.3% 12 / 13 Podpod 100% 2 / 2 totaltotal 70.4% 88 / 125 CC 18 SCAR 23.4
CC
18
Cov
73%
paste
21.9
new
5.4
_set_error
3.6
CRAP 10.3
« blib/lib/WWW/Pastebin/Sprunge/Create.pm ↑ Summary blib/lib/WWW/Pastebin/Sprunge/Retrieve.pm »
1 package WWW::Pastebin::Sprunge::Create;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 # ABSTRACT: create new pastes on sprunge.us
5 5 our $VERSION = '0.009'; # VERSION
6 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use base 'Class::Data::Accessor';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 __PACKAGE__->mk_classaccessors(qw(
13 ua
14 paste_uri
15 error
16 ));
17  
18 5 ▶use overload q|""| => sub { shift->paste_uri; };
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
19  
20  
21 sub new {
22 4 ▶ my $class = shift;
Subroutine new: called CC=5 SCAR=16.8
Pod documented
23 4 ▶ croak 'Must have even number of arguments to new()'
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 4 4
24 if @_ & 1;
25  
26 4 my %args = @_;
27 4 $args{ +lc } = delete $args{ $_ } for keys %args;
28  
29 4 ▶ $args{timeout} ||= 30;
Condition50%1/2
$args{"timeout"} ||= 30
l!l
04
MC/DC0%0/1
$args{"timeout"} ||= 30
$args{"timeout"}
Truth table50%1/2
A
A= $args{"timeout"}
Aresult
11
00
30 $args{ua} ||= LWP::UserAgent->new(
31 timeout => $args{timeout},
32 4 ▶ agent => 'WWW::Pastebin::Sprunge (+http://p3rl.org/WWW::Pastebin::Sprunge)',
Condition33%1/3
$args{"ua"} ||= "LWP::UserAgent"->new("timeout", $args{"timeout"}, "agent", "WWW::Pastebin::Sprunge (+http://p3rl.org/WWW::Pastebin::Sprunge)")
l!l&&r!l&&!r
040
MC/DC0%0/2
$args{"ua"} ||= "LWP::UserAgent"->new("timeout", $args{"timeout"}, "agent", "WWW::Pastebin::Sprunge (+http://p3rl.org/WWW::Pastebin::Sprunge)")
$args{"ua"} "LWP::UserAgent"->new("timeout", $args{"timeout"}, "agent", "WWW::Pastebin::Sprunge (+http://p3rl.org/WWW::Pastebin::Sprunge)")
Truth table33%1/3
A ||= B
A= $args{"ua"}B= "LWP::UserAgent"->new("timeout", $args{"timeout"}, "agent", "WWW::Pastebin::Sprunge (+http://p3rl.org/WWW::Pastebin::Sprunge)")
ABresult
1X1
011
000
33 );
34  
35 4 my $self = bless {}, $class;
36 4 $self->ua( $args{ua} );
37  
38 4 return $self;
39 }
40  
41  
42 sub paste {
43 3 ▶ my ( $self, $text ) = splice @_, 0, 2;
Subroutine paste: called CC=12 SCAR=30.8
Pod documented
44  
45 3 $self->$_(undef) for qw(paste_uri error);
46  
47 3 ▶ defined $text or carp 'Undefined paste content' and return;
Branch: 1 branch
BranchTrueFalseTotal
defined $text or carp("Undefined paste content") and return 0 3 3
Condition20%1/5
defined $text || carp('Undefined paste content') && (return)
l!l&&r!l&&!r
300
carp("Undefined paste content") and return
!ll
00
MC/DC0%0/2
defined $text || carp('Undefined paste content') && (return)
defined $text carp("Undefined paste content")
Truth table33%1/3
A || B
A= defined $textB= carp("Undefined paste content")
ABresult
1X1
011
000
48  
49 3 ▶ croak 'Must have even number of optional arguments to paste()'
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 3 3
50 if @_ & 1;
51  
52 3 my %args = @_;
53 3 %args = (
54 sprunge => $text,
55 %args,
56 );
57  
58 3 ▶ $args{lang} = lc $args{lang} if $args{lang};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"lang"} 0 3 3
59  
60 $args{file}
61 and not -e $args{sprunge}
62 3 ▶ and return $self->_set_error("File $args{sprunge} does not seem to exist");
Branch: 1 branch
BranchTrueFalseTotal
$args{"file"} and not -e $args{"sprunge"} and return $self->_set_error(qq[File $args{"sprunge"} does not seem to exist]) 0 3 3
Condition66%2/3
$args{"file"} and not -e $args{"sprunge"}
!ll&&!rl&&r
210
MC/DC0%0/2
$args{"file"} and not -e $args{"sprunge"}
$args{"file"} not -e $args{"sprunge"}
Truth table66%2/3
A and not(B)
A= $args{"file"}B= not -e $args{"sprunge"}
ABresult
0X0
100
111
63  
64 3 my $ua = $self->ua;
65 3 $ua->requests_redirectable( [ ] );
66  
67 my @post_request = (
68 'http://sprunge.us',
69 Content_Type => 'form-data',
70 Content => [
71 $args{file}
72 ? (sprunge => [ $args{sprunge}, '' ])
73 3 ▶ : (sprunge => encode_utf8($args{sprunge}))
Branch: 1 branch
BranchTrueFalseTotal
$args{'file'} ? : 1 2 3
74 ],
75 );
76 3 my $response = do {
77 3 local $HTTP::Request::Common::DYNAMIC_FILE_UPLOAD = 1;
78 3 $self->ua->post( @post_request );
79 };
80 3 ▶ if ( $response->is_success() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 3 3
81 0 my $uri = URI->new($response->{_content});
82 0 ▶ return $self->_set_error(q{Request was successful but I don't see a link to the paste }
Branch: 1 branch
BranchTrueFalseTotal
unless $uri 0 0 0
83 . $response->code
84 . $response->content
85 ) unless $uri;
86 0 ▶ $uri->query($args{lang}) if $args{lang};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"lang"} 0 0 0
87 0 return $self->paste_uri($uri);
88 }
89 else {
90 3 return $self->_set_error($response, 'net');
91 }
92 }
93  
94  
95 sub _set_error {
96 3 ▶ my ( $self, $error, $type ) = @_;
Subroutine _set_error: called CC=3 SCAR=12.7
97  
98 3 ▶ if ( defined $type and $type eq 'net' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and $type eq 'net') { } 3 0 3
Condition33%1/3
defined $type and $type eq "net"
!ll&&!rl&&r
003
MC/DC0%0/2
defined $type and $type eq "net"
defined $type $type eq "net"
Truth table33%1/3
A and B
A= defined $typeB= $type eq "net"
ABresult
0X0
100
111
99 3 $self->error( 'Network error: ' . $error->status_line );
100 }
101 else {
102 0 $self->error( $error );
103 }
104 3 return;
105 }
106  
107  
108 5 1;
109  
110 __END__
« blib/lib/WWW/Pastebin/Sprunge/Create.pm ↑ Summary blib/lib/WWW/Pastebin/Sprunge/Retrieve.pm »