File Coverage

blib/lib/Web/Machine/Util.pm
Criterion Covered Total %
statement 48 48 100.0
branch 21 24 87.5
condition n/a
subroutine 14 14 100.0
pod 6 7 85.7
total 89 93 95.7


line stmt bran cond sub pod time code
1             package Web::Machine::Util;
2             # ABSTRACT: General Utility module
3              
4 19     19   107853 use strict;
  19         24  
  19         459  
5 19     19   57 use warnings;
  19         22  
  19         598  
6              
7             our $VERSION = '0.17';
8              
9 19     19   60 use Carp qw[ confess ];
  19         19  
  19         705  
10 19     19   62 use Scalar::Util qw[ blessed ];
  19         29  
  19         788  
11 19     19   66 use List::Util qw[ first ];
  19         22  
  19         1521  
12              
13 19     19   8019 use HTTP::Headers::ActionPack 0.07;
  19         31490  
  19         1270  
14              
15 19         145 use Sub::Exporter -setup => {
16             exports => [qw[
17             first
18             pair_key
19             pair_value
20             bind_path
21             create_date
22             create_header
23             inflate_headers
24             ]]
25 19     19   7098 };
  19         122551  
26              
27 210     210 1 596 sub pair_key { ( keys %{ $_[0] } )[0] }
  210         680  
28 54     54 1 66 sub pair_value { ( values %{ $_[0] } )[0] }
  54         136  
29              
30             {
31             my $ACTION_PACK = HTTP::Headers::ActionPack->new;
32 132     132 1 328 sub create_header { $ACTION_PACK->create( @_ ) }
33 21     21 1 946 sub create_date { $ACTION_PACK->create( 'DateHeader' => shift ) }
34 124     124 1 122207 sub inflate_headers { $ACTION_PACK->inflate( @_ ) }
35 18     18 0 35 sub get_action_pack { $ACTION_PACK }
36             }
37              
38             sub bind_path {
39 23     23 1 37 my ($spec, $path) = @_;
40 23 50       52 my @parts = grep { defined $_ && $_ ne q{} } split /\// => $path;
  66         192  
41 23 50       39 my @spec = grep { defined $_ && $_ ne q{} } split /\// => $spec;
  71         180  
42              
43 23         20 my @results;
44 23         44 foreach my $i ( 0 .. $#spec ) {
45 46 100       127 if ( $spec[ $i ] =~ /^\*$/ ) {
    100          
    100          
46 5         6 push @results => @parts;
47 5         8 @parts = ();
48 5         4 last;
49             }
50             elsif ( $spec[ $i ] =~ /^\:/ ) {
51 17 100       31 return unless defined $parts[ 0 ];
52 16         21 push @results => shift @parts;
53             }
54             elsif ( $spec[ $i ] =~ /^\?\:/ ) {
55 9 100       20 push @results => shift @parts
56             if defined $parts[ 0 ];
57             }
58             else {
59 15 50       23 return unless defined $parts[ 0 ];
60 15 100       28 return unless $spec[ $i ] eq $parts[ 0 ];
61 14         17 shift @parts;
62             }
63             }
64              
65 21 100       45 return if @parts;
66              
67             wantarray
68             ? @results
69 17 100       89 : (scalar @results == 1)
    100          
70             ? $results[0]
71             : @results;
72             }
73              
74             1;
75              
76             __END__