class Mustermann::Simple
Sinatra 1.3 style pattern implementation.
@example
Mustermann.new('/:foo', type: :simple) === '/bar' # => true
@see Mustermann::Pattern @see file:README.md#simple Syntax description in the README
Public Class Methods
highlighter()
click to toggle source
@!visibility private @return [#highlight, nil]
highlighing logic for mustermann-visualizer, nil if mustermann-visualizer hasn't been loaded
# File lib/mustermann/simple.rb, line 21 def self.highlighter return unless defined? Mustermann::Visualizer::Highlighter @highlighter ||= Mustermann::Visualizer::Highlighter.create do on(/:(\w+)/) { |matched| element(:capture, ':') { element(:name, matched[1..-1]) } } on("*" => :splat, "?" => :optional) end end
Private Instance Methods
compile(greedy: true, uri_decode: true, space_matches_plus: true, **options)
click to toggle source
# File lib/mustermann/simple.rb, line 29 def compile(greedy: true, uri_decode: true, space_matches_plus: true, **options) pattern = @string.gsub(/[^\?\%\\\/\:\*\w]/) { |c| encoded(c, uri_decode, space_matches_plus) } pattern.gsub!(/((:\w+)|\*)/) do |match| match == "*" ? "(?<splat>.*?)" : "(?<#{$2[1..-1]}>[^/?#]+#{?? unless greedy})" end Regexp.new(pattern) rescue SyntaxError, RegexpError => error type = error.message["invalid group name"] ? CompileError : ParseError raise type, error.message, error.backtrace end
encoded(char, uri_decode, space_matches_plus)
click to toggle source
# File lib/mustermann/simple.rb, line 40 def encoded(char, uri_decode, space_matches_plus) return Regexp.escape(char) unless uri_decode parser = URI::Parser.new encoded = Regexp.union(parser.escape(char), parser.escape(char, /./).downcase, parser.escape(char, /./).upcase) encoded = Regexp.union(encoded, encoded('+', true, true)) if space_matches_plus and char == " " encoded end