Revision history for Perl extension URI::Template
+0.07 Thu May 24 2007
+ - allow the user to pass an array ref to process and process_to_string
+ which fills values by position
+ - added all_variables() which returns all arguments by position (including
+ duplicates)
+
0.06 Mon Apr 23 2007
- added some caching for better deparse() performance [Paul "LeoNerd" Evans]
README
t/01-use.t
t/10-basic.t
+t/11-ordered.t
t/20-deparse.t
t/98-pod.t
t/99-podcoverage.t
---
name: URI-Template
-version: 0.06
+version: 0.07
author:
- 'Brian Cassidy <brian.cassidy@nald.ca>'
abstract: Object for handling URI templates
provides:
URI::Template:
file: lib/URI/Template.pm
- version: 0.06
-generated_by: Module::Build version 0.2807
+ version: 0.07
+generated_by: Module::Build version 0.2808
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.2.html
version: 1.2
Returns an array of variable names found in the template. NB: they are
returned in random order.
- process( %vars )
- Given a list of key-value pairs, it will URI escape the values and
- substitute them in to the template. Returns a URI object.
-
- process_to_string( %vars )
- Processes key-values pairs like the "process" method, but doesn't
- inflate the result to a URI object.
+ all_variables( )
+ Returns an array of variable names found as they appear in template --
+ in order, duplicates included.
+
+ process( %vars|\@values )
+ 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.
+
+ process_to_string( %vars|\@values )
+ Processes input like the "process" method, but doesn't inflate the
+ result to a URI object.
deparse( $uri )
Does some rudimentary deparsing of a uri based on the current template.
use strict;
use warnings;
-our $VERSION = '0.06';
+our $VERSION = '0.07';
use URI;
use URI::Escape ();
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( )
-Given a list of key-value pairs, it will URI escape the values and
+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 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
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<process> method, but doesn't
+Processes input like the C<process> 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 = @_;
# fix undef vals
for my $var ( @vars ) {
- $params{ $var } = '' unless defined $params{ $var };
+ $params{ $var } = defined $params{ $var }
+ ? URI::Escape::uri_escape( $params{ $var } )
+ : '';
}
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 )
+ : ''
+ : ''/eg;
return $uri;
}
use strict;
use warnings;
-use Test::More tests => 14;
+use Test::More tests => 15;
use_ok( 'URI::Template' );
+# fatal - no template provided
+{
+ eval { URI::Template->new; };
+ ok( $@ );
+}
+
{
my $text = 'http://foo.com/{bar}/{baz}?q=%7B';
my $template = URI::Template->new( $text );
--- /dev/null
+use strict;
+use warnings;
+
+use Test::More tests => 9;
+
+use_ok( 'URI::Template' );
+
+{
+ my $text = 'http://foo.com/{arg2}/{arg1}';
+ my $template = URI::Template->new( $text );
+ isa_ok( $template, 'URI::Template' );
+ is_deeply( [ $template->all_variables ], [ qw( arg2 arg1 ) ], 'all_variables()' );
+
+ {
+ my $result = $template->process( [ qw( x y ) ] );
+ is( $result, 'http://foo.com/x/y', 'process(\@args)' );
+ isa_ok( $result, 'URI', 'return value from process() isa URI' );
+ }
+
+ {
+ my $result = $template->process_to_string( [ qw( x y ) ] );
+ is( $result, 'http://foo.com/x/y', 'process_to_string(\@args)' );
+ ok( !ref $result, 'result is not a ref' );
+ }
+
+ # test for 0 as value
+ {
+ my $result = $template->process_to_string( [ qw( 0 0 ) ] );
+ is( $result, 'http://foo.com/0/0', 'process w/ 0' );
+ }
+
+ # test with no values
+ {
+ my $result = $template->process_to_string( [ ] );
+ is( $result, 'http://foo.com//', 'process w/ no values' );
+ }
+}
+