File Coverage

blib/lib/Acme/PrettyCure/Girl/Role.pm
Criterion Covered Total %
statement 36 49 73.4
branch 7 20 35.0
condition n/a
subroutine 11 12 91.6
pod 2 5 40.0
total 56 86 65.1


line stmt bran cond sub pod time code
1             package Acme::PrettyCure::Girl::Role;
2 16     16   173111 use utf8;
  16         34  
  16         191  
3 16     16   432 use Moo::Role;
  16         29  
  16         101  
4              
5 16     16   24222 use Encode;
  16         101898  
  16         1700  
6 16     16   23390 use Furl;
  16         580333  
  16         239  
7 16     16   13652 use Cache::LRU;
  16         10727  
  16         220  
8 16     16   15828 use Net::DNS::Lite;
  16         136595  
  16         1027  
9 16     16   25485 use Imager;
  16         837266  
  16         141  
10              
11             $Net::DNS::Lite::CACHE = Cache::LRU->new(size => 256);
12              
13             requires qw(human_name precure_name challenge image_url);
14              
15             has 'is_precure' => (
16             is => 'rw',
17             isa => sub { die "$_[0] is not a boolean" if $_[0] !~ /^[01]$/ },
18             default => sub {0}
19             );
20              
21             sub say {
22 109     109 0 175 my ($self, $text) = @_;
23 109         339 my $color = $self->color;
24 109 100       251 if ( defined $color ) {
25 93         250 print "\e[38;5;${color}m";
26             }
27 109         413 print encode_utf8("$text\n");
28 109 100       1806 if ( defined $color ) {
29 93         939 print "\e[0m";
30             }
31             }
32              
33 16     16 0 26 sub color { undef }
34              
35             sub name {
36 70     70 1 18916 my $self = shift;
37              
38 70 100       248 return $self->is_precure ? $self->precure_name : $self->human_name;
39             }
40              
41             sub transform {
42 53     53 1 10616 my ($self, $buddy) = @_;
43              
44 53 50       232 die "already transformed" if $self->is_precure;
45              
46 53         3100 $self->is_precure(1);
47              
48 53         600 $self->say($_) for $self->challenge;
49              
50 53         1158 return $self;
51             }
52              
53             sub image {
54 0     0 0   my $self = shift;
55              
56 0           my $furl = Furl::HTTP->new(
57             agent => 'Acme-PrettyCure',
58             inet_aton => \&Net::DNS::Lite::inet_aton,
59             timeout => 60,
60             );
61 0           my ( $minor_version, $status, $message, $headers, $content ) =
62             $furl->request( method => 'GET', url => $self->image_url, );
63              
64 0           my $img = Imager->new();
65 0           my $type;
66 0 0         if ($self->image_url =~ /\.gif$/) {
    0          
    0          
67 0           $type = 'gif';
68             }
69             elsif ($self->image_url =~ /\.png$/) {
70 0           $type = 'png';
71             }
72             elsif ($self->image_url =~ /\.jpe?g$/) {
73 0           $type = 'jpeg';
74             }
75 0 0         $img->read(data => $content, type => $type) or die $img->errstr;
76              
77 0 0         open my $ah, '|-', qw/aview -reverse -driver curses/ or die "aview:$!";
78 0 0         $img->write( fh => $ah, type => 'pnm' ) or die $img->errstr;
79 0           close($ah);
80             }
81              
82             1;
83             __END__