| 1 | package App::Cerberus::Plugin::ParseBrowser; | ||
| 2 | { | ||
| 3 | $App::Cerberus::Plugin::ParseBrowser::VERSION = '0.01'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use HTML::ParseBrowser; | ||
| 9 | use List::Util qw(first); | ||
| 10 | use Carp; | ||
| 11 | use parent 'App::Cerberus::Plugin'; | ||
| 12 | |||
| 13 | #=================================== | ||
| 14 | sub init { | ||
| 15 | #=================================== | ||
| 16 | my $self = shift; | ||
| 17 | $self->{cache} = {}; | ||
| 18 | } | ||
| 19 | |||
| 20 | #=================================== | ||
| 21 | sub request { | ||
| 22 | #=================================== | ||
| 23 | my ( $self, $req, $response ) = @_; | ||
| 24 | my $ua = $req->param('ua') or return; | ||
| 25 | $response->{ua} = $self->{cache}{$ua} and return; | ||
| 26 | |||
| 27 | my $detect; | ||
| 28 | eval { | ||
| 29 | $detect = HTML::ParseBrowser->new($ua); | ||
| 30 | }; | ||
| 31 | return unless $detect; | ||
| 32 | |||
| 33 | # , category, ,, | ||
| 34 | my %data = ( | ||
| 35 | browser => $detect->name || '', | ||
| 36 | os => $detect->os || '', | ||
| 37 | ostype => $detect->ostype || '', | ||
| 38 | osvers => $detect->osvers || '', | ||
| 39 | osarc => $detect->osarc || '', | ||
| 40 | language => $detect->language || '', | ||
| 41 | languages => $detect->languages || [], | ||
| 42 | version => { | ||
| 43 | major => $detect->version->{'major'} || '', | ||
| 44 | minor => $detect->version->{'minor'} || '', | ||
| 45 | full => $detect->v || '', | ||
| 46 | }, | ||
| 47 | browser_properties => [ $detect->properties ], | ||
| 48 | ); | ||
| 49 | |||
| 50 | $response->{ua} = $self->{cache}{$ua} = \%data; | ||
| 51 | } | ||
| 52 | |||
| 53 | 1; | ||
| 54 | |||
| 55 | # ABSTRACT: Add user-agent information to App::Cerberus | ||
| 56 | |||
| 57 | __END__ |