Class: RouterSimple::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/router_simple.rb

Overview

Yet another HTTP Router

Instance Method Summary collapse

Constructor Details

#initializeRouter

Create new instance



30
31
32
# File 'lib/router_simple.rb', line 30

def initialize
    @patterns = []
end

Instance Method Details

#match(http_method, path) ⇒ Object

match the path with this router.

Parameters:

  • http_method (String)

    REQUEST_METHOD

  • path (String)

    PATH_INFO

Returns:

  • dest destination info

  • captured captured parameters

  • method_not_allowed true if method not allowed.



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/router_simple.rb', line 58

def match(http_method, path)
    found_method_not_allowed = false
    @patterns.each do |pattern|
        dest, captured, method_not_allowed = pattern.match(http_method, path)
        if dest
            return dest, captured
        elsif method_not_allowed
            found_method_not_allowed = true
        end
    end
    return nil, nil, found_method_not_allowed
end

#register(http_method, path, dest) ⇒ Object

register a path to router.

You can specify three pattern of path.

  1. Use regexp directly

  2. Use /:name/:id

  3. Use /*path

Parameters:

  • http_method (Araray or String)

    HTTP method. You can specify nil, [‘GET’, ‘HEAD“], ’GET’, etc.

  • path (String or Regexp)
  • dest (Any)

    Destination of this path

Returns:

  • None



46
47
48
# File 'lib/router_simple.rb', line 46

def register(http_method, path, dest)
    @patterns.push(Route.new(http_method, path, dest))
end