blib/lib/Plack/Middleware/QRCode.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Plack::Middleware::QRCode;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 our $VERSION = '0.02';
5 1 ▶use parent qw(Plack::Middleware);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 use Plack::Util::Accessor qw(render config image_type);
7 use Imager;
8 use Imager::QRCode;
9 use Plack::Response;
10 use Plack::Request;
11 use feature qw(say);
12  
13 sub prepare_app {
14 my $self = shift;
15 my $default_config = {
16 size => 4,
17 margin => 2,
18 version => 1,
19 level => 'M',
20 casesensitive => 1,
21 lightcolor => Imager::Color->new(255, 255, 255),
22 darkcolor => Imager::Color->new(0, 0, 0),
23 };
24 my $config = {
25 %$default_config ,
26 %{ $self->render || { } } };
27 $self->config( $config );
28 $self->image_type( 'png' ) unless $self->image_type;
29 }
30  
31 sub call {
32 my ($self,$env) = @_;
33 my $req = Plack::Request->new( $env );
34 my $params = $req->parameters->mixed;
35 my %config = %{ $self->config };
36  
37 $config{size} = $params->{s} if exists $params->{s};
38 $config{margin} = $params->{m};
39  
40 my $pathinfo = substr $env->{'PATH_INFO'},1;
41 say STDERR "Generating QRCode for '" , $pathinfo , "'";
42  
43 my @error = ( 500 , [ 'Content-type' => 'text/plain' ] );
44  
45 return [ @error , [ 'Please enter text for QRCode' ] ] unless $pathinfo;
46  
47 unless ( $config{size} > 0 ) {
48 return [ @error, [ 'Please enter an integer for size param' ] ];
49 }
50  
51 my $qrcode = Imager::QRCode->new( %config );
52 my $img = $qrcode->plot( $pathinfo );
53  
54 my $data = '';
55 $img->write( data => \$data , type => $self->image_type );
56  
57 return [ 200 , [ 'Content-type' => 'image/' . $self->image_type ] , [ $data ] ];
58 }
59  
60 1;
61 __END__
↑ Summary