class Addressable::Template

def parse_template_pattern(pattern, processor=nil)

Returns:
  • (Regexp) -

Parameters:
  • processor (#match) -- The template processor to use.
  • pattern (String) -- The URI template pattern.
def parse_template_pattern(pattern, processor=nil)
  # Escape the pattern. The two gsubs restore the escaped curly braces
  # back to their original form. Basically, escape everything that isn't
  # within an expansion.
  escaped_pattern = Regexp.escape(
    pattern
  ).gsub(/\\\{(.*?)\\\}/) do |escaped|
    escaped.gsub(/\\(.)/, "\\1")
  end
  expansions = []
  # Create a regular expression that captures the values of the
  # variables in the URI.
  regexp_string = escaped_pattern.gsub(
    /#{OPERATOR_EXPANSION}|#{VARIABLE_EXPANSION}/
  ) do |expansion|
    expansions << expansion
    if expansion =~ OPERATOR_EXPANSION
      capture_group = "(.*)"
      operator, argument, names, _ =
        parse_template_expansion(expansion)
      if processor != nil && processor.respond_to?(:match)
        # We can only lookup the match values for single variable
        # operator expansions. Besides, ".*" is usually the only
        # reasonable value for multivariate operators anyways.
        if ["prefix", "suffix", "list"].include?(operator)
          capture_group = "(#{processor.match(names.first)})"
        end
      elsif operator == "prefix"
        capture_group = "(#{Regexp.escape(argument)}.*?)"
      elsif operator == "suffix"
        capture_group = "(.*?#{Regexp.escape(argument)})"
      end
      capture_group
    else
      capture_group = "(.*?)"
      if processor != nil && processor.respond_to?(:match)
        name = expansion[/\{([^\}=]+)(=[^\}]+)?\}/, 1]
        capture_group = "(#{processor.match(name)})"
      end
      capture_group
    end
  end
  # Ensure that the regular expression matches the whole URI.
  regexp_string = "^#{regexp_string}$"
  return expansions, Regexp.new(regexp_string)
end