Imported upstream version 0.07 upstream/0.07
authorIan Beckwith <ianb@erislabs.net>
Sat, 1 Mar 2008 00:34:49 +0000 (00:34 +0000)
committerIan Beckwith <ianb@erislabs.net>
Sat, 1 Mar 2008 00:34:49 +0000 (00:34 +0000)
Changes
MANIFEST
META.yml
README
lib/URI/Template.pm
t/10-basic.t
t/11-ordered.t [new file with mode: 0644]

diff --git a/Changes b/Changes
index cb2217e..41e12ef 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,11 @@
 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]
 
index dd9981e..eaf12d9 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -7,6 +7,7 @@ META.yml
 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
index ac515c7..6fcf3f2 100644 (file)
--- a/META.yml
+++ b/META.yml
@@ -1,6 +1,6 @@
 ---
 name: URI-Template
-version: 0.06
+version: 0.07
 author:
   - 'Brian Cassidy <brian.cassidy@nald.ca>'
 abstract: Object for handling URI templates
@@ -12,8 +12,8 @@ requires:
 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
diff --git a/README b/README
index eb4da93..59f5a76 100644 (file)
--- a/README
+++ b/README
@@ -43,13 +43,18 @@ METHODS
     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.
index 233ce61..47cfb2b 100644 (file)
@@ -3,7 +3,7 @@ package URI::Template;
 use strict;
 use warnings;
 
-our $VERSION = '0.06';
+our $VERSION = '0.07';
 
 use URI;
 use URI::Escape ();
@@ -81,13 +81,27 @@ are returned in random order.
 
 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
@@ -97,14 +111,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<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 = @_;
@@ -112,11 +137,28 @@ 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 } )
+            : '';
     }
 
     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;
 }
index 162063c..4240367 100644 (file)
@@ -1,10 +1,16 @@
 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 );
diff --git a/t/11-ordered.t b/t/11-ordered.t
new file mode 100644 (file)
index 0000000..a160818
--- /dev/null
@@ -0,0 +1,38 @@
+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' );
+    }
+}
+