Skip to content

Commit

Permalink
Fix URI template operators 'prefix', 'suffix' and 'list' to return th…
Browse files Browse the repository at this point in the history
…e empty string for undefined variables.

As per draft 3 of the URI template spec, made the the 'prefix', 'suffix' and 'list' to return the empty string for undefined variables.
Previously, 'prefix' and 'suffix' returned the arg regardless and 'list' raised an exception.

Added tests.  Ideally the next draft of the spec would include examples equivalent to these.
  • Loading branch information
Michael Burrows authored and Michael Burrows committed Apr 10, 2009
1 parent 178bf50 commit 429e214
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/addressable/uri.rb
Expand Up @@ -461,7 +461,7 @@ def self.expand_prefix_operator(argument, variables, mapping)
value = mapping[variables.first]
if value.kind_of?(Array)
(value.map { |list_value| argument + list_value }).join("")
else
elsif value
argument + value.to_s
end
end
Expand All @@ -483,7 +483,7 @@ def self.expand_suffix_operator(argument, variables, mapping)
value = mapping[variables.first]
if value.kind_of?(Array)
(value.map { |list_value| list_value + argument }).join("")
else
elsif value
value.to_s + argument
end
end
Expand Down Expand Up @@ -526,7 +526,8 @@ def self.expand_list_operator(argument, variables, mapping)
raise InvalidTemplateOperatorError,
"Template operator 'list' takes exactly one variable."
end
mapping[variables.first].join(argument)
values = mapping[variables.first]
values.join(argument) if values
end
class <<self; private :expand_list_operator; end

Expand Down
30 changes: 30 additions & 0 deletions spec/addressable/uri_spec.rb
Expand Up @@ -3697,6 +3697,36 @@ def self.transform(name, value)
end
end

describe Addressable::URI, "when given an empty mapping" do
before do
@mapping = {}
end

it "should result in 'http://example.com/search/'" +
" when used to expand 'http://example.com/search/{-list|+|query}'" do
Addressable::URI.expand_template(
"http://example.com/search/{-list|+|query}",
@mapping).to_str.should ==
"http://example.com/search/"
end

it "should result in 'http://example.com'" +
" when used to expand 'http://example.com{-prefix|/|foo}'" do
Addressable::URI.expand_template(
"http://example.com{-prefix|/|foo}",
@mapping).to_str.should ==
"http://example.com"
end

it "should result in 'http://example.com'" +
" when used to expand 'http://example.com{-suffix|/|foo}'" do
Addressable::URI.expand_template(
"http://example.com{-suffix|/|foo}",
@mapping).to_str.should ==
"http://example.com"
end
end

class SuperString
def initialize(string)
@string = string.to_s
Expand Down

0 comments on commit 429e214

Please sign in to comment.