Class: Wurk::Web::Extension::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/wurk/web/extension.rb

Overview

Compiles a Sinatra-style path ("/locks/:digest") into a matcher that extracts Symbol-keyed route params. A trailing "*" matches the rest.

Constant Summary collapse

NAMED =
%r{/([^/]*):([^./:$]+)}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Route

Returns a new instance of Route.



94
95
96
97
98
# File 'lib/wurk/web/extension.rb', line 94

def initialize(path)
  @path = path.to_s
  @keys = []
  @regex = compile(@path)
end

Instance Attribute Details

#path ⇒ Object (readonly)

Returns the value of attribute path.



92
93
94
# File 'lib/wurk/web/extension.rb', line 92

def path
  @path
end

Instance Method Details

#match(path) ⇒ Object

Returns a {key => value} Hash of route params, or nil if no match.



101
102
103
104
105
106
# File 'lib/wurk/web/extension.rb', line 101

def match(path)
  m = @regex.match(path)
  return nil unless m

  @keys.each_with_index.to_h { |k, i| [k, m[i + 1] && CGI.unescape(m[i + 1])] }
end