Class: Rack::Routing::Router

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

Constant Summary collapse

VALID_HTTP_METHODS =
[ :GET, :POST, :PUT, :DELETE, :OPTIONS, :HEAD ]

Class Method Summary collapse

Class Method Details

.for(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rack/router.rb', line 7

def for env
  http_method = env[ 'REQUEST_METHOD' ].to_sym

  raise 'Invalid HTTP method' unless VALID_HTTP_METHODS.include?( http_method )

  parts = [ http_method ] + env[ 'PATH_INFO' ].split( '/' ).reject{| p | String.blank?( p )}

  matched_route = ROUTES.find do |route|
    route.match?( parts )
  end
      
  return { method: :not_found, params:{} } unless matched_route

  params = matched_route.params_for( parts )
      
  { method: matched_route.routing_method,
    params:params  }
end

.load_routesObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/rack/router.rb', line 26

def load_routes
  lines = ::File.read( ROUTES_FILE )
              .split( "\n"      )
              .reject{| l | String.blank?( l )}
              .reject{| l | l.match /\A\s*#/ }

  lines.map do |line|
    Route.new( line )
  end
end