| 1 | package Magpie::Plugin::URITemplate; | ||||||||||||||
| 2 | 44 | $Magpie::Plugin::URITemplate::VERSION = '1.141660'; | |||||||||||||
| 3 | 44 | ▶ | use Moose::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | # | ||||||||||||||
| 5 | # ABSTRACT: A Role to add URI Template-like path variable capture; | ||||||||||||||
| 6 | # | ||||||||||||||
| 7 | 44 | ▶ | use Magpie::Constants; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | has uri_template_param_names => ( | ||||||||||||||
| 10 | is => 'rw', | ||||||||||||||
| 11 | isa => 'ArrayRef', | ||||||||||||||
| 12 | 12 | ▶ | default => sub {[]}, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 44 | clearer => 'clear_params', | |||||||||||||
| 14 | ); | ||||||||||||||
| 15 | |||||||||||||||
| 16 | has uri_template => ( | ||||||||||||||
| 17 | is => 'rw', | ||||||||||||||
| 18 | isa => 'Str', | ||||||||||||||
| 19 | required => 1, | ||||||||||||||
| 20 | trigger => sub { | ||||||||||||||
| 21 | 27 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | 27 | $self->clear_regex; | |||||||||||||
| 23 | 27 | $self->clear_params; | |||||||||||||
| 24 | }, | ||||||||||||||
| 25 | 44 | ); | |||||||||||||
| 26 | |||||||||||||||
| 27 | 44 | has uri_template_regex => ( | |||||||||||||
| 28 | is => 'ro', | ||||||||||||||
| 29 | isa => 'RegexpRef', | ||||||||||||||
| 30 | lazy => 1, | ||||||||||||||
| 31 | builder => '_build_regexp', | ||||||||||||||
| 32 | clearer => 'clear_regex', | ||||||||||||||
| 33 | ); | ||||||||||||||
| 34 | |||||||||||||||
| 35 | sub _build_regexp { | ||||||||||||||
| 36 | 15 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_build_regexp: called CC=1 SCAR=0.0
| |||||||||||||||
| 37 | 15 | my ($re, $names) = process_template($self->uri_template); | |||||||||||||
| 38 | 15 | $self->uri_template_param_names($names); | |||||||||||||
| 39 | 15 | return $re; | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | sub uri_template_params { | ||||||||||||||
| 43 | 15 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
uri_template_params: called CC=5 SCAR=16.4
Pod
undocumented
| |||||||||||||||
| 44 | 15 | my $extractor = $self->uri_template_regex; | |||||||||||||
| 45 | 15 | my $names = $self->uri_template_param_names; | |||||||||||||
| 46 | 15 | my $path = $self->request->path_info; | |||||||||||||
| 47 | #my @vals = ( $path =~ $extractor ); | ||||||||||||||
| 48 | 15 | my %params = (); | |||||||||||||
| 49 | 15 | ▶ | if ($path =~ $extractor) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 50 | 15 | ▶ | for (my $i = 0; $i < @{$names}; $i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 51 | 22 | $params{$names->[$i]} = $+{$names->[$i]}; | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | } | ||||||||||||||
| 54 | 15 | ▶ | unless (scalar keys %params == scalar @{$names}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | 0 | warn "URI template param extraction mismatch\n"; | |||||||||||||
| 56 | } | ||||||||||||||
| 57 | |||||||||||||||
| 58 | |||||||||||||||
| 59 | |||||||||||||||
| 60 | # for (my $i = 0; $i < @{$names}; $i++) { | ||||||||||||||
| 61 | # $params{$names->[$i]} = $vals[$i]; | ||||||||||||||
| 62 | # } | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 15 | ▶ | return wantarray ? %params : \%params; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 65 | } | ||||||||||||||
| 66 | |||||||||||||||
| 67 | sub process_template { | ||||||||||||||
| 68 | 24 | ▶ | my $template = shift; | ||||||||||||
|
Subroutine
process_template: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||
| 69 | 24 | my @names = (); | |||||||||||||
| 70 | 24 | my $intoken = 0; | |||||||||||||
| 71 | 24 | my $token = undef; | |||||||||||||
| 72 | 24 | my $transformed = ''; | |||||||||||||
| 73 | #my @chars = split '', $string; | ||||||||||||||
| 74 | |||||||||||||||
| 75 | 24 | for (split '', $template) { | |||||||||||||
| 76 | 699 | ▶ | if ($_ eq '{') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 77 | 36 | $intoken = 1; | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | elsif ($_ eq '}') { | ||||||||||||||
| 80 | 36 | push @names, $token; | |||||||||||||
| 81 | 36 | $transformed .= '(?<' . $token . '>[^/]*)'; | |||||||||||||
| 82 | 36 | $token = undef; | |||||||||||||
| 83 | 36 | $intoken = 0; | |||||||||||||
| 84 | |||||||||||||||
| 85 | } | ||||||||||||||
| 86 | else { | ||||||||||||||
| 87 | 627 | ▶ | if ($intoken) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 88 | 296 | $token .= $_; | |||||||||||||
| 89 | } | ||||||||||||||
| 90 | else { | ||||||||||||||
| 91 | 331 | $transformed .= $_; | |||||||||||||
| 92 | } | ||||||||||||||
| 93 | } | ||||||||||||||
| 94 | } | ||||||||||||||
| 95 | |||||||||||||||
| 96 | 24 | my $re = qr|$transformed|; | |||||||||||||
| 97 | 24 | return ($re, \@names); | |||||||||||||
| 98 | |||||||||||||||
| 99 | } | ||||||||||||||
| 100 | |||||||||||||||
| 101 | 44 | ▶ | no Moose::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 102 | |||||||||||||||
| 103 | 44 | 1; | |||||||||||||
| 104 | |||||||||||||||
| 105 | __END__ | ||||||||||||||
| 106 | =pod | ||||||||||||||
| 107 | |||||||||||||||
| 108 | =head1 NAME | ||||||||||||||
| 109 | |||||||||||||||
| 110 | Magpie::Plugin::URITemplate - A Role to add URI Template-like path variable capture; | ||||||||||||||
| 111 | |||||||||||||||
| 112 | =head1 VERSION | ||||||||||||||
| 113 | |||||||||||||||
| 114 | version 1.141660 | ||||||||||||||
| 115 | |||||||||||||||
| 116 | =head1 AUTHORS | ||||||||||||||
| 117 | |||||||||||||||
| 118 | =over 4 | ||||||||||||||
| 119 | |||||||||||||||
| 120 | =item * | ||||||||||||||
| 121 | |||||||||||||||
| 122 | Kip Hampton <kip.hampton@tamarou.com> | ||||||||||||||
| 123 | |||||||||||||||
| 124 | =item * | ||||||||||||||
| 125 | |||||||||||||||
| 126 | Chris Prather <chris.prather@tamarou.com> | ||||||||||||||
| 127 | |||||||||||||||
| 128 | =back | ||||||||||||||
| 129 | |||||||||||||||
| 130 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 131 | |||||||||||||||
| 132 | This software is copyright (c) 2011 by Tamarou, LLC. | ||||||||||||||
| 133 | |||||||||||||||
| 134 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 135 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 136 | |||||||||||||||
| 137 | =cut | ||||||||||||||
| 138 | |||||||||||||||