ActionPack/ContentNegotiation.pm

filter: Statementstmt 100% 92 / 92 Branchbran 85.4% 41 / 48 Conditioncond 66.6% 12 / 18 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 100% 19 / 19 Podpod 66.6% 4 / 6 totaltotal 89.2% 174 / 195 CC 33 SCAR 24.7
CC
33
Cov
92%
_make_choice
23.4
choose_media_type
4.0
new
3.3
CRAP 11.8
↑ Summary ActionPack.pm »
1 package HTTP::Headers::ActionPack::ContentNegotiation;
2 # ABSTRACT: A class to handle content negotiation
3  
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 our $VERSION = '0.10';
8  
9 4 ▶use Carp qw[ confess ];
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Scalar::Util qw[ blessed ];
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use List::Util qw[ first ];
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶sub new {
Subroutine new: called CC=3 SCAR=12.0
Pod undocumented
14 4 my $class = shift;
15 4 my $action_pack = shift;
16  
17 4 ▶ (blessed $action_pack && $action_pack->isa('HTTP::Headers::ActionPack'))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($action_pack) and $action_pack->isa("HTTP::Headers::ActionPack") or confess("You must supply an instance of HTTP::Headers::ActionPack") 4 0 4
Condition33%1/3
builtin::blessed($action_pack) and $action_pack->isa("HTTP::Headers::ActionPack")
!ll&&!rl&&r
004
MC/DC0%0/2
builtin::blessed($action_pack) and $action_pack->isa("HTTP::Headers::ActionPack")
builtin::blessed($action_pack) $action_pack->isa("HTTP::Headers::ActionPack")
Truth table33%1/3
A and B
A= builtin::blessed($action_pack)B= $action_pack->isa("HTTP::Headers::ActionPack")
ABresult
0X0
100
111
18 || confess "You must supply an instance of HTTP::Headers::ActionPack";
19  
20 4 bless { action_pack => $action_pack } => $class;
21 }
22  
23 91 ▶sub action_pack { (shift)->{'action_pack'} }
Subroutine action_pack: called CC=1 SCAR=0.0
Pod undocumented
24  
25 20 ▶sub choose_media_type {
Subroutine choose_media_type: called CC=4 SCAR=13.9
Pod documented
26 20 my ($self, $provided, $header) = @_;
27 20 ▶ my $requested = blessed $header ? $header : $self->action_pack->create( 'MediaTypeList' => $header );
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($header) ? : 0 20 20
28 20 my $parsed_provided = [ map { $self->action_pack->create( 'MediaType' => $_ ) } @$provided ];
29  
30 20 my $chosen;
31 20 foreach my $request ( $requested->iterable ) {
32 32 my $requested_type = $request->[1];
33 32 $chosen = _media_match( $requested_type, $parsed_provided );
34 32 ▶ return $chosen if $chosen;
Branch: 1 branch
BranchTrueFalseTotal
if $chosen 17 15 32
35 }
36  
37 3 return;
38 }
39  
40 17 ▶sub choose_language {
Subroutine choose_language: called CC=1 SCAR=0.0
Pod documented
41 17 my ($self, $provided, $header) = @_;
42  
43 17 return $self->_make_choice(
44 choices => $provided,
45 header => $header,
46 class => 'AcceptLanguage',
47 matcher => \&_language_match,
48 );
49 }
50  
51 14 ▶sub choose_charset {
Subroutine choose_charset: called CC=1 SCAR=0.0
Pod documented
52 14 my ($self, $provided, $header) = @_;
53  
54 # NOTE:
55 # Making the default charset UTF-8, which
56 # is maybe sensible, I dunno.
57 # - SL
58 14 return $self->_make_choice(
59 choices => $provided,
60 header => $header,
61 class => 'AcceptCharset',
62 default => 'UTF-8',
63 matcher => \&_simple_match,
64 );
65 }
66  
67 5 ▶sub choose_encoding {
Subroutine choose_encoding: called CC=1 SCAR=0.0
Pod documented
68 5 my ($self, $provided, $header) = @_;
69  
70 5 return $self->_make_choice(
71 choices => $provided,
72 header => $header,
73 class => 'PriorityList',
74 default => 'identity',
75 matcher => \&_simple_match,
76 );
77 }
78  
79 36 ▶sub _make_choice {
Subroutine _make_choice: called CC=23 SCAR=31.5
80 36 my $self = shift;
81 36 my %args = @_;
82  
83 my ($choices, $header, $class, $default, $matcher)
84 36 = @args{qw( choices header class default matcher )};
85  
86 36 ▶ return if @$choices == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$choices == 0 3 33 36
87 33 ▶ return if $header eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $header eq "" 0 33 33
88  
89 33 ▶ my $accepted = blessed $header ? $header : $self->action_pack->create( $class => $header );
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($header) ? : 0 33 33
90 33 my $star_priority = $accepted->priority_of( '*' );
91  
92 my @canonical = map {
93 33 my $c = $accepted->canonicalize_choice($_);
94 67 ▶ $c ? [ $_, $c ] : ()
Branch: 1 branch
BranchTrueFalseTotal
$c ? : 67 0 67
95 } @$choices;
96  
97 33 my ($default_ok, $any_ok);
98  
99 33 ▶ if ($default) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default) 17 16 33
100 17 $default = $accepted->canonicalize_choice($default);
101 17 my $default_priority = $accepted->priority_of( $args{default} );
102  
103 17 ▶ if ( not defined $default_priority ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $default_priority) { } 6 11 17
elsif ($default_priority == 0.0) { } 3 8 11
104 6 ▶ if ( defined $star_priority && $star_priority == 0.0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $star_priority and $star_priority == 0.0) { } 1 5 6
Condition66%2/3
defined $star_priority and $star_priority == 0
!ll&&!rl&&r
501
MC/DC50%1/2
defined $star_priority and $star_priority == 0
defined $star_priority $star_priority == 0
Truth table66%2/3
A and B
A= defined $star_priorityB= $star_priority == 0
ABresult
0X0
100
111
105 1 $default_ok = 0;
106 }
107 else {
108 5 $default_ok = 1;
109 }
110 }
111 elsif ( $default_priority == 0.0 ) {
112 3 $default_ok = 0;
113 }
114 else {
115 8 $default_ok = 1;
116 }
117 }
118  
119 33 ▶ if ( not defined $star_priority ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $star_priority) { } 30 3 33
elsif ($star_priority == 0.0) { } 1 2 3
120 30 $any_ok = 0;
121 }
122 elsif ( $star_priority == 0.0 ) {
123 1 $any_ok = 0;
124 }
125 else {
126 2 $any_ok = 1;
127 }
128  
129 33 my $chosen;
130 33 for my $item ($accepted->iterable) {
131 48 my ($priority, $acceptable) = @$item;
132  
133 48 ▶ next if $priority == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $priority == 0 4 44 48
134  
135 44 ▶ if (my $match = first { $matcher->( $acceptable, $_->[1] ) } @canonical) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (my $match = &first(sub { &$matcher($acceptable, $_->[1]); } , @canonical)) 20 24 44
136 20 $chosen = $match->[0];
137 20 last;
138 }
139 }
140  
141 33 ▶ return $chosen if $chosen;
Branch: 1 branch
BranchTrueFalseTotal
if $chosen 20 13 33
142  
143 13 ▶ if ($any_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($any_ok) 1 12 13
144 2 ▶ my $match = first {
Subroutine __ANON__: called CC=3 SCAR=11.0
145 2 my $priority = $accepted->priority_of( $_->[1] );
146 2 ▶ return 1 unless defined $priority && $priority == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $priority and $priority == 0 1 1 2
Condition66%2/3
defined $priority and $priority == 0
!ll&&!rl&&r
101
MC/DC50%1/2
defined $priority and $priority == 0
defined $priority $priority == 0
Truth table66%2/3
A and B
A= defined $priorityB= $priority == 0
ABresult
0X0
100
111
147 1 return 0;
148 }
149 1 @canonical;
150  
151 1 ▶ return $match->[0] if $match;
Branch: 1 branch
BranchTrueFalseTotal
if $match 1 0 1
152 }
153  
154 12 ▶ if ( $default && $default_ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default and $default_ok) 4 8 12
Condition100%3/3
$default and $default_ok
!ll&&!rl&&r
534
MC/DC100%2/2
$default and $default_ok
$default $default_ok
Truth table100%3/3
A and B
A= $defaultB= $default_ok
ABresult
0X0
100
111
155 4 ▶ my $match = first { $matcher->( $default, $_->[1] ) } @canonical;
Subroutine __ANON__: called CC=1 SCAR=0.0
156 4 ▶ if ($match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match) 2 2 4
157 2 my $priority = $accepted->priority_of( $match->[1] );
158 2 ▶ return $match->[0] unless defined $priority && $priority == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $priority and $priority == 0 2 0 2
Condition33%1/3
defined $priority and $priority == 0
!ll&&!rl&&r
200
MC/DC0%0/2
defined $priority and $priority == 0
defined $priority $priority == 0
Truth table33%1/3
A and B
A= defined $priorityB= $priority == 0
ABresult
0X0
100
111
159 }
160 }
161  
162 10 return;
163 }
164  
165 ## ....
166  
167 32 ▶sub _media_match {
Subroutine _media_match: called CC=2 SCAR=6.9
168 32 my ($requested, $provided) = @_;
169 32 ▶ return $provided->[0] if $requested->matches_all;
Branch: 1 branch
BranchTrueFalseTotal
if $requested->matches_all 1 31 32
170 31 ▶ return first { $_->match( $requested ) } @$provided;
Subroutine __ANON__: called CC=1 SCAR=0.0
171 }
172  
173 35 ▶sub _language_match {
Subroutine _language_match: called CC=3 SCAR=11.0
174 35 my ($range, $tag) = map { lc($_) } @_;
175 35 ▶ ($range eq $tag || $range eq "*" || ( substr( $tag, 0, length($range) + 1 ) eq ( $range . "-" ) ) );
Branch: 1 branch
BranchTrueFalseTotal
$range eq $tag or $range eq "*" or substr($tag, 0, length($range) + 1) eq $range . '-' 8 27 35
Condition100%3/3
$range eq $tag or $range eq "*"
l!l&&r!l&&!r
7127
MC/DC100%2/2
$range eq $tag or $range eq "*"
$range eq $tag $range eq "*"
Truth table100%3/3
A or B
A= $range eq $tagB= $range eq "*"
ABresult
1X1
011
000
176 }
177  
178 44 ▶sub _simple_match {
Subroutine _simple_match: called CC=1 SCAR=0.0
179 44 return $_[0] eq $_[1];
180 }
181  
182 4 1;
183  
184 __END__
↑ Summary ActionPack.pm »