Skip to content

Instantly share code, notes, and snippets.

@dcunited001
Created February 14, 2013 16:27
Show Gist options
  • Save dcunited001/4953982 to your computer and use it in GitHub Desktop.
Save dcunited001/4953982 to your computer and use it in GitHub Desktop.
An example of using the Addressable URI Templates. Couldn't find any other detailed gists. Most of these examples are just lifted from the Addressable specs.
# Addressable::Template#match
# Addressable::Template#variables
# {$1} where $1=reqvarname
# {$1=$2} where $1=varname & $2=defvalue
# {-list|$1|$2,$3} where $1=delim & $2=varname
# {-join|$1|$2=$3,$4=$5} where $1=delim & $2=varname & $3=value, etc
# {-prefix}
# {-suffix}
#E.G.
Addressable::Template.new("/{number}/").
extract("/99/") == {"number" => "99"}
Addressable::Template.new("/{number}/{number}/").
extract("/99.99/") == {"number" => "99"}
Addressable::Template.new("/{number}{-prefix|.|number}").
extract("/99.99/") == {"number" => "99"}
Addressable::Template.new("/search/{-prefix|/|stuff}/").
extract("/search/one/spacer/two/") ==
{"stuff" => ["one", "spacer", "two"]}
Addressable::Template.new("/search/{-suffix|/|stuff}/").
extract("/search/one/spacer/two/") ==
{"stuff" => ["one", "spacer", "two"]}
Addressable::Template.new("/search/{-list|+|query}/").
extract("/search/an+example+search+query/") ==
{ "query" => ["an", "example", "search", "query"] }
Addressable::Template.new("/search/?{-join|&|a,b,c}").
extract("/search/?a=one&b=two&c=three") ==
{ "a" => "one", "b" => "two", "c" => "three" }
#Addressable::Template.new("/?email={-opt|[email protected]|test}").
# extract("/[email protected]") ==
#Addressable::Template.new("/?email={-opt|[email protected]|test}").
# extract("/[email protected]") ==
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment