Class: Silicon::Routing::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/silicon/routing/matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(routes) ⇒ Matcher

Returns a new instance of Matcher.



6
7
8
# File 'lib/silicon/routing/matcher.rb', line 6

def initialize(routes)
  @routes = routes
end

Instance Method Details

#match(path, http_method) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/silicon/routing/matcher.rb', line 10

def match(path, http_method)
  segments = path.split('/').concat(['/'])
  params = {}
  matched = nil

  @routes.each do |route|
    if route.segments.length == segments.length
      for i in 0..segments.length - 1 do
        template = route.segments[i]
        value = segments[i]

        if value == template
        elsif template.index('$') == 0
          name = template.sub('$', '')
          params[name] = value
        else
          break
        end

        if i == segments.length - 1 && route.http_verb == http_method
          matched = route
        end
      end
    end
  end

  matched.nil? ? nil : Match.new(matched, params)
end