File Coverage

lib/Dancer/Plugin/UUID.pm
Criterion Covered Total %
statement 27 27 100.0
branch 3 4 75.0
condition n/a
subroutine 10 10 100.0
pod n/a
total 40 41 97.5


line stmt bran cond sub pod time code
1             package Dancer::Plugin::UUID;
2              
3             #ABSTRACT: Maintain unique identifiers for each visitor of the application.
4              
5              
6 1     1   382876 use strict;
  1         3  
  1         52  
7 1     1   6 use warnings;
  1         2  
  1         39  
8 1     1   5 use feature ':5.10';
  1         4  
  1         117  
9 1     1   6 use Dancer ':syntax';
  1         1  
  1         9  
10 1     1   1238 use Dancer::Plugin;
  1         2106  
  1         119  
11 1     1   793 use Digest::SHA1 'sha1_hex';
  1         982  
  1         91  
12 1     1   1432 use Data::UUID;
  1         2256  
  1         748  
13              
14             # constants
15             my $uuid_cookie_name = 'dancer.uuid';
16             my $drop_test_name = 'dancer.uuid.test';
17              
18             hook 'before' => sub {
19              
20             # if Do Not Track, do nothing
21             return if request->env->{'HTTP_DNT'};
22              
23             # if we have a UUID already, return
24             my $uuid = cookies->{$uuid_cookie_name};
25             return if defined $uuid;
26              
27             # no UUID cookie is found, do we have the 'test cookie'?
28             if ( defined cookies->{$drop_test_name} ) {
29              
30             # check the value, to make sure that's our cookie
31             my $expected = _build_test_cookie_value();
32              
33             # the test cookie has the expected value, we can drop our UUID cookie
34             if ( $expected eq cookies->{$drop_test_name}->value ) {
35             my $uuid_fact = Data::UUID->new;
36             $uuid = $uuid_fact->create();
37             cookie $uuid_cookie_name => $uuid_fact->to_string($uuid),
38             expires => "3 years";
39             }
40             }
41              
42             # drop the test cookie, for the session
43             cookie $drop_test_name => _build_test_cookie_value();
44             };
45              
46             register uuid => sub {
47 2     2   1216 return _uuid_value();
48             };
49              
50             hook 'before_template_render' => sub {
51             my $tokens = shift;
52             $tokens->{'dancer.uuid'} = _uuid_value();
53             };
54              
55             # private
56              
57             sub _build_test_cookie_value {
58 3     3   39 return sha1_hex( __FILE__ );
59             }
60              
61             sub _uuid_value {
62 2 50   2   11 if ( !request->env->{HTTP_DNT} ) {
63 2         42 my $uuid = cookies->{$uuid_cookie_name};
64 2 100       26 return $uuid->value if defined $uuid;
65             }
66 1         4 undef;
67             }
68              
69             register_plugin;
70             1;
71              
72             __END__