From: Ian Beckwith Date: Sat, 28 Nov 2015 02:26:18 +0000 (+0000) Subject: Imported Upstream version 0.19 X-Git-Tag: upstream/0.19^0 X-Git-Url: http://erislabs.net/gitweb/?p=liburi-template-perl.git;a=commitdiff_plain;h=1d94fcb2781945132b9a29e93744b6c168e647aa Imported Upstream version 0.19 --- diff --git a/Changes b/Changes index edd4a6f..3e3cf90 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,9 @@ Revision history for Perl extension URI::Template +0.19 2015-01-05 + - Allow empty template as per spec (RT 101109) + - template() now accepts a new string as an argument (Artem Krivopolenov) + 0.18 2013-12-05 - Fix empty hash/array special casing to not return anything diff --git a/META.yml b/META.yml index 4ce2ac9..21d777f 100644 --- a/META.yml +++ b/META.yml @@ -27,4 +27,4 @@ requires: resources: license: http://dev.perl.org/licenses/ repository: http://github.com/bricas/uri-template -version: 0.18 +version: 0.19 diff --git a/README b/README index b8e6224..86648fa 100644 --- a/README +++ b/README @@ -3,8 +3,16 @@ NAME SYNOPSIS use URI::Template; + my $template = URI::Template->new( 'http://example.com/{x}' ); my $uri = $template->process( x => 'y' ); + + # or + + my $template = URI::Template->new(); + $template->template( 'http://example.com/{x}' ); + my $uri = $template->process( x => 'y' ); + # uri is a URI object with value 'http://example.com/y' DESCRIPTION @@ -20,10 +28,11 @@ INSTALLATION METHODS new( $template ) Creates a new URI::Template instance with the template passed in as the - first parameter. + first parameter (optional). - template - This method returns the original template string. + template( $template ) + This method returns the original template string. If provided, it will + also set and parse a new template string. variables Returns an array of unique variable names found in the template. NB: @@ -49,7 +58,7 @@ AUTHORS * Ricardo SIGNES COPYRIGHT AND LICENSE - Copyright 2007-2013 by Brian Cassidy + Copyright 2007-2015 by Brian Cassidy This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. diff --git a/lib/URI/Template.pm b/lib/URI/Template.pm index 54a73ac..d96c895 100644 --- a/lib/URI/Template.pm +++ b/lib/URI/Template.pm @@ -3,7 +3,7 @@ package URI::Template; use strict; use warnings; -our $VERSION = '0.18'; +our $VERSION = '0.19'; use URI; use URI::Escape (); @@ -24,7 +24,7 @@ my %TOSTRING = ( sub new { my $class = shift; - my $templ = shift || die 'No template provided'; + my $templ = shift || ''; my $self = bless { template => $templ, _vars => {} } => $class; $self->_study; @@ -286,7 +286,18 @@ sub _compile_expansion { } sub template { - return $_[ 0 ]->{ template }; + my $self = shift; + my $new_template = shift; + + # Update template + if ( $new_template && $new_template ne $self->{ template } ) { + $self->{ template } = $new_template; + $self->{ _vars } = {}; + $self->_study; + return $self; + } + + return $self->{ template }; } sub variables { @@ -328,8 +339,16 @@ URI::Template - Object for handling URI templates (RFC 6570) =head1 SYNOPSIS use URI::Template; + my $template = URI::Template->new( 'http://example.com/{x}' ); my $uri = $template->process( x => 'y' ); + + # or + + my $template = URI::Template->new(); + $template->template( 'http://example.com/{x}' ); + my $uri = $template->process( x => 'y' ); + # uri is a URI object with value 'http://example.com/y' =head1 DESCRIPTION @@ -349,11 +368,12 @@ L<< http://tools.ietf.org/html/rfc6570 >>. =head2 new( $template ) Creates a new L instance with the template passed in -as the first parameter. +as the first parameter (optional). -=head2 template +=head2 template( $template ) -This method returns the original template string. +This method returns the original template string. If provided, it will also set and parse a +new template string. =head2 variables @@ -387,7 +407,7 @@ URI object. =head1 COPYRIGHT AND LICENSE -Copyright 2007-2013 by Brian Cassidy +Copyright 2007-2015 by Brian Cassidy This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. diff --git a/t/10-basic.t b/t/10-basic.t index 934d87d..4d6f404 100644 --- a/t/10-basic.t +++ b/t/10-basic.t @@ -1,14 +1,37 @@ use strict; use warnings; -use Test::More tests => 15; +use Test::More tests => 21; use_ok( 'URI::Template' ); -# fatal - no template provided +# new, empty template { - eval { URI::Template->new; }; - ok( $@ ); + my $template = URI::Template->new; + isa_ok( $template, 'URI::Template' ); + + { + my $result = $template->process(); + is( $result, '', 'process() for empty template' ); + isa_ok( $result, 'URI', 'return value from process() isa URI' ); + } +} + +# Update template +{ + my $template = URI::Template->new; + is( "$template", '', 'stringify from empty' ); + + my $text = 'http://foo.com/{bar}/{baz}'; + $template->template($text); + + is( "$template", $text, 'stringify from updated template' ); + + { + my $result = $template->process( bar => 'x', baz => 'y' ); + is( $result, 'http://foo.com/x/y', 'process() for updated template' ); + isa_ok( $result, 'URI', 'return value from process() isa URI' ); + } } {