lib/WWW/Pastebin/Sprunge/Retrieve.pm

filter: Statementstmt 79.7% 59 / 74 Branchbran 38.8% 7 / 18 Conditioncond 37.5% 3 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 80.0% 12 / 15 Podpod 100% 2 / 2 totaltotal 68.8% 84 / 122 CC 15 SCAR 16.9
CC
15
Cov
69%
_parse
20.0
retrieve
5.3
new
5.2
CRAP 5.4
« blib/lib/WWW/Pastebin/Sprunge/Retrieve.pm ↑ Summary Build »
1 package WWW::Pastebin::Sprunge::Retrieve;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 # ABSTRACT: retrieves pastes from the sprunge.us pastebin
5 6 our $VERSION = '0.009'; # VERSION
6 6 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use base 'Class::Data::Accessor';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 __PACKAGE__->mk_classaccessors(qw(
12 ua
13 uri
14 id
15 content
16 error
17 results
18 ));
19  
20 6 ▶use overload q|""| => sub { shift->content };
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
21  
22  
23  
24 sub new {
25 5 ▶ my $class = shift;
Subroutine new: called CC=5 SCAR=16.4
Pod documented
26 5 ▶ croak 'new() takes an even number of arguments' if @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 5 5
27  
28 5 my %args = @_;
29 5 $args{ +lc } = delete $args{ $_ } for keys %args;
30  
31 5 ▶ $args{timeout} ||= 30;
Condition100%2/2
$args{"timeout"} ||= 30
l!l
14
MC/DC100%1/1
$args{"timeout"} ||= 30
$args{"timeout"}
Truth table100%2/2
A
A= $args{"timeout"}
Aresult
11
00
32 $args{ua} ||= LWP::UserAgent->new(
33 timeout => $args{timeout},
34 5 ▶ 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
050
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
35 );
36  
37 5 my $self = bless {}, $class;
38 5 $self->ua( $args{ua} );
39  
40 5 return $self;
41 }
42  
43  
44 sub retrieve {
45 1 ▶ my $self = shift;
Subroutine retrieve: called CC=5 SCAR=16.6
Pod documented
46 1 my $id = shift;
47  
48 1 $self->$_(undef) for qw( error uri id results );
49  
50 1 ▶ return $self->_set_error('Missing or empty paste ID/URL')
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 1 1
51 unless $id;
52  
53 1 ▶ (my $uri, $id) = $self->_make_uri_and_id($id, @_) or return;
Branch: 1 branch
BranchTrueFalseTotal
(my $uri, $id) = $self->_make_uri_and_id($id, @_) or return 0 1 1
54  
55 1 $self->uri($uri);
56 1 $self->id($id);
57  
58 1 my $ua = $self->ua;
59 1 my $response = $ua->get($uri);
60 1 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 1 1
61 0 return $self->_get_was_successful($response->content);
62 }
63 else {
64 1 return $self->_set_error('Network error: ' . $response->status_line);
65 }
66 }
67  
68 sub _get_was_successful {
69 0 ▶ my $self = shift;
Subroutine _get_was_successful: not called CC=1 SCAR=6.9
70 0 my $content = shift;
71  
72 0 return $self->results( $self->_parse($content) );
73 }
74  
75 sub _set_error {
76 1 ▶ my $self = shift;
Subroutine _set_error: called CC=2 SCAR=7.1
77 1 my $err_or_res = shift;
78 1 my $is_net_error = shift;
79  
80 1 ▶ if (defined $is_net_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $is_net_error) { } 0 1 1
81 0 $self->error('Network error: ' . $err_or_res->status_line);
82 }
83 else {
84 1 $self->error($err_or_res);
85 }
86 1 return;
87 }
88  
89 sub _make_uri_and_id {
90 1 ▶ my $self = shift;
Subroutine _make_uri_and_id: called CC=3 SCAR=11.6
91 1 my $in = shift;
92  
93 1 my $id;
94 1 ▶ if ( $in =~ m{ (?:http://)? (?:www\.)? sprunge.us/ (\S+?) (?:\?\w+)? $}ix ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ m[ (?:http://)? (?:www\.)? sprunge.us/ (\S+?) (?:\?\w+)? $]xi) 0 1 1
95 0 $id = $1;
96 }
97 1 ▶ $id = $in unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 1 0 1
98  
99 1 return ( URI->new("http://sprunge.us/$id"), $id );
100 }
101  
102 sub _parse {
103 0 ▶ my $self = shift;
Subroutine _parse: not called CC=4 SCAR=30.0
104 0 my $content = shift;
105 0 my $id = $self->id;
106  
107 0 ▶ if (!defined($content) or !length($content)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $content && length $content) { } 0 0 0
elsif ($content eq "$id not found") { } 0 0 0
Condition0%0/3
defined $content && length $content
!ll&&!rl&&r
000
MC/DC0%0/2
defined $content && length $content
defined $content length $content
Truth table0%0/3
A && B
A= defined $contentB= length $content
ABresult
0X0
100
111
108 0 return $self->_set_error('Nothing to parse (empty document retrieved)');
109 }
110 elsif ($content eq "$id not found") {
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 6 1;
128  
129 __END__
« blib/lib/WWW/Pastebin/Sprunge/Retrieve.pm ↑ Summary Build »