ActionPack/ContentNegotiation.pm

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