PrettyCure/Image.pm

filter: Statementstmt 90.2% 46 / 51 Branchbran 50.0% 7 / 14 Conditioncond 38.1% 8 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 5 totaltotal 63.1% 72 / 114 CC 16 SCAR 19.1
CC
16
Cov
71%
imager
20.0
bytes
7.5
default_cache_dir
7.0
CRAP 6.8
« PrettyCure/Image/Terminal.pm ↑ Summary PrettyCure/Team.pm »
1 1 ▶use v5.44;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use experimental qw(class);
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # The image of a girl, from the URL in Acme::PrettyCure::Data: fetched over HTTP(S) once, and kept in
6 # the cache directory (XDG_CACHE_HOME, ~/.cache by default) after that. The images are not part of the
7 # distribution.
8 class Acme::PrettyCure::Image;
9  
10 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 field $url :param :reader;
16  
17 # An HTTP::Tiny, or anything with its `get` (the tests give one of their own).
18 1 field $http :param = undef;
19  
20 1 field $cache_dir :param :reader = default_cache_dir();
21  
22 # The part of the image to keep (left, top, width, height of Imager's `crop`; left and top are 0 when
23 # left out, the others the rest of the image), or the whole image.
24 1 field $crop :param :reader = undef;
25  
26 # $XDG_CACHE_HOME/acme-prettycure, or on Windows %LOCALAPPDATA%\acme-prettycure, or else
27 # ~/.cache/acme-prettycure.
28 2 ▶sub default_cache_dir () {
Subroutine default_cache_dir: called CC=5 SCAR=19.4
Pod undocumented
29 my $base =
30 $ENV{XDG_CACHE_HOME}
31 || ($^O eq 'MSWin32' && $ENV{LOCALAPPDATA})
32 2 ▶ || File::Spec->catdir($ENV{HOME} // File::Spec->tmpdir, '.cache');
Condition50%6/12
$ENV{'XDG_CACHE_HOME'} || $^O eq 'MSWin32' && $ENV{'LOCALAPPDATA'} || 'File::Spec'->catdir($ENV{'HOME'} // 'File::Spec'->tmpdir, '.cache')
l!l&&r!l&&!r
110
$ENV{"XDG_CACHE_HOME"} or $^O eq "MSWin32" and $ENV{"LOCALAPPDATA"}
l!l&&r!l&&!r
101
$^O eq "MSWin32" and $ENV{"LOCALAPPDATA"}
!ll&&!rl&&r
100
$ENV{"HOME"} // "File::Spec"->tmpdir
l!l&&r!l&&!r
100
MC/DC0%0/4
$ENV{'XDG_CACHE_HOME'} || $^O eq 'MSWin32' && $ENV{'LOCALAPPDATA'} || 'File::Spec'->catdir($ENV{'HOME'} // 'File::Spec'->tmpdir, '.cache')
$ENV{"XDG_CACHE_HOME"} $^O eq "MSWin32" $ENV{"LOCALAPPDATA"} 'File::Spec'->catdir($ENV{'HOME'} // 'File::Spec'->tmpdir, '.cache')
MC/DC0%0/2
$ENV{"HOME"} // "File::Spec"->tmpdir
$ENV{"HOME"} "File::Spec"->tmpdir
Truth table33%2/6
A or B and C || D
A= $ENV{"XDG_CACHE_HOME"}B= $^O eq "MSWin32"C= $ENV{"LOCALAPPDATA"}D= 'File::Spec'->catdir($ENV{'HOME'} // 'File::Spec'->tmpdir, '.cache')
ABCDresult
1XXX1
011X1
00X11
01011
00X00
01000
Truth table33%1/3
A // B
A= $ENV{"HOME"}B= "File::Spec"->tmpdir
ABresult
1X1
011
000
33 2 return File::Spec->catdir($base, 'acme-prettycure');
34 }
35  
36 # The file of the image in the cache: named by the URL, with its extension.
37 6 ▶method cache_file {
Subroutine cache_file: called CC=2 SCAR=7.2
Pod undocumented
38 6 my ($ext) = $url =~ m{(\.[A-Za-z0-9]+)(?:\?.*)?\z};
39 6 ▶ return File::Spec->catfile($cache_dir, md5_hex($url) . lc($ext // ''));
Condition50%1/2
$ext // ""
l!l
60
MC/DC0%0/1
$ext // ""
$ext
Truth table50%1/2
A
A= $ext
Aresult
11
00
40 }
41  
42 # The bytes of the image, from the cache or else fetched (and then kept in the cache).
43 3 ▶method bytes {
Subroutine bytes: called CC=7 SCAR=20.2
Pod undocumented
44 3 my $file = $self->cache_file;
45 3 ▶ return slurp($file) if -s $file;
Branch: 1 branch
BranchTrueFalseTotal
if -s $file 1 2 3
46  
47 2 require HTTP::Tiny;
48 2 ▶ $http //= HTTP::Tiny->new(agent => 'Acme-PrettyCure/' . ($Acme::PrettyCure::VERSION // 'dev'));
Condition20%1/5
$http //= "HTTP::Tiny"->new("agent", 'Acme-PrettyCure/' . ($Acme::PrettyCure::VERSION // "dev"))
l!l&&r!l&&!r
200
$Acme::PrettyCure::VERSION // "dev"
l!l
00
MC/DC0%0/2
$http //= "HTTP::Tiny"->new("agent", 'Acme-PrettyCure/' . ($Acme::PrettyCure::VERSION // "dev"))
$http "HTTP::Tiny"->new("agent", 'Acme-PrettyCure/' . ($Acme::PrettyCure::VERSION // "dev"))
MC/DC0%0/1
$Acme::PrettyCure::VERSION // "dev"
$Acme::PrettyCure::VERSION
Truth table33%1/3
A //= B
A= $httpB= "HTTP::Tiny"->new("agent", 'Acme-PrettyCure/' . ($Acme::PrettyCure::VERSION // "dev"))
ABresult
1X1
011
000
Truth table0%0/2
A
A= $Acme::PrettyCure::VERSION
Aresult
11
00
49 2 my $res = $http->get($url);
50 2 ▶ die "can't get $url: $res->{status} $res->{reason}\n" unless $res->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"success"} 1 1 2
51  
52 # Written to a file of its own first: a fetch cut off halfway is never taken from the cache.
53 1 make_path($cache_dir);
54 1 my $tmp = File::Temp->new(DIR => $cache_dir, UNLINK => 0);
55 1 binmode $tmp;
56 1 print {$tmp} $res->{content};
57 1 ▶ close $tmp or die "$tmp: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $tmp or die "${tmp}: $!" 1 0 1
58 1 ▶ rename "$tmp", $file or die "$file: $!";
Branch: 1 branch
BranchTrueFalseTotal
rename "$tmp", $file or die "${file}: $!" 1 0 1
59 1 return $res->{content};
60 }
61  
62 # The image, as an Imager object, cut to `crop`. The format is told from the bytes; JPEG and GIF need
63 # the Imager::File::JPEG and Imager::File::GIF plugins (and libjpeg and giflib).
64 0 ▶method imager {
Subroutine imager: not called CC=4 SCAR=30.0
Pod undocumented
65 0 require Imager;
66 0 my $img = Imager->new;
67 0 ▶ $img->read(data => $self->bytes) or die "can't read the image of $url: " . $img->errstr . "\n";
Branch: 1 branch
BranchTrueFalseTotal
$img->read("data", $self->bytes) or die "can't read the image of ${url}: " . $img->errstr . "\n" 0 0 0
68 0 ▶ return $img unless $crop;
Branch: 1 branch
BranchTrueFalseTotal
unless $crop 0 0 0
69  
70 # From the top left: Imager's `crop` takes the middle when left and top are left out.
71 0 ▶ return $img->crop(left => 0, top => 0, %$crop)
Condition0%0/2
$img->crop("left", 0, "top", 0, %$crop) // die("can't crop the image of ${url}: " . $img->errstr . "\n")
l!l
00
MC/DC0%0/1
$img->crop("left", 0, "top", 0, %$crop) // die("can't crop the image of ${url}: " . $img->errstr . "\n")
$img->crop("left", 0, "top", 0, %$crop)
Truth table0%0/2
A
A= $img->crop("left", 0, "top", 0, %$crop)
Aresult
11
00
72 // die "can't crop the image of $url: " . $img->errstr . "\n";
73 }
74  
75 1 ▶sub slurp ($file) {
Subroutine slurp: called CC=2 SCAR=7.1
Pod undocumented
76 1 ▶ open my $fh, '<:raw', $file or die "$file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or die "${file}: $!" 1 0 1
77 1 local $/;
78 1 return scalar <$fh>;
79 }
80  
81 1 1;
82  
83 __END__
« PrettyCure/Image/Terminal.pm ↑ Summary PrettyCure/Team.pm »