File Coverage

blib/lib/Plack/Middleware/Woothee.pm
Criterion Covered Total %
statement 48 49 97.9
branch 4 6 66.6
condition 3 6 50.0
subroutine 18 19 94.7
pod 1 1 100.0
total 74 81 91.3


line stmt bran cond sub pod time code
1             package Plack::Middleware::Woothee;
2 4     4   234392 use 5.008_001;
  4         17  
  4         161  
3 4     4   25 use strict;
  4         8  
  4         122  
4 4     4   21 use warnings;
  4         14  
  4         203  
5              
6             our $VERSION = '0.02';
7              
8 4     4   865 use parent 'Plack::Middleware';
  4         290  
  4         29  
9              
10 4     4   78177 use Plack::Util::Accessor qw/parse_all_req/;
  4         11  
  4         19  
11              
12             sub call {
13 17     17 1 153863 my($self, $env) = @_;
14              
15 17         160 $env->{'psgix.woothee'} = Plack::Middleware::Woothee::Object->new(user_agent => $env->{HTTP_USER_AGENT});
16              
17 17 100       84 $env->{'psgix.woothee'}->parse if $self->parse_all_req;
18              
19 17         431 $self->app->($env);
20             }
21              
22             1;
23              
24             package Plack::Middleware::Woothee::Object;
25 4     4   416 use strict;
  4         8  
  4         105  
26 4     4   17 use warnings;
  4         8  
  4         87  
27 4     4   3410 use Woothee;
  4         145628  
  4         2139  
28              
29             sub new {
30 17     17   74 my ($class, %args) = @_;
31 17         75 bless \%args, $class;
32             }
33              
34 0     0   0 sub user_agent { $_[0]->{user_agent} }
35              
36             sub name {
37 2     2   27 return $_[0]->_get('name');
38             }
39              
40             sub category {
41 2     2   23 return $_[0]->_get('category');
42             }
43              
44             sub os {
45 2     2   23 return $_[0]->_get('os');
46             }
47              
48             sub vendor {
49 2     2   25 return $_[0]->_get('vendor');
50             }
51              
52             sub version {
53 2     2   24 return $_[0]->_get('version');
54             }
55              
56             sub _get {
57 10     10   22 my ($self, $key) = @_;
58              
59 10 50       33 unless ($self->{$key}) {
60 10         28 $self->parse;
61             }
62              
63 10         298 return $self->{$key};
64             }
65              
66             sub parse {
67 13     13   43 my $self = shift;
68              
69 13   33     118 $self->{parse} ||= Woothee->parse($self->{user_agent});
70              
71 13         5817 for my $key (keys %{$self->{parse}}) {
  13         217  
72 65         329 $self->{$key} = delete $self->{parse}{$key};
73             }
74             }
75              
76             sub is_crawler {
77 3     3   34 my $self = shift;
78              
79 3 50       17 unless ( exists $self->{is_crawler} ) {
80 3   66     42 $self->{is_crawler} ||= Woothee->is_crawler($self->{user_agent});
81             }
82              
83 3         327 return $self->{is_crawler};
84             }
85              
86             1;
87              
88             __END__