Simple Syntax for Mustermann
This gem implements the simple
pattern type for Mustermann. It is compatible with Sinatra (1.x), Scalatra and Dancer.
Overview
Supported options:
greedy
, space_matches_plus
, uri_decode
and ignore_unknown_options
.
This is useful for porting an application that relies on this behavior to a later Sinatra version and to make sure Sinatra 2.0 patterns do not decrease performance. Simple patterns internally use the same code older Sinatra versions used for compiling the pattern. Error messages for broken patterns will therefore not be as informative as for other pattern implementations.
require 'mustermann'
pattern = Mustermann.new('/:example', type: :simple)
pattern === "/foo.bar" # => true
pattern === "/foo/bar" # => false
pattern.params("/foo.bar") # => { "example" => "foo.bar" }
pattern.params("/foo/bar") # => nil
pattern = Mustermann.new('/:example/?:optional?', type: :simple)
pattern === "/foo.bar" # => true
pattern === "/foo/bar" # => true
pattern.params("/foo.bar") # => { "example" => "foo.bar", "optional" => nil }
pattern.params("/foo/bar") # => { "example" => "foo", "optional" => "bar" }
pattern = Mustermann.new('/*', type: :simple)
pattern === "/foo.bar" # => true
pattern === "/foo/bar" # => true
pattern.params("/foo.bar") # => { "splat" => ["foo.bar"] }
pattern.params("/foo/bar") # => { "splat" => ["foo/bar"] }
Syntax
Syntax Element | Description |
---|---|
:name | Captures anything but a forward slash in a greedy fashion. Capture is named name. |
* | Captures anything in a non-greedy fashion. Capture is named splat. It is always an array of captures, as you can use * more than once in a pattern. |
x? | Makes x optional. For instance foo? matches foo or fo. |
/ | Matches forward slash. Does not match URI encoded version of forward slash. |
any special character | Matches exactly that character or a URI encoded version of it. |
any other character | Matches exactly that character. |