AnyPAN/Agent.pm

filter: Statementstmt 66.6% 36 / 54 Branchbran 10.0% 1 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 2 totaltotal 58.5% 48 / 82 CC 7 SCAR 23.0
CC
7
Cov
57%
__ANON__
30.0
download
3.8
new
2.1
CRAP 10.0
« AnyPAN/ReverseProxy.pm ↑ Summary AnyPAN/Index/Merged.pm »
1 package AnyPAN::Agent;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Class::Accessor::Lite ro => [qw/furl retry_policy logger/];
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Furl;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Furl::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use AnyPAN::Logger::Null;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
Pod undocumented
13 1 my ($class, %args) = @_;
14 1 my $retry_policy = delete $args{retry_policy};
15 1 ▶ my $logger = delete $args{logger} || AnyPAN::Logger::Null->instance();
Condition33%1/3
delete $args{'logger'} || 'AnyPAN::Logger::Null'->instance
l!l&&r!l&&!r
100
MC/DC0%0/2
delete $args{'logger'} || 'AnyPAN::Logger::Null'->instance
delete $args{'logger'} 'AnyPAN::Logger::Null'->instance
Truth table33%1/3
A || B
A= delete $args{'logger'}B= 'AnyPAN::Logger::Null'->instance
ABresult
1X1
011
000
16 1 bless {
17 furl => Furl->new(%args),
18 retry_policy => $retry_policy,
19 logger => $logger,
20 }, $class;
21 }
22  
23 6 ▶sub download {
Subroutine download: called CC=2 SCAR=13.4
Pod undocumented
24 6 my ($self, $url, $path) = @_;
25  
26 # file:///...
27 6 ▶ if ($url->scheme eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($url->scheme eq "file") 6 0 6
28 6 $self->logger->debug("copy @{[ $url->file ]} to $path");
29  
30 6 my $src = Path::Tiny->new($url->file);
31 6 $src->copy($path);
32 6 return Furl::Response->new(0, 200, 'OK', [
33 'Content-Type' => 'application/octet-stream',
34 'Content-Length' => -s $src,
35 ], 'DUMMY');
36 }
37  
38 0 ▶ return $self->retry_policy->apply_and_doit(sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
39 0 my ($retry_count, $e) = @_;
40 0 ▶ $self->logger->warn("retry request: $retry_count", { error => $e }) if $retry_count;
Branch: 1 branch
BranchTrueFalseTotal
if $retry_count 0 0 0
41 0 $self->logger->debug("download $url to $path");
42  
43 0 my $tempfile = Path::Tiny->tempfile(UNLINK => 1);
44  
45 0 my $fh = $tempfile->openw_raw();
46 0 my $res = $self->furl->request(
47 method => 'GET',
48 url => $url,
49 write_file => $fh,
50 );
51 0 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "$!: $tempfile" 0 0 0
52 or die "$!: $tempfile";
53  
54 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
55 0 $self->logger->debug("error status: @{[ $res->status_line ]}");
56 0 ▶ if ($res->status == 404) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->status == 404) 0 0 0
57 0 AnyPAN::Agent::Exception::NotFound->throw(
58 0 message => "Failed to download: $url (@{[ $res->status_line ]})",
59 );
60 }
61 0 die "Failed to download: $url (@{[ $res->status_line ]})";
62 }
63  
64 0 $tempfile->copy($path);
65  
66 0 return $res;
67 0 });
68 }
69  
70 package # hide from PAUSE
71 AnyPAN::Agent::Exception::NotFound;
72  
73 2 ▶use parent qw/Exception::Tiny/;
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75  
76 2 1;
77 __END__
« AnyPAN/ReverseProxy.pm ↑ Summary AnyPAN/Index/Merged.pm »