File Coverage

blib/lib/Dancer2/Plugin/BrowserDetect.pm
Criterion Covered Total %
statement 22 24 91.6
branch n/a
condition n/a
subroutine 7 8 87.5
pod 1 2 50.0
total 30 34 88.2


line stmt bran cond sub pod time code
1             #
2             # This file is part of Dancer2-Plugin-BrowserDetect
3             #
4             # This software is copyright (c) 2016 by Natal Ngétal.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9             package Dancer2::Plugin::BrowserDetect;
10             $Dancer2::Plugin::BrowserDetect::VERSION = '1.163590';
11 2     2   429996 use strict;
  2         3  
  2         49  
12 2     2   6 use warnings;
  2         2  
  2         55  
13              
14 2     2   929 use Dancer2::Plugin 0.200000;
  2         67362  
  2         15  
15              
16 2     2   18279 use HTTP::BrowserDetect ();
  2         23210  
  2         61  
17 2     2   19 use Scalar::Util ();
  2         1  
  2         269  
18              
19             #ABSTRACT: Provides an easy to have info of the browser.
20              
21              
22             sub BUILD {
23 1     1 0 1520 my $plugin = shift;
24             # Create a weakened plugin that we can close over to avoid leaking.
25 1         2 Scalar::Util::weaken( my $weak_plugin = $plugin );
26             $plugin->app->add_hook(
27             Dancer2::Core::Hook->new(
28             name => 'before_template',
29             code => sub {
30 0     0   0 my $tokens = shift;
31 0         0 $tokens->{browser_detect} = $weak_plugin->browser_detect;
32             },
33             )
34 1         25 );
35             }
36              
37             plugin_keywords 'browser_detect';
38              
39             sub browser_detect {
40 1     1 1 9636 my $plugin = shift;
41 1         9 my $useragent = $plugin->app->request->env->{HTTP_USER_AGENT};
42 1         11 my $browser = HTTP::BrowserDetect->new($useragent);
43              
44 1         111 return $browser;
45             }
46              
47              
48             1;
49              
50             __END__