Class: RocketIO::Router

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

Constant Summary collapse

ROOT_PATH_PIECES =
[EMPTY_STRING].freeze
PATH_SPLITTER =
/\/+/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*controllers) ⇒ Router

Returns a new instance of Router.



9
10
11
12
13
# File 'lib/rocketio/router.rb', line 9

def initialize *controllers
  @controllers = controllers.flatten.compact.uniq
  @routes = routes()
  freeze!
end

Instance Attribute Details

#controllersObject (readonly)

Returns the value of attribute controllers.



7
8
9
# File 'lib/rocketio/router.rb', line 7

def controllers
  @controllers
end

Instance Method Details

#resolve_path(path) ⇒ Object

fully traversing the tree and use the last matched controller



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rocketio/router.rb', line 16

def resolve_path path
  routes = @routes
  pieces = path_pieces(path)
  depth = -1
  controller = nil
  path_params = []

  while routes = routes[pieces[depth += 1]]
    # do not use `next unless controller = routes[0]` here
    # cause this may override earlier found controller.
    next unless routes[0]
    controller = routes[0]
    path_params = pieces[ (depth + 1) .. -1 ]
  end

  return EMPTY_ARRAY unless controller

  method = if path_params.any? && controller.api[path_params[0].to_sym]
    path_params.slice!(0).to_sym
  else
    INDEX_METHOD
  end

  [controller, method, path_params]
end