blib/lib/Dancer2/Plugin/Negotiate.pm

filter: Statementstmt 96.4% 54 / 56 Branchbran 67.8% 19 / 28 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 90.0% 9 / 10 Podpod 100% 3 / 3 totaltotal 81.8% 90 / 110 CC 18 SCAR 15.3
CC
18
Cov
84%
_langmap
8.1
apply_variant
6.0
choose_variant
4.0
CRAP 4.6
↑ Summary
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Dancer2::Plugin::Negotiate;
5  
6 # ABSTRACT: Content negotiation plugin for Dancer2
7  
8 3 ▶use Dancer2::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use HTTP::Negotiate;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = '0.002'; # VERSION
13  
14 7 ▶sub choose_variant {
Subroutine choose_variant: called CC=4 SCAR=14.0
Pod documented
15 7 my $dsl = shift;
16 7 my $app = $dsl->app;
17 7 my $variants = [];
18 7 ▶ while ( my ( $variant, $options ) = ( shift, shift ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = (shift(), shift())) 24 0 24
19 24 ▶ last unless defined $variant and defined $options;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $variant and defined $options 7 17 24
Condition66%2/3
defined $variant and defined $options
!ll&&!rl&&r
7017
MC/DC50%1/2
defined $variant and defined $options
defined $variant defined $options
Truth table66%2/3
A and B
A= defined $variantB= defined $options
ABresult
0X0
100
111
20 push @$variants => [
21 $variant, $options->{Quality}, $options->{Type},
22 $options->{Encoding}, $options->{Charset}, $options->{Language},
23 $options->{Size}
24 17 ];
25 }
26 7 HTTP::Negotiate::choose( $variants, $app->request->headers, );
27 }
28  
29 5 ▶sub apply_variant {
Subroutine apply_variant: called CC=6 SCAR=18.0
Pod documented
30 5 my ( $dsl, %variants ) = @_;
31 5 my $app = $dsl->app;
32 5 my $variant = scalar choose_variant(@_);
33 5 ▶ return unless defined $variant;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $variant 0 5 5
34 5 my %options = %{ $variants{$variant} };
35 5 my $R = $app->response;
36 5 ▶ $R->header( 'Content-Type' => $options{Type} ) if defined $options{Type};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"Type"} 2 3 5
37 $R->header( 'Content-Encoding' => $options{Encoding} )
38 5 ▶ if defined $options{Encoding};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"Encoding"} 0 5 5
39 $R->header( 'Content-Charset' => $options{Charset} )
40 5 ▶ if defined $options{Charset};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"Charset"} 2 3 5
41 $R->header( 'Content-Language' => $options{Language} )
42 5 ▶ if defined $options{Language};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"Language"} 4 1 5
43 5 $variant;
44 }
45  
46 3 ▶sub _langmap {
Subroutine _langmap: called CC=6 SCAR=20.9
47 0 ▶ my $grep = shift || sub { 1 };
Subroutine __ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
shift() || sub { 1; }
l!l&&r!l&&!r
300
MC/DC0%0/2
shift() || sub { 1; }
shift() sub { 1; }
Truth table33%1/3
A || B
A= shift()B= sub { 1; }
ABresult
1X1
011
000
48 3 ▶ my $langs = plugin_setting->{languages} || {};
Condition50%1/2
plugin_setting()->{'languages'} || {}
l!l
30
MC/DC0%0/1
plugin_setting()->{'languages'} || {}
plugin_setting()->{'languages'}
Truth table50%1/2
A
A= plugin_setting()->{'languages'}
Aresult
11
00
49 return grep defined, map {
50 3 ▶ my $opt = {
Branch: 2 branches
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 0 6 6
ref $_ eq 'HASH' ? : 0 6 6
51 Language => scalar( ref $_ eq 'HASH' ? ( keys %$_ )[0] : $_ ),
52 Quality => scalar( ref $_ eq 'HASH' ? ( values %$_ )[0] : 1 )
53 };
54 6 my $id = lc $opt->{Language};
55 6 ▶ $grep->($opt) ? ( $id => $opt ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
&$grep($opt) ? : 6 0 6
56 } @$langs;
57 }
58  
59 3 ▶sub negotiate {
Subroutine negotiate: called CC=3 SCAR=11.2
Pod documented
60 3 my ( $dsl, $tplname, @rest ) = @_;
61 3 my $app = $dsl->app;
62 3 my $engine = $app->engine('template');
63 my @langmap = _langmap(
64 6 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
65 6 my $lang = shift->{Language};
66 6 my $view = $engine->view_pathname( $tplname . '.' . $lang );
67 6 ▶ defined $view and -e $view ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
defined $view and -e $view ? 1 : 0 6 0 6
-e $view ? : 6 0 6
68 }
69 3 );
70 3 my $lang = apply_variant( $dsl, 0, {}, @langmap );
71 3 ▶ $tplname .= '.' . $lang if $lang;
Branch: 1 branch
BranchTrueFalseTotal
if $lang 2 1 3
72 3 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 3 0 3
73 3 return ( $tplname, @rest );
74 }
75 else {
76 0 return $tplname;
77 }
78 }
79  
80 3 register
81 choose_variant => \&choose_variant,
82 { is_global => 0 };
83 3 register
84 apply_variant => \&apply_variant,
85 { is_global => 0 };
86 3 register
87 negotiate => \&negotiate,
88 { is_global => 0 };
89  
90 3 register_plugin;
91  
92 3 1;
93  
94 __END__
↑ Summary