class Addressable::Template
def parse_new_template_pattern(pattern, processor = nil)
Experimental RBS support (using type sampling data from the type_fusion
project).
def parse_new_template_pattern: (String pattern, ?nil processor) -> untyped
This signature was generated using 6 samples from 2 applications.
-
(Array, Regexp)
-
Parameters:
-
processor
(#match
) -- The template processor to use. -
pattern
(String
) -- The URI template pattern.
def parse_new_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( EXPRESSION ) do |expansion| expansions << expansion _, operator, varlist = *expansion.match(EXPRESSION) leader = Regexp.escape(LEADERS.fetch(operator, '')) joiner = Regexp.escape(JOINERS.fetch(operator, ',')) combined = varlist.split(',').map do |varspec| _, name, modifier = *varspec.match(VARSPEC) result = processor && processor.respond_to?(:match) ? processor.match(name) : nil if result "(?<#{name}>#{ result })" else group = case operator when '+' "#{ RESERVED }*?" when '#' "#{ RESERVED }*?" when '/' "#{ UNRESERVED }*?" when '.' "#{ UNRESERVED.gsub('\.', '') }*?" when ';' "#{ UNRESERVED }*=?#{ UNRESERVED }*?" when '?' "#{ UNRESERVED }*=#{ UNRESERVED }*?" when '&' "#{ UNRESERVED }*=#{ UNRESERVED }*?" else "#{ UNRESERVED }*?" end if modifier == '*' "(?<#{name}>#{group}(?:#{joiner}?#{group})*)?" else "(?<#{name}>#{group})?" end end end.join("#{joiner}?") "(?:|#{leader}#{combined})" end # Ensure that the regular expression matches the whole URI. regexp_string = "\\A#{regexp_string}\\z" return expansions, Regexp.new(regexp_string) end