| 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
| |||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$args{"timeout"} ||= 30
$args{"timeout"}
Truth table100%2/2
A
A= $args{"timeout"}
| |||||||||||||||||||||||||||||||||
| 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)")
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)")
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 51 | unless $id; | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | (my $uri, $id) = $self->_make_uri_and_id($id, @_) or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 95 | 0 | $id = $1; | |||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | $id = $in unless defined $id; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $content && length $content
MC/DC0%0/2
defined $content && length $content
defined $content
length $content
Truth table0%0/3
A && B
A= defined $contentB= length $content
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||