class Silicon::Routing::Matcher

Public Class Methods

new(routes) click to toggle source
# File lib/silicon/routing/matcher.rb, line 6
def initialize(routes)
  @routes = routes
end

Public Instance Methods

match(path, http_method) click to toggle source
# 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