blib/lib/HTTP/Headers/Fancy.pm

filter: Statementstmt 94.8% 128 / 135 Branchbran 85.5% 65 / 76 Conditioncond 71.4% 10 / 14 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 95.0% 19 / 20 Podpod 100% 15 / 15 totaltotal 89.9% 241 / 268 CC 50 SCAR 17.3
CC
50
Cov
90%
decode
20.0
split
8.0
encode_hash
6.0
CRAP 5.6
↑ Summary
1 16 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTTP::Headers::Fancy;
4  
5 16 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # ABSTRACT: Fancy naming schema of HTTP headers
9  
10 16 our $VERSION = '1.001'; # VERSION
11  
12 16 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 16 our %EXPORT_TAGS = ( all => \@EXPORT_OK, );
24  
25 194 ▶sub _self {
Subroutine _self: called CC=6 SCAR=17.9
26 194 my @args = @_;
27 194 ▶ if ( blessed $args[0] and $args[0]->isa(__PACKAGE__) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($args[0]) and $args[0]->isa(__PACKAGE__)) { } 43 151 194
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
151043
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 43 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 6 ▶sub _encode_hash_deeply {
Subroutine _encode_hash_deeply: called CC=4 SCAR=13.9
40 6 my ( $self, @args ) = _self(@_);
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 50 ▶sub decode_key {
Subroutine decode_key: called CC=1 SCAR=0.0
Pod documented
54 50 my ( $self, $k ) = _self(@_);
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 2 ▶sub decode_hash {
Subroutine decode_hash: called CC=5 SCAR=16.1
Pod documented
62 2 my ( $self, @args ) = _self(@_);
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 64 ▶sub encode_key {
Subroutine encode_key: called CC=2 SCAR=6.9
Pod documented
74 64 my ( $self, $k ) = _self(@_);
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 6 ▶sub encode_hash {
Subroutine encode_hash: called CC=6 SCAR=17.9
Pod documented
83 6 my ( $self, @args ) = _self(@_);
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 5 ▶sub prettify_key {
Subroutine prettify_key: called CC=1 SCAR=0.0
Pod documented
96 5 my ( $self, $k ) = _self(@_);
97 5 $k = lc $k;
98 5 $k =~ s{-+}{-}g;
99 5 $k =~ s{(-)(.)}{$1.ucfirst($2)}eg;
100 5 return ucfirst $k;
101 }
102  
103 20 ▶sub split_field_hash {
Subroutine split_field_hash: called CC=5 SCAR=16.1
Pod documented
104 20 my ( $self, $value, @rest ) = _self(@_);
105 20 ▶ return () unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 18 20
106 18 ▶ if ( ref $value eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 1 17 18
107 1 foreach my $key (@rest) {
108 2 $value->{$key} = { $self->split_field_hash( $value->{$key} ) };
109 }
110 1 return $value;
111 }
112 17 pos($value) = 0;
113 17 my %data;
114 17 $value .= ',';
115 17 ▶ while ( $value =~
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ / \G \s* (?<key> [^=,]+? ) \s* (?: \s* = \s* (?: (?: " (?<value> [^"]*? ) " ) | (?<value> [^,]*? ) ) )? \s* ,+ \s* /gsx) 29 17 46
116 m{ \G \s* (?<key> [^=,]+? ) \s* (?: \s* = \s* (?: (?: " (?<value> [^"]*? ) " ) | (?<value> [^,]*? ) ) )? \s* ,+ \s* }gsx
117 )
118 {
119 29 $data{ decode_key( $+{key} ) } = $+{value};
120 }
121 17 return %data;
122 }
123  
124 11 ▶sub split_field_list {
Subroutine split_field_list: called CC=6 SCAR=17.9
Pod documented
125 11 my ( $self, $value, @rest ) = _self(@_);
126 11 ▶ return () unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 11 11
127 11 ▶ if ( ref $value eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 1 10 11
128 1 foreach my $key (@rest) {
129 2 $value->{$key} = [ $self->split_field_list( $value->{$key} ) ];
130 }
131 1 return $value;
132 }
133 10 pos($value) = 0;
134 10 my @data;
135 10 $value .= ',';
136 10 ▶ while ( $value =~
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ m[ \G \s* (?<weak> W/ )? " (?<value> [^"]*? ) " \s* ,+ \s* ]gisx) 17 10 27
137 m{ \G \s* (?<weak> W/ )? " (?<value> [^"]*? ) " \s* ,+ \s* }gsix )
138 {
139 17 my $value = $+{value};
140 17 ▶ push @data => $+{weak} ? \$value : $value;
Branch: 1 branch
BranchTrueFalseTotal
$+{'weak'} ? : 7 10 17
141 }
142 10 return @data;
143 }
144  
145 15 ▶sub build_field_hash {
Subroutine build_field_hash: called CC=4 SCAR=13.9
Pod documented
146 15 my ( $self, @args ) = _self(@_);
147 15 ▶ if ( ref $args[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq "HASH") 4 11 15
148 4 return $self->build_field_hash( %{ $args[0] } );
149 }
150 11 my %data = @args;
151 return join ', ', sort map {
152 11 $self->encode_key($_)
153 . (
154 defined( $data{$_} )
155 ? '='
156 . (
157 ( $data{$_} =~ m{[=,]} )
158 ? '"' . $data{$_} . '"'
159 17 ▶ : $data{$_}
Branch: 2 branches
BranchTrueFalseTotal
defined $data{$_} ? : 12 5 17
$data{$_} =~ /[=,]/ ? : 8 4 12
160 )
161 : ''
162 )
163 } keys %data;
164 }
165  
166 15 ▶sub build_field_list {
Subroutine build_field_list: called CC=3 SCAR=11.0
Pod documented
167 15 my ( $self, @args ) = _self(@_);
168 15 ▶ if ( ref $args[0] eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq "ARRAY") 6 9 15
169 6 return $self->build_field_list( @{ $args[0] } );
170 }
171 9 ▶ return join ', ', map { ref($_) ? 'W/"' . $$_ . '"' : qq{"$_"} } @args;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 8 17 25
172 }
173  
174 7 ▶sub new {
Subroutine new: called CC=3 SCAR=13.1
Pod documented
175 7 ▶ my $class = shift // __PACKAGE__;
Condition50%1/2
shift() // __PACKAGE__
l!l
70
MC/DC0%0/1
shift() // __PACKAGE__
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
176 7 ▶ return bless {@_} => ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
070
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
177 }
178  
179 6 ▶sub encode {
Subroutine encode: called CC=4 SCAR=14.0
Pod documented
180 6 my $self = shift;
181 6 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 2 4 6
182 4 ▶ if ( @_ > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ > 1) { } 2 2 4
elsif (ref $_[0] eq 'HASH') { } 2 0 2
183 2 return $self->_encode_hash_deeply(@_);
184 }
185 elsif ( ref $_[0] eq 'HASH' ) {
186 2 return $self->_encode_hash_deeply( $_[0] );
187 }
188 else {
189 0 return $self->encode_key( $_[0] );
190 }
191 }
192  
193 0 ▶sub decode {
Subroutine decode: not called CC=4 SCAR=30.0
Pod documented
194 0 my $self = shift;
195 0 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
196 0 ▶ if ( @_ > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ > 1) { } 0 0 0
elsif (ref $_[0] eq 'HASH') { } 0 0 0
197 0 return $self->decode_hash(@_);
198 }
199 elsif ( ref $_[0] eq 'HASH' ) {
200 0 return $self->decode_hash( $_[0] );
201 }
202 else {
203 0 return $self->decode_key( $_[0] );
204 }
205 }
206  
207 5 ▶sub split {
Subroutine split: called CC=8 SCAR=20.8
Pod documented
208 5 my $self = shift;
209 5 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 4 5
210 4 my $val = shift;
211 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
212 2 return $self->split_field_list($val);
213 }
214 elsif ( not ref $val ) {
215 1 return $self->split_field_hash($val);
216 }
217 elsif ( ref $val eq 'HASH' ) {
218 1 foreach my $key (@_) {
219 3 ▶ next unless $val->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $val->{$key} 0 3 3
220 3 ▶ if ( $val->{$key} =~ m{^ \s* ( W/ )? " }six ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val->{$key} =~ m[^ \s* ( W/ )? " ]six) { } 2 1 3
221 2 $val->{$key} = [ $self->split_field_list( $val->{$key} ) ];
222 }
223 else {
224 1 $val->{$key} = { $self->split_field_hash( $val->{$key} ) };
225 }
226 }
227 1 return $val;
228 }
229 }
230  
231 7 ▶sub build {
Subroutine build: called CC=3 SCAR=11.0
Pod documented
232 7 my $self = shift;
233 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
234 3 return $self->build_field_hash(@_);
235 }
236 elsif ( ref $_[0] eq 'ARRAY' ) {
237 3 return $self->build_field_list(@_);
238 }
239 else {
240 1 return $self->build_field_hash(@_);
241 }
242 }
243  
244 2 ▶sub etags {
Subroutine etags: called CC=1 SCAR=0.0
Pod documented
245 2 my $self = shift;
246 2 return $self->build_field_list(@_);
247 }
248  
249 16 1;
250  
251 __END__
↑ Summary