Skip to content
A URI template library for ruby.
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.


Type Name Latest commit message Commit time
Failed to load latest commit information.
lib Changed licence to MIT Mar 21, 2014
spec Changed licence to MIT Mar 21, 2014
.gitignore Ignore more coverage files Jun 16, 2013
.gitmodules Merged some commits into uritemplate-test suite (@mnot seems busy) Jun 16, 2013
.rspec Updated gemspec for development Sep 10, 2012
.travis.yml added ruby 2.1.0 to .travis.yml Mar 21, 2014
.yardopts Changed licence to MIT Mar 21, 2014
Gemfile.lock Fix typo in Nov 24, 2013
Rakefile Added correct options for rspec. Nov 18, 2011
uri_template.gemspec Changed licence to MIT Mar 21, 2014

URITemplate - a uri template library

Build Status Dependency Status Code Climate Coverage

With URITemplate you can generate URIs based on simple templates and extract variables from URIs using the same templates. There are currently two syntaxes defined. Namely the one defined in RFC 6570 and a colon based syntax, similiar to the one used by sinatra.

From version 0.2.0, it will use escape_utils if available. This will significantly boost uri-escape/unescape performance if more characters need to be escaped ( may be slightly slower in trivial cases. working on that ... ), but does not run everywhere. To enable this, do the following:

# escape_utils has to be loaded when uri_templates is loaded
gem 'escape_utils'
require 'escape_utils'

gem 'uri_template'
require 'uri_template'

URITemplate::Utils.using_escape_utils? #=> true


require 'uri_template'

tpl ='http://{host}{/segments*}/{file}{.extensions*}')

# This will give:

# This will give: { 'host'=>'','segments'=>['path','to','a'],'file'=>'file','extensions'=>['x','y']}

# If you like colon templates more:
tpl2 =, '/:x/y')

# This will give: {'x' => 'z'}

# This will give a new uri template with just the host expanded:
tpl.expand_partial(host: "")

RFC 6570 Syntax

The syntax defined by RFC 6570 is pretty straight forward. Basically anything surrounded by curly brackets is interpreted as variable.'{variable}').expand('variable' => 'value') #=> "value"

The way variables are inserted can be modified using operators. The operator is the first character between the curly brackets. There are seven operators defined #, +, ;, ?, &, / and .. So if you want to create a form-style query do this:'{?variable}').expand('variable' => 'value') #=> "?variable=value"


I have assembled one benchmark based on the uritemplate-test examples. You can find them in the "benchmarks" folder. The short result: uri_template is 2-10x faster than addressable on ruby 1.9.3.

You can’t perform that action at this time.