File Coverage

blib/lib/Plack/Middleware/DevFavicon.pm
Criterion Covered Total %
statement 18 34 52.9
branch 0 8 0.0
condition n/a
subroutine 6 9 66.6
pod 1 2 50.0
total 25 53 47.1


line stmt bran cond sub pod time code
1             package Plack::Middleware::DevFavicon;
2 1     1   832 use 5.008_001;
  1         3  
  1         30  
3 1     1   6 use strict;
  1         1  
  1         31  
4 1     1   16 use warnings;
  1         2  
  1         55  
5              
6             our $VERSION = "0.02";
7              
8 1     1   750 use parent qw(Plack::Middleware);
  1         300  
  1         4  
9              
10 1     1   459812 use Plack::Util;
  1         3  
  1         41  
11 1     1   3812 use Imager;
  1         92918  
  1         11  
12              
13             sub call {
14 0     0 1   my($self, $env) = @_;
15 0           my $res = $self->app->($env);
16 0 0         if ($env->{PATH_INFO} =~ m{ /favicon\. (?:ico|png) $}xms) {
17 0           return $self->dev_favicon($res);
18             }
19              
20 0           return $res;
21             }
22              
23             sub dev_favicon {
24 0     0 0   my($self, $res) = @_;
25              
26 0           my $data = '';
27             Plack::Util::foreach($res->[2], sub {
28 0     0     my($buf) = @_;
29 0           $data .= $buf;
30 0           });
31              
32 0 0         my $type = Plack::Util::header_get($res->[1], 'content-type') =~ /png/ ? 'png' : 'ico';
33              
34 0 0         my $img = Imager->new(data => $data, type => $type) or die Imager->errstr;
35 0 0         $img = $img->convert(preset => 'gray') or die Imager->errstr;
36              
37 0           my $out;
38 0           $img->write(data => \$out, type => $type);
39              
40 0           return [$res->[0], $res->[1], [$out]];
41             }
42              
43             1;
44             __END__