blib/lib/HTTP/UserAgentClientHints.pm

filter: Statementstmt 100% 66 / 66 Branchbran 100% 20 / 20 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 98.2% 112 / 114 CC 15 SCAR 11.2
CC
15
Cov
99%
_normalize
6.0
_accept_ch
4.0
_as_http_header_key
3.0
CRAP 3.1
« Build ↑ Summary blib/lib/HTTP/UserAgentClientHints/Util.pm »
1 package HTTP::UserAgentClientHints;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use HTTP::UserAgentClientHints::BrandVersion;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use HTTP::UserAgentClientHints::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.05';
8  
9 2 my $HTTP_HEADER_PREFIX = 'Sec-CH-UA';
10  
11 2 my @FIELDS = qw/
12 UA
13 Mobile
14 Platform
15 Arch
16 Bitness
17 Model
18 Full-Version-List
19 Full-Version
20 Platform-Version
21 /;
22  
23 # Build getters
24 2 for my $field (@FIELDS) {
25 18 my $method = __PACKAGE__->_as_method($field);
26 2 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
27 18 *{__PACKAGE__ . '::' . $method} = sub {
28 17 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
29 17 ▶ if (exists $self->{_value}{$method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_value"}{$method}) 3 14 17
30 3 return $self->{_value}{$method};
31 }
32 14 my $raw_value = $self->{_headers}->header($self->_as_http_header_key($field));
33 14 return $self->{_value}{$method} = $self->_normalize($raw_value, $field);
34 18 };
35 18 *{__PACKAGE__ . '::' . $method . '_raw'} = sub {
36 15 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
37 15 ▶ if (exists $self->{_value_raw}{$method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_value_raw"}{$method}) 1 14 15
38 1 return $self->{_value_raw}{$method};
39 }
40 14 return $self->{_value_raw}{$method} = $self->{_headers}->header($self->_as_http_header_key($field));
41 18 };
42 }
43  
44 2 my $FULL_ACCEPT_CH = __PACKAGE__->_accept_ch;
45  
46 sub _as_method {
47 18 ▶ my ($self, $field) = @_;
Subroutine _as_method: called CC=1 SCAR=0.0
48  
49 18 $field =~ s/-/_/g;
50 18 $field = lc $field;
51  
52 18 return $field;
53 }
54  
55 sub _as_http_header_key {
56 64 ▶ my ($self, $field) = @_;
Subroutine _as_http_header_key: called CC=3 SCAR=11.1
57  
58 64 ▶ return $HTTP_HEADER_PREFIX . (!$field || $field eq 'UA' ? '' : "-$field");
Branch: 1 branch
BranchTrueFalseTotal
!$field || $field eq 'UA' ? : 8 56 64
Condition66%2/3
not $field or $field eq "UA"
l!l&&r!l&&!r
0856
MC/DC50%1/2
not $field or $field eq "UA"
not $field $field eq "UA"
Truth table66%2/3
not(A) or B
A= not $fieldB= $field eq "UA"
ABresult
1X1
011
000
59 }
60  
61 sub _normalize {
62 14 ▶ my ($self, $value, $field) = @_;
Subroutine _normalize: called CC=6 SCAR=17.9
63  
64 14 ▶ return $value unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 3 11 14
65  
66 11 ▶ if ($field eq 'UA' || $field eq 'Full-Version-List') {
Branch: 3 branches
BranchTrueFalseTotal
if ($field eq 'UA' or $field eq 'Full-Version-List') { } 2 9 11
elsif ($field =~ /^(?:Platform|Arch|Bitness|Model|Full-Version)$/) { } 6 3 9
elsif ($field eq 'Mobile') { } 2 1 3
Condition100%3/3
$field eq "UA" or $field eq "Full-Version-List"
l!l&&r!l&&!r
119
MC/DC100%2/2
$field eq "UA" or $field eq "Full-Version-List"
$field eq "UA" $field eq "Full-Version-List"
Truth table100%3/3
A or B
A= $field eq "UA"B= $field eq "Full-Version-List"
ABresult
1X1
011
000
67 2 $value = HTTP::UserAgentClientHints::BrandVersion->new($value);
68 }
69 elsif ($field =~ m!^(?:Platform|Arch|Bitness|Model|Full-Version)$!) {
70 6 $value = HTTP::UserAgentClientHints::Util->strip_quote($value);
71 }
72 elsif ($field eq 'Mobile') {
73 2 $value =~ s/^\?//;
74 }
75  
76 11 return $value;
77 }
78  
79 sub new {
80 3 ▶ my ($class, $http_headers_obj) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
81  
82 3 ▶ unless ($http_headers_obj->can('header')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($http_headers_obj->can("header")) 1 2 3
83 1 die q|Argument object:| . ref($http_headers_obj) . q| doesn't have "header" method to get HTTP header value.|;
84 }
85  
86 bless {
87 2 _headers => $http_headers_obj,
88 _value_raw => {},
89 _value => {},
90 }, $class;
91 }
92  
93 sub accept_ch {
94 3 ▶ return $FULL_ACCEPT_CH unless $_[1];
Subroutine accept_ch: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 1 2 3
95  
96 2 return _accept_ch(@_);
97 }
98  
99 sub _accept_ch {
100 4 ▶ my ($self, $excepts) = @_;
Subroutine _accept_ch: called CC=4 SCAR=13.9
101  
102 4 ▶ $excepts ||= [];
Condition100%2/2
$excepts ||= []
l!l
22
MC/DC100%1/1
$excepts ||= []
$excepts
Truth table100%2/2
A
A= $excepts
Aresult
11
00
103  
104 4 unshift @{$excepts}, 'Sec-CH-UA', 'Sec-CH-UA-Mobile', 'Sec-CH-UA-Platform'; # Default fields
105  
106 4 my @accept_ch;
107 4 for my $field (@FIELDS) {
108 36 my $f = $self->_as_http_header_key($field);
109 36 ▶ next if grep { lc($f) eq lc($_) } @{$excepts};
Branch: 1 branch
BranchTrueFalseTotal
if grep {lc $f eq lc $_;} @{$excepts;} 15 21 36
110 21 push @accept_ch, $f;
111 }
112  
113 4 return join(', ', @accept_ch);
114 }
115  
116 2 1;
117  
118 __END__
« Build ↑ Summary blib/lib/HTTP/UserAgentClientHints/Util.pm »