Method: Addressable::Template#expand
- Defined in:
- lib/addressable/template.rb
#expand(mapping, processor = nil) ⇒ Addressable::URI
Expands a URI template into a full URI.
The object should respond to either the validate or transform messages or both. Both the validate and transform methods should take two parameters: name and value. The validate method should return true or false; true if the value of the variable is valid, false otherwise. An InvalidTemplateValueError exception will be raised if the value is invalid. The transform method should return the transformed variable value as a String. If a transform method is used, the value will not be percent encoded automatically. Unicode normalization will be performed both before and after sending the value to the transform method.
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 |
# File 'lib/addressable/template.rb', line 456 def (mapping, processor=nil) result = self.pattern.dup transformed_mapping = transform_mapping(mapping, processor) result.gsub!( /#{OPERATOR_EXPANSION}|#{VARIABLE_EXPANSION}/ ) do |capture| if capture =~ OPERATOR_EXPANSION operator, argument, variables, default_mapping = parse_template_expansion(capture, transformed_mapping) = "expand_#{operator}_operator" if ([, .to_sym] & private_methods).empty? raise InvalidTemplateOperatorError, "Invalid template operator: #{operator}" else send(.to_sym, argument, variables, default_mapping) end else varname, _, vardefault = capture.scan(/^\{(.+?)(=(.*))?\}$/)[0] transformed_mapping[varname] || vardefault end end return Addressable::URI.parse(result) end |