X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2FURI%2FTemplate.pm;h=24c01f1261dcc1a4be253d32daa1f37aa5ebb56a;hb=HEAD;hp=390f6a6ec94849050ee104c3231210f63bc7baeb;hpb=39817159a196a6acb7ce58266633ad6ea8490864;p=liburi-template-perl.git diff --git a/lib/URI/Template.pm b/lib/URI/Template.pm index 390f6a6..24c01f1 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.17'; +our $VERSION = '0.22'; use URI; use URI::Escape (); @@ -24,7 +24,8 @@ my %TOSTRING = ( sub new { my $class = shift; - my $templ = shift || die 'No template provided'; + my $templ = shift; + $templ = '' unless defined $templ; my $self = bless { template => $templ, _vars => {} } => $class; $self->_study; @@ -123,10 +124,7 @@ sub _tostring_query { $join = '&' if $exp->{ op } =~ /[?&]/; if ( ref $value eq 'ARRAY' ) { - if( !@$value ) { - return if $var->{ explode }; - return $var->{ name } . '='; - } + return if !@$value; if ( $var->{ explode } ) { return join( $join, map { $var->{ name } . '=' . _quote( $_, $safe ) } @$value ); @@ -137,10 +135,7 @@ sub _tostring_query { } } elsif ( ref $value eq 'HASH' ) { - if( !keys %$value ) { - return if $var->{ explode }; - return $var->{ name } . '='; - } + return if !keys %$value; if ( $var->{ explode } ) { return join( $join, @@ -204,15 +199,16 @@ sub _tostring_path { sub _study { my ( $self ) = @_; my @hunks = grep { defined && length } split /(\{.+?\})/, $self->template; + my $pos = 1; for ( @hunks ) { next unless /^\{(.+?)\}$/; - $_ = $self->_compile_expansion( $1 ); + $_ = $self->_compile_expansion( $1, $pos++ ); } $self->{ studied } = \@hunks; } sub _compile_expansion { - my ( $self, $str ) = @_; + my ( $self, $str, $pos ) = @_; my %exp = ( op => '', vars => [], str => $str ); if ( $str =~ /^([+#.\/;?&|!\@])(.+)/ ) { @@ -239,7 +235,7 @@ sub _compile_expansion { # remove "optional" flag (for opensearch compatibility) $var{ name } =~ s{\?$}{}; - $self->{ _vars }->{ $var{ name } }++; + $self->{ _vars }->{ $var{ name } } = $pos; push @{ $exp{ vars } }, \%var; } @@ -292,11 +288,23 @@ sub _compile_expansion { } sub template { - return $_[ 0 ]->{ template }; + my $self = shift; + my $templ = shift; + + # Update template + if ( defined $templ && $templ ne $self->{ template } ) { + $self->{ template } = $templ; + $self->{ _vars } = {}; + $self->_study; + return $self; + } + + return $self->{ template }; } sub variables { - return keys %{ $_[ 0 ]->{ _vars } }; + my @vars = sort {$_[ 0 ]->{ _vars }->{ $a } <=> $_[ 0 ]->{ _vars }->{ $b } } keys %{ $_[ 0 ]->{ _vars } }; + return @vars; } sub expansions { @@ -334,14 +342,22 @@ 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 This module provides a wrapper around URI templates as described in RFC 6570: -http://tools.ietf.org/html/rfc6570 +L<< http://tools.ietf.org/html/rfc6570 >>. =head1 INSTALLATION @@ -355,15 +371,16 @@ 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 -Returns an array of unique variable names found in the template. NB: they are returned in random order. +Returns an array of unique variable names found in the template (in the order of appearance). =head2 expansions @@ -393,7 +410,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.