| 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
| |||||||||||||||||||||||||||||
| 19 | 24 | ▶ | last unless defined $variant and defined $options; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $variant and defined $options
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 37 | $R->header( 'Content-Encoding' => $options{Encoding} ) | ||||||||||||||||||||||||||||
| 38 | 5 | ▶ | if defined $options{Encoding}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | $R->header( 'Content-Charset' => $options{Charset} ) | ||||||||||||||||||||||||||||
| 40 | 5 | ▶ | if defined $options{Charset}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | $R->header( 'Content-Language' => $options{Language} ) | ||||||||||||||||||||||||||||
| 42 | 5 | ▶ | if defined $options{Language}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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;
}
MC/DC0%0/2
shift() || sub {
1;
}
shift()
sub {
1;
}
Truth table33%1/3
A || B
A= shift()B= sub {
1;
}
| |||||||||||||||||||||||||||||
| 48 | 3 | ▶ | my $langs = plugin_setting->{languages} || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
plugin_setting()->{'languages'} || {}
MC/DC0%0/1
plugin_setting()->{'languages'} || {}
plugin_setting()->{'languages'}
Truth table50%1/2
A
A= plugin_setting()->{'languages'}
| |||||||||||||||||||||||||||||
| 49 | return grep defined, map { | ||||||||||||||||||||||||||||
| 50 | 3 | ▶ | my $opt = { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 3 | ); | |||||||||||||||||||||||||||
| 70 | 3 | my $lang = apply_variant( $dsl, 0, {}, @langmap ); | |||||||||||||||||||||||||||
| 71 | 3 | ▶ | $tplname .= '.' . $lang if $lang; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 3 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||