class Rack::Way::Router
Attributes
not_found[W]
Public Class Methods
new()
click to toggle source
# File lib/rack-way/router.rb, line 9 def initialize @routes = { 'GET' => [], 'POST' => [], 'DELETE' => [], 'PUT' => [], 'PATCH' => [] } @namespaces = [] @not_found = proc { [404, {}, ['Not found']] } end
Public Instance Methods
add(method, path, endpoint)
click to toggle source
# File lib/rack-way/router.rb, line 37 def add(method, path, endpoint) route = Route.new("/" + @namespaces.join('/') + put_path_slash(path), endpoint) @routes[method.to_s.upcase].push route end
add_not_found(endpoint)
click to toggle source
# File lib/rack-way/router.rb, line 44 def add_not_found(endpoint) @not_found = endpoint end
append_namespace(name)
click to toggle source
# File lib/rack-way/router.rb, line 48 def append_namespace(name) @namespaces.push(name) end
call(env)
click to toggle source
# File lib/rack-way/router.rb, line 24 def call(env) route = match_route(env) request_builder = RequestBuilder.new(env) return render_not_found(request_builder.call) if route.nil? if route.endpoint.respond_to?(:call) return route.endpoint.call(request_builder.call(route)) end route.endpoint.new.call(request_builder.call(route)) end
clear_last_namespace()
click to toggle source
# File lib/rack-way/router.rb, line 52 def clear_last_namespace @namespaces = @namespaces.first(@namespaces.size - 1) end
Private Instance Methods
match_route(env)
click to toggle source
# File lib/rack-way/router.rb, line 71 def match_route(env) @routes[env["REQUEST_METHOD"]] .detect { |route| route.match?(env) } end
put_path_slash(path)
click to toggle source
# File lib/rack-way/router.rb, line 59 def put_path_slash(path) return '' if (path == '/' || path == '') && @namespaces != [] return '/' + path if @namespaces != [] path end
render_not_found(env)
click to toggle source
# File lib/rack-way/router.rb, line 65 def render_not_found(env) return @not_found.call(env) if @not_found.respond_to?(:call) @not_found.new.call(env) end