Class: UriTemplate
- Inherits:
-
Object
- Object
- UriTemplate
- Defined in:
- lib/uri_template.rb,
lib/uri_template/parser.rb,
lib/uri_template/version.rb,
lib/uri_template/transformer.rb
Defined Under Namespace
Classes: Expression, ParseError, Parser, Transformer, Var
Constant Summary collapse
- VERSION =
"1.0.0"
Instance Method Summary collapse
- #expand(params) ⇒ Object
-
#initialize(uri_template) ⇒ UriTemplate
constructor
A new instance of UriTemplate.
Constructor Details
#initialize(uri_template) ⇒ UriTemplate
Returns a new instance of UriTemplate.
11 12 13 14 15 16 17 |
# File 'lib/uri_template.rb', line 11 def initialize(uri_template) @uri_template = uri_template tree = Parser.new.parse(@uri_template) @template = Transformer.new.apply(tree) rescue Parslet::ParseFailed => ex raise ParseError, "invalid template: #{uri_template}" end |
Instance Method Details
#expand(params) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/uri_template.rb', line 19 def (params) output = "" @template.each do |part| case part when String then output << part when Hash then output << Expression.new(part).(params) end end output end |