File Coverage

blib/lib/Plack/Middleware/Watermark.pm
Criterion Covered Total %
statement 37 38 97.3
branch 10 12 83.3
condition 5 8 62.5
subroutine 9 9 100.0
pod 2 2 100.0
total 63 69 91.3


line stmt bran cond sub pod time code
1             package Plack::Middleware::Watermark;
2              
3 3     3   3223 use strict;
  3         7  
  3         120  
4 3     3   17 use warnings;
  3         6  
  3         154  
5              
6             our $VERSION = '0.01';
7              
8 3     3   1018 use parent qw( Plack::Middleware );
  3         378  
  3         18  
9 3     3   23525 use Plack::Util::Accessor qw( comment );
  3         11  
  3         26  
10              
11 3     3   134 use Plack::Util;
  3         6  
  3         59  
12 3     3   16 use Carp ();
  3         5  
  3         1233  
13              
14             sub prepare_app {
15 13     13 1 11718 my $self = shift;
16              
17 13 50       34 unless ($self->comment) {
18 0         0 Carp::croak "'comment' is not defined";
19             }
20             }
21              
22             my %comment_style = (
23             'html' => [ '' ],
24             'xml' => [ '' ],
25             'css' => [ '/*', '*/' ],
26             'js' => [ '//', '' ],
27             );
28              
29             my %mime_type = (
30             # html
31             'text/html' => 'html',
32             'application/xhtml+xml' => 'html',
33             # xml
34             'text/xml' => 'xml',
35             'application/xml' => 'xml',
36             'application/rss+xml' => 'xml',
37             'application/atom+xml' => 'xml',
38             # css
39             'text/css' => 'css',
40             # js
41             'text/javascript' => 'js',
42             'application/javascript' => 'js',
43             );
44              
45             sub call {
46 13     13 1 63527 my $self = shift;
47 13         21 my ($env) = @_;
48              
49 13         45 my $res = $self->app->($env);
50             $self->response_cb($res, sub {
51 13     13   167 my $res = shift;
52 13         41 my $type = Plack::Util::header_get($res->[1], 'Content-Type');
53 13 50 33     330 if ($type && $res->[0] == 200) {
54 13         43 ($type) = split /;\s*/, $type;
55 13 100 100     19 my ($start, $stop) = @{ $comment_style{$mime_type{$type}||''} || [] };
  13         72  
56 13 100 66     66 if ($start or $stop) {
57             return sub {
58 24         418 my $chunk = shift;
59 24 100       98 return unless defined $chunk;
60 12 100       32 my $comment = ref $self->comment eq 'CODE' ? $self->comment->($env) : $self->comment;
61 12         143 $chunk .= join ' ', $start, $comment, $stop;
62 12         163 return $chunk;
63 12         61 };
64             }
65             }
66 13         197 });
67             }
68              
69             1;
70             __END__