class Shigeru::UriTemplate

Handles a subset of [URI Template](tools.ietf.org/html/rfc6570), Level 4

Constants

PATTERN

Public Class Methods

new(template) click to toggle source
# File lib/shigeru.rb, line 23
def initialize(template)
  terms = template.split(PATTERN)
  parts = ["Proc.new do |params, __o|", "params ||= {}", "__o ||= ''"]
  while (term = terms.shift)
    case term
    when '' then parts.push(expansion('', ',', terms.shift.split(',')))
    when '?' then parts.push(expansion('?', '&', terms.shift.split(',')))
    when '/' then parts.push(expansion('/', '/', terms.shift.split(',')))
    else parts << "__o << '#{term}'"
    end
  end
  parts.push("__o", "end")
  @template = self.instance_eval(parts.join("\n"))
end

Public Instance Methods

expand(parameters) click to toggle source
# File lib/shigeru.rb, line 38
def expand(parameters)
  @template[parameters]
end

Private Instance Methods

expansion(sigil, seperator, terms) click to toggle source
# File lib/shigeru.rb, line 44
def expansion(sigil, seperator, terms)
  result = []

  case sigil
  when '?' then result << "__o << '?'"
  when '/' then result << "__o << '/'"
  end

  while (term = terms.shift)
    if term[-1] == '*'
      result << "__o << params[:#{term[0...-1]}].map do |t|"
      result << "  '#{term[0...-1]}=' +" if sigil == '?'
      result << "  URI.escape(t.to_s)"
      result << "end.join('#{seperator}')"
    else
      result << "__o << '#{term}='" if sigil == '?'
      result << "__o << URI.escape(params[:#{term}].to_s)"
    end
    result << "__o << '#{seperator}'" if terms.any?
  end

  result
end