X-Git-Url: http://erislabs.net/gitweb/?p=liburi-template-perl.git;a=blobdiff_plain;f=lib%2FURI%2FTemplate.pm;h=c7f45a2e5dca58fe731c5755f47cd630cdced550;hp=54ea7d28f6cead4a4cb4cb0cbb4e87ef7151830b;hb=6ce2a97438a9ca90e550caeefb1d3cecf259ded4;hpb=3f3d56e5fb9a55ee113ccf2bc03f3a453f80baa0 diff --git a/lib/URI/Template.pm b/lib/URI/Template.pm index 54ea7d2..c7f45a2 100644 --- a/lib/URI/Template.pm +++ b/lib/URI/Template.pm @@ -3,12 +3,14 @@ package URI::Template; use strict; use warnings; -our $VERSION = '0.04'; +our $VERSION = '0.12'; use URI; use URI::Escape (); use overload '""' => \&as_string; +my $unsafe = q(^A-Za-z0-9\-_.~!\$\&'()*+,;=:/?\[\]#@); + =head1 NAME URI::Template - Object for handling URI templates @@ -26,23 +28,14 @@ URI::Template - Object for handling URI templates =head1 DESCRIPTION This is an initial attempt to provide a wrapper around URI templates -as described at http://www.ietf.org/internet-drafts/draft-gregorio-uritemplate-00.txt +as described at http://www.ietf.org/internet-drafts/draft-gregorio-uritemplate-01.txt =head1 INSTALLATION -To install this module via Module::Build: - - perl Build.PL - ./Build # or `perl Build` - ./Build test # or `perl Build test` - ./Build install # or `perl Build install` - -To install this module via ExtUtils::MakeMaker: - - perl Makefile.PL - make - make test - make install + perl Makefile.PL + make + make test + make install =head1 METHODS @@ -74,19 +67,34 @@ sub as_string { =head2 variables( ) -Returns an array of variable names found in the template. +Returns an array of unique variable names found in the template. +NB: they are returned in random order. =cut sub variables { my $self = shift; - my %vars = map { $_ => 1 } $self->as_string =~ /{(.+?)}/g; + my %vars = map { $_ => 1 } $self->all_variables; return keys %vars; } -=head2 process( %vars ) +=head2 all_variables( ) + +Returns an array of variable names found as they appear in template -- +in order, duplicates included. + +=cut + +sub all_variables { + my $self = shift; + my @vars = $self->as_string =~ /{(.+?)}/g; + return @vars; +} + +=head2 process( %vars|\@values ) -Given a list of key-value pairs, it will URI escape the values and +Given a list of key-value pairs or an array ref of values (for +positional substitution), it will URI escape the values and substitute them in to the template. Returns a URI object. =cut @@ -96,14 +104,25 @@ sub process { return URI->new( $self->process_to_string( @_ ) ); } -=head2 process_to_string( %vars ) +=head2 process_to_string( %vars|\@values ) -Processes key-values pairs like the C method, but doesn't +Processes input like the C method, but doesn't inflate the result to a URI object. =cut sub process_to_string { + my $self = shift; + + if ( ref $_[ 0 ] ) { + return $self->_process_by_position( @_ ); + } + else { + return $self->_process_by_key( @_ ); + } +} + +sub _process_by_key { my $self = shift; my @vars = $self->variables; my %params = @_; @@ -111,11 +130,29 @@ sub process_to_string { # fix undef vals for my $var ( @vars ) { - $params{ $var } = '' unless defined $params{ $var }; + $params{ $var } + = defined $params{ $var } + ? URI::Escape::uri_escape( $params{ $var }, $unsafe ) + : ''; } my $regex = '\{(' . join( '|', map quotemeta, @vars ) . ')\}'; - $uri =~ s/$regex/URI::Escape::uri_escape($params{$1})/eg; + $uri =~ s/$regex/$params{$1}/eg; + + return $uri; +} + +sub _process_by_position { + my $self = shift; + my @params = @{ $_[ 0 ] }; + + my $uri = $self->as_string; + + $uri =~ s/{(.+?)}/@params + ? defined $params[ 0 ] + ? URI::Escape::uri_escape( shift @params, $unsafe ) + : '' + : ''/eg; return $uri; } @@ -131,27 +168,29 @@ sub deparse { my $self = shift; my $uri = shift; - my $templ = $self->as_string; - my @vars = $templ =~ /{(.+?)}/g; - $templ =~ s/{.+?}/(.+?)/g; - my @matches = $uri =~ /$templ/; + if ( !$self->{ deparse_re } ) { + my $templ = $self->as_string; + $self->{ vars_list } = [ $templ =~ /{(.+?)}/g ]; + $templ =~ s/{.+?}/(.+?)/g; + # If the template ends w/ a match, then make it greedy. + $templ =~ s/\Q(.+?)\E$/(.+)/; + $self->{ deparse_re } = qr/$templ/; + } + + my @matches = $uri =~ $self->{ deparse_re }; my %results; - @results{ @vars } = @matches; + @results{ @{ $self->{ vars_list } } } = @matches; return %results; } =head1 AUTHOR -=over 4 - -=item * Brian Cassidy Ebricas@cpan.orgE - -=back +Brian Cassidy Ebricas@cpan.orgE =head1 COPYRIGHT AND LICENSE -Copyright 2007 by Brian Cassidy +Copyright 2008 by Brian Cassidy This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.