| 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
| |||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$args{"timeout"} ||= 30
$args{"timeout"}
Truth table50%1/2
A
A= $args{"timeout"}
| |||||||||||||||||||||||||||||||||
| 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)")
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)")
| |||||||||||||||||||||||||||||||||
| 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
Condition20%1/5
defined $text || carp('Undefined paste content') && (return)
carp("Undefined paste content") and return
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")
| |||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 3 | ▶ | croak 'Must have even number of optional arguments to paste()' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$args{"file"} and not -e $args{"sprunge"}
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"}
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 83 | . $response->code | ||||||||||||||||||||||||||||||||
| 84 | . $response->content | ||||||||||||||||||||||||||||||||
| 85 | ) unless $uri; | ||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | $uri->query($args{lang}) if $args{lang}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $type and $type eq "net"
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"
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||