Class: Angus::Router
- Inherits:
-
Object
- Object
- Angus::Router
- Defined in:
- lib/angus/router.rb,
lib/angus/router/version.rb
Defined Under Namespace
Classes: PathPattern, Route
Constant Summary collapse
- RE_TRAILING_SLASH =
%r{/\Z}
- SUPPORTED_HTTP_METHODS =
[ :get, :post, :put, :delete, :head, :options, :patch, :trace ]
- VERSION =
'0.0.4'
Instance Method Summary collapse
-
#on(m, path, &block) ⇒ Object
It registers block to be yielded at a given HTTP method and path.
-
#route(env) ⇒ Object
Calls the corresponding previously registered block.
Instance Method Details
#on(m, path, &block) ⇒ Object
It registers block to be yielded at a given HTTP method and path.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/angus/router.rb', line 60 def on(m, path, &block) path = path.gsub(RE_TRAILING_SLASH, '') unless SUPPORTED_HTTP_METHODS.include?(m) raise ArgumentError.new("Unsupported HTTP method #{m}") end route = Route.new(m.to_s.upcase, block, compile(path)) if route.params? dynamic_routes << route else static_routes << route end end |
#route(env) ⇒ Object
Calls the corresponding previously registered block.
When calling, the rack environment and the extracted path params are passed to the route block.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/angus/router.rb', line 84 def route(env) request = Rack::Request.new(env) path_info = request.path_info.gsub(RE_TRAILING_SLASH, '').squeeze('/') matched_route = match_route(env['REQUEST_METHOD'], path_info) match, route_block, path_param_names = matched_route unless match raise NotImplementedError, "No route found #{request.path_info}" end path_params = extract_params(match, path_param_names) whole_params = request.params.clone.merge!(path_params) whole_params = symbolize(whole_params) route_block.call(env, whole_params) end |