Class: UriTemplate

Inherits:
Object
  • Object
show all
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

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 expand(params)
  output = ""
  @template.each do |part|
    case part
    when String then output << part
    when Hash   then output << Expression.new(part).expand(params)
    end
  end

  output
end