blib/lib/Dancer/Plugin/Negotiate.pm

filter: Statementstmt 98.1% 54 / 55 Branchbran 67.8% 19 / 28 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 90.9% 10 / 11 Podpod n/a totaltotal 82.2% 88 / 107 CC 18 SCAR 15.1
CC
18
Cov
85%
_langmap
8.1
apply_variant
6.0
choose_variant
4.1
CRAP 4.5
↑ Summary
1 package Dancer::Plugin::Negotiate;
2  
3 5 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use HTTP::Negotiate ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Dancer::Plugin::Negotiate - Content negotiation plugin for Dancer
12  
13 =head1 VERSION
14  
15 Version 0.031
16  
17 =cut
18  
19 5 our $VERSION = '0.031';
20  
21 =head1 SYNOPSIS
22  
23 use Dancer::Plugin::Negotiate;
24  
25 $variant = choose_variant(
26 var1 => {
27 Quality => 1.000,
28 Type => 'text/html',
29 Charset => 'iso-8859-1',
30 Language => 'en',
31 Size => 3000
32 },
33 var2 => {
34 Quality => 0.950,
35 Type => 'text/plain',
36 Charset => 'us-ascii',
37 Language => 'no',
38 Size => 400
39 },
40 var3 => {
41 Quality => 0.3,
42 Type => 'image/gif',
43 Size => 43555
44 }
45 ); # returns 'var1' or 'var2' or 'var3' or undef
46  
47 =head1 DESCRIPTION
48  
49 This module is a wrapper for L<HTTP::Negotiate>.
50  
51 =head1 KEYWORDS
52  
53 =head2 C<< choose_variant(%variants) >>
54  
55 C<%options> is a hash like this:
56  
57 %variants = (
58 $identifier => \%options
59 )
60
61 The key C<$identifier> is a string that will be returned by C<choose_variant()>.
62  
63 Valid keywords of hashref C<\%options>:
64  
65 =over 4
66  
67 =item Quality
68  
69 A float point value between I<0.000> and I<1.000>, describing the source quality (defaults to 1)
70  
71 =item Type
72  
73 A MIME media type (with no charset attributes, but other attributes like I<version>)
74  
75 =item Encoding
76  
77 An encoding like I<gzip> or I<compress>
78  
79 =item Charset
80  
81 An encoding like I<utf-8> or I<iso-8859-1>
82  
83 =item Language
84  
85 A language tag conforming to RFC 3066
86  
87 =item Size
88  
89 Number of bytes used to represent
90  
91 =back
92  
93 Returns C<undef> if no variant matched.
94  
95 See L<HTTP::Negotiate|HTTP::Negotiate> for more information.
96  
97 =cut
98  
99 7 ▶sub choose_variant {
Subroutine choose_variant: called CC=4 SCAR=14.0
100 7 my $variants = [];
101 7 ▶ while (my ($variant, $options) = (shift, shift)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = (shift(), shift())) 24 0 24
102 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
103 push @$variants => [
104 $variant,
105 $options->{Quality},
106 $options->{Type},
107 $options->{Encoding},
108 $options->{Charset},
109 $options->{Language},
110 $options->{Size}
111 17 ];
112 }
113 7 return HTTP::Negotiate::choose($variants, Dancer::SharedData->request->headers);
114 }
115  
116 =head2 C<< apply_variant(%options) >>
117  
118 This method behaves like C<choose_variant> but sets the according response headers if a variant matched.
119  
120 =cut
121  
122 5 ▶sub apply_variant {
Subroutine apply_variant: called CC=6 SCAR=18.0
123 5 local %_ = @_;
124 5 my $variant = scalar choose_variant(@_);
125 5 ▶ return undef unless defined $variant;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $variant 0 5 5
126 5 my %options = %{$_{$variant}};
127 5 my $R = Dancer::SharedData->response;
128 5 ▶ $R->header('Content-Type' => $options{Type} ) if defined $options{Type};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"Type"} 2 3 5
129 5 ▶ $R->header('Content-Encoding' => $options{Encoding}) if defined $options{Encoding};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"Encoding"} 0 5 5
130 5 ▶ $R->header('Content-Charset' => $options{Charset} ) if defined $options{Charset};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"Charset"} 2 3 5
131 5 ▶ $R->header('Content-Language' => $options{Language}) if defined $options{Language};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"Language"} 4 1 5
132 5 return $variant;
133 }
134  
135 =head2 C<< negotiate($template_name) >>
136  
137 This method returns C<$template_name> with a suffixed language tag. The file needs to exist. This method behaves similiary to mod_negotiate of apache httpd's.
138  
139 Language tags must be specified in plugin settings and ordered by priority:
140  
141 plugin:
142 Negotiate:
143 languages:
144 - en
145 - de
146 - fr
147  
148 The result of this method can be propagated to C<template()> in order to render a localized version of the file.
149  
150 get '/index' => sub {
151 return template negotiate 'index';
152 }; # renders index.de.tt or index.en.tt or index.fr.tt or index.tt
153  
154 Falls back to C<$template_name> if negotiaten fails.
155  
156 Hint: additional arguments applies to C<template()>:
157  
158 template negotiate index => { foo => 'bar' };
159 # is the same as
160 template(negotiate('index'), { foo => 'bar' });
161  
162 =cut
163  
164 3 ▶sub _langmap {
Subroutine _langmap: called CC=6 SCAR=20.9
165 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
166 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
167 return grep defined, map {
168 3 ▶ my $opt = {
Branch: 2 branches
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 0 6 6
ref $_ eq 'HASH' ? : 0 6 6
169 Language => scalar(ref $_ eq 'HASH' ? (keys %$_)[0] : $_ ),
170 Quality => scalar(ref $_ eq 'HASH' ? (values %$_)[0] : 1 )
171 };
172 6 my $id = lc $opt->{Language};
173 6 ▶ $grep->($opt) ? ( $id => $opt ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
&$grep($opt) ? : 6 0 6
174 } @$langs;
175 }
176  
177 3 ▶sub negotiate($;) {
Subroutine negotiate: called CC=3 SCAR=11.1
178 3 my ($tplname, @rest) = @_;
179 3 my $engine = engine('template');
180 6 ▶ my @langmap = _langmap(sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
181 6 my $lang = shift->{Language};
182 6 my $view = $engine->view($tplname.'.'.$lang);
183 6 ▶ defined $view and $engine->view_exists($view) ? 1 : 0
Branch: 2 branches
BranchTrueFalseTotal
defined $view and $engine->view_exists($view) ? 1 : 0 6 0 6
$engine->view_exists($view) ? : 6 0 6
184 3 });
185 3 my $lang = apply_variant(0, {}, @langmap);
186 3 ▶ return ($tplname, @rest) unless defined $lang;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lang 0 3 3
187 3 ▶ return ($tplname, @rest) unless $lang;
Branch: 1 branch
BranchTrueFalseTotal
unless $lang 1 2 3
188 2 return ($tplname.'.'.$lang, @rest);
189 }
190  
191 5 register choose_variant => \&choose_variant;
192 5 register apply_variant => \&apply_variant;
193 5 register negotiate => \&negotiate;
194  
195 5 register_plugin;
196 5 1;
197  
198 =head1 AUTHOR
199  
200 David Zurborg, C<< <zurborg@cpan.org> >>
201  
202 =head1 BUGS
203  
204 Please report any bugs or feature requests through my project management tool
205 at L<http://development.david-zurb.org/projects/libdancer-plugin-negotiate-perl/issues/new>. I
206 will be notified, and then you'll automatically be notified of progress on
207 your bug as I make changes.
208  
209 =head1 SUPPORT
210  
211 You can find documentation for this module with the perldoc command.
212  
213 perldoc Dancer::Plugin::Negotiate
214  
215 You can also look for information at:
216  
217 =over 4
218  
219 =item * Redmine: Homepage of this module
220  
221 L<http://development.david-zurb.org/projects/libdancer-plugin-negotiate-perl>
222  
223 =item * RT: CPAN's request tracker
224  
225 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Dancer-Plugin-Negotiate>
226  
227 =item * AnnoCPAN: Annotated CPAN documentation
228  
229 L<http://annocpan.org/dist/Dancer-Plugin-Negotiate>
230  
231 =item * CPAN Ratings
232  
233 L<http://cpanratings.perl.org/d/Dancer-Plugin-Negotiate>
234  
235 =item * Search CPAN
236  
237 L<http://search.cpan.org/dist/Dancer-Plugin-Negotiate/>
238  
239 =back
240  
241 =head1 COPYRIGHT & LICENSE
242  
243 Copyright 2014 David Zurborg, all rights reserved.
244  
245 This program is released under the following license: open-source
246  
247 =cut
248  
249 5 1;
↑ Summary