blib/lib/HTTP/Headers/Fancy.pm

filter: Statementstmt 94.5% 122 / 129 Branchbran 85.5% 65 / 76 Conditioncond 71.4% 10 / 14 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 94.7% 18 / 19 Podpod 100% 14 / 14 totaltotal 89.6% 233 / 260 SCAR 39.6
CC
50
Cov
90%
decode
20.0
split
8.0
encode_hash
6.0
CRAP 52.5
↑ Summary
1 14 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTTP::Headers::Fancy;
4  
5 14 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # ABSTRACT: Fancy naming schema of HTTP headers
9  
10 14 our $VERSION = '1.000'; # VERSION
11  
12 14 our @EXPORT_OK = qw(
13 decode_key
14 encode_key
15 decode_hash
16 encode_hash
17 split_field_hash
18 split_field_list
19 build_field_hash
20 build_field_list
21 );
22  
23 14 our %EXPORT_TAGS = ( all => \@EXPORT_OK, );
24  
25 sub _self {
26 189 ▶ my @args = @_;
Subroutine _self: called CC=6 SCAR=17.9
27 189 ▶ if ( blessed $args[0] and $args[0]->isa(__PACKAGE__) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($args[0]) and $args[0]->isa(__PACKAGE__)) { } 38 151 189
elsif (defined $args[0] and not ref $args[0] and $args[0] eq __PACKAGE__) { } 18 133 151
Condition66%2/3
builtin::blessed($args[0]) and $args[0]->isa(__PACKAGE__)
!ll&&!rl&&r
151038
MC/DC50%1/2
builtin::blessed($args[0]) and $args[0]->isa(__PACKAGE__)
builtin::blessed($args[0]) $args[0]->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= builtin::blessed($args[0])B= $args[0]->isa(__PACKAGE__)
ABresult
0X0
100
111
28 38 return @args;
29 }
30 ▶ elsif ( defined $args[0] and not ref $args[0] and $args[0] eq __PACKAGE__ )
Condition100%6/6
defined $args[0] and not ref $args[0] and $args[0] eq __PACKAGE__
!ll&&!rl&&r
1112218
defined $args[0] and not ref $args[0]
!ll&&!rl&&r
38140
MC/DC100%3/3
defined $args[0] and not ref $args[0] and $args[0] eq __PACKAGE__
defined $args[0] not ref $args[0] $args[0] eq __PACKAGE__
Truth table100%4/4
A and not(B) and C
A= defined $args[0]B= not ref $args[0]C= $args[0] eq __PACKAGE__
ABCresult
0XX0
10X0
1100
1111
31 {
32 18 return @args;
33 }
34 else {
35 133 return ( __PACKAGE__, @args );
36 }
37 }
38  
39 sub _encode_hash_deeply {
40 6 ▶ my ( $self, @args ) = _self(@_);
Subroutine _encode_hash_deeply: called CC=4 SCAR=13.9
41 6 ▶ if ( ref $args[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0]) 2 4 6
42 2 return { $self->_encode_hash_deeply( %{ $args[0] } ) };
43 }
44 4 my %hash = $self->encode_hash(@args);
45 4 foreach my $key ( keys %hash ) {
46 8 ▶ if ( ref $hash{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash{$key}) 4 4 8
47 4 $hash{$key} = $self->build( $hash{$key} );
48 }
49 }
50 4 return %hash;
51 }
52  
53 sub decode_key {
54 50 ▶ my ( $self, $k ) = _self(@_);
Subroutine decode_key: called CC=1 SCAR=0.0
Pod documented
55 50 $k =~ s{^([^-]+)}{ucfirst(lc($1))}se;
56 50 $k =~ s{-+([^-]+)}{ucfirst(lc($1))}sge;
57 50 $k =~ s{^X([A-Z])}{-$1}s;
58 50 return ucfirst($k);
59 }
60  
61 sub decode_hash {
62 2 ▶ my ( $self, @args ) = _self(@_);
Subroutine decode_hash: called CC=5 SCAR=16.1
Pod documented
63 2 ▶ my %headers = @args == 1 ? %{ $args[0] } : @args;
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 ? : 1 1 2
64 2 foreach my $old ( keys %headers ) {
65 14 my $new = decode_key($old);
66 14 ▶ if ( $old ne $new ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old ne $new) 12 2 14
67 12 $headers{$new} = delete $headers{$old};
68 }
69 }
70 2 ▶ wantarray ? %headers : \%headers;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
71 }
72  
73 sub encode_key {
74 64 ▶ my ( $self, $k ) = _self(@_);
Subroutine encode_key: called CC=2 SCAR=6.9
Pod documented
75 64 $k =~ s{_}{-}sg;
76 64 $k =~ s{-+}{-}sg;
77 64 $k =~ s{^-(.)}{'X'.uc($1)}se;
78 64 ▶ $k =~ s{([^-])([A-Z])}{$1-$2}s while $k =~ m{([^-])([A-Z])}s;
Branch: 1 branch
BranchTrueFalseTotal
$k =~ /([^-])([A-Z])/s and do { $k =~ s/([^-])([A-Z])/$1-$2/s } 31 64 95
79 64 return lc($k);
80 }
81  
82 sub encode_hash {
83 6 ▶ my ( $self, @args ) = _self(@_);
Subroutine encode_hash: called CC=6 SCAR=17.9
Pod documented
84 6 ▶ my %headers = @args == 1 ? %{ $args[0] } : @args;
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 ? : 1 5 6
85 6 foreach my $old ( keys %headers ) {
86 34 ▶ delete $headers{$old} unless defined $headers{$old};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $headers{$old} 0 34 34
87 34 my $new = encode_key($old);
88 34 ▶ if ( $old ne $new ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old ne $new) 30 4 34
89 30 $headers{$new} = delete $headers{$old};
90 }
91 }
92 6 ▶ wantarray ? %headers : \%headers;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 1 6
93 }
94  
95 sub split_field_hash {
96 20 ▶ my ( $self, $value, @rest ) = _self(@_);
Subroutine split_field_hash: called CC=5 SCAR=16.1
Pod documented
97 20 ▶ return () unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 18 20
98 18 ▶ if ( ref $value eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 1 17 18
99 1 foreach my $key (@rest) {
100 2 $value->{$key} = { $self->split_field_hash( $value->{$key} ) };
101 }
102 1 return $value;
103 }
104 17 pos($value) = 0;
105 17 my %data;
106 17 $value .= ',';
107 17 ▶ while ( $value =~
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ / \G \s* (?<key> [^=,]+? ) \s* (?: \s* = \s* (?: (?: " (?<value> [^"]*? ) " ) | (?<value> [^,]*? ) ) )? \s* ,+ \s* /gsx) 29 17 46
108 m{ \G \s* (?<key> [^=,]+? ) \s* (?: \s* = \s* (?: (?: " (?<value> [^"]*? ) " ) | (?<value> [^,]*? ) ) )? \s* ,+ \s* }gsx
109 )
110 {
111 29 $data{ decode_key( $+{key} ) } = $+{value};
112 }
113 17 return %data;
114 }
115  
116 sub split_field_list {
117 11 ▶ my ( $self, $value, @rest ) = _self(@_);
Subroutine split_field_list: called CC=6 SCAR=17.9
Pod documented
118 11 ▶ return () unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 11 11
119 11 ▶ if ( ref $value eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 1 10 11
120 1 foreach my $key (@rest) {
121 2 $value->{$key} = [ $self->split_field_list( $value->{$key} ) ];
122 }
123 1 return $value;
124 }
125 10 pos($value) = 0;
126 10 my @data;
127 10 $value .= ',';
128 10 ▶ while ( $value =~
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ m[ \G \s* (?<weak> W/ )? " (?<value> [^"]*? ) " \s* ,+ \s* ]gisx) 17 10 27
129 m{ \G \s* (?<weak> W/ )? " (?<value> [^"]*? ) " \s* ,+ \s* }gsix )
130 {
131 17 my $value = $+{value};
132 17 ▶ push @data => $+{weak} ? \$value : $value;
Branch: 1 branch
BranchTrueFalseTotal
$+{'weak'} ? : 7 10 17
133 }
134 10 return @data;
135 }
136  
137 sub build_field_hash {
138 15 ▶ my ( $self, @args ) = _self(@_);
Subroutine build_field_hash: called CC=4 SCAR=13.9
Pod documented
139 15 ▶ if ( ref $args[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq "HASH") 4 11 15
140 4 return $self->build_field_hash( %{ $args[0] } );
141 }
142 11 my %data = @args;
143 return join ', ', sort map {
144 11 $self->encode_key($_)
145 . (
146 defined( $data{$_} )
147 ? '='
148 . (
149 ( $data{$_} =~ m{[=,]} )
150 ? '"' . $data{$_} . '"'
151 17 ▶ : $data{$_}
Branch: 2 branches
BranchTrueFalseTotal
defined $data{$_} ? : 12 5 17
$data{$_} =~ /[=,]/ ? : 8 4 12
152 )
153 : ''
154 )
155 } keys %data;
156 }
157  
158 sub build_field_list {
159 15 ▶ my ( $self, @args ) = _self(@_);
Subroutine build_field_list: called CC=3 SCAR=11.0
Pod documented
160 15 ▶ if ( ref $args[0] eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq "ARRAY") 6 9 15
161 6 return $self->build_field_list( @{ $args[0] } );
162 }
163 9 ▶ return join ', ', map { ref($_) ? 'W/"' . $$_ . '"' : qq{"$_"} } @args;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 8 17 25
164 }
165  
166 sub new {
167 6 ▶ my $class = shift // __PACKAGE__;
Subroutine new: called CC=3 SCAR=13.1
Pod documented
Condition50%1/2
shift() // __PACKAGE__
l!l
60
MC/DC0%0/1
shift() // __PACKAGE__
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
168 6 ▶ return bless {@_} => ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
169 }
170  
171 sub encode {
172 6 ▶ my $self = shift;
Subroutine encode: called CC=4 SCAR=14.0
Pod documented
173 6 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 2 4 6
174 4 ▶ if ( @_ > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ > 1) { } 2 2 4
elsif (ref $_[0] eq 'HASH') { } 2 0 2
175 2 return $self->_encode_hash_deeply(@_);
176 }
177 elsif ( ref $_[0] eq 'HASH' ) {
178 2 return $self->_encode_hash_deeply( $_[0] );
179 }
180 else {
181 0 return $self->encode_key( $_[0] );
182 }
183 }
184  
185 sub decode {
186 0 ▶ my $self = shift;
Subroutine decode: not called CC=4 SCAR=30.0
Pod documented
187 0 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
188 0 ▶ if ( @_ > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ > 1) { } 0 0 0
elsif (ref $_[0] eq 'HASH') { } 0 0 0
189 0 return $self->decode_hash(@_);
190 }
191 elsif ( ref $_[0] eq 'HASH' ) {
192 0 return $self->decode_hash( $_[0] );
193 }
194 else {
195 0 return $self->decode_key( $_[0] );
196 }
197 }
198  
199 sub split {
200 5 ▶ my $self = shift;
Subroutine split: called CC=8 SCAR=20.8
Pod documented
201 5 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 4 5
202 4 my $val = shift;
203 4 ▶ if ( $val =~ m{^ \s* ( W/ ) ? " }six ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($val =~ m[^ \s* ( W/ ) ? " ]six) { } 2 2 4
elsif (not ref $val) { } 1 1 2
elsif (ref $val eq 'HASH') { } 1 0 1
204 2 return $self->split_field_list($val);
205 }
206 elsif ( not ref $val ) {
207 1 return $self->split_field_hash($val);
208 }
209 elsif ( ref $val eq 'HASH' ) {
210 1 foreach my $key (@_) {
211 3 ▶ next unless $val->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $val->{$key} 0 3 3
212 3 ▶ if ( $val->{$key} =~ m{^ \s* ( W/ )? " }six ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val->{$key} =~ m[^ \s* ( W/ )? " ]six) { } 2 1 3
213 2 $val->{$key} = [ $self->split_field_list( $val->{$key} ) ];
214 }
215 else {
216 1 $val->{$key} = { $self->split_field_hash( $val->{$key} ) };
217 }
218 }
219 1 return $val;
220 }
221 }
222  
223 sub build {
224 7 ▶ my $self = shift;
Subroutine build: called CC=3 SCAR=11.0
Pod documented
225 7 ▶ if ( ref $_[0] eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 3 4 7
elsif (ref $_[0] eq 'ARRAY') { } 3 1 4
226 3 return $self->build_field_hash(@_);
227 }
228 elsif ( ref $_[0] eq 'ARRAY' ) {
229 3 return $self->build_field_list(@_);
230 }
231 else {
232 1 return $self->build_field_hash(@_);
233 }
234 }
235  
236 sub etags {
237 2 ▶ my $self = shift;
Subroutine etags: called CC=1 SCAR=0.0
Pod documented
238 2 return $self->build_field_list(@_);
239 }
240  
241 14 1;
242  
243 __END__
↑ Summary