class SoarSc::Rack::Router

Constants

EMPTY_STRING
VERSION

Public Class Methods

new(app, routes = nil, &block) click to toggle source
# File lib/soar_sc/rack/router.rb, line 17
def initialize(app, routes = nil, &block)
  @app = app
  @routes = []
  if !routes.nil?
    raise NotImplementedError, "Route map syntax obsoleted (use builder syntax)"
  end
  instance_eval(&block) if block_given?
end

Public Instance Methods

call(env) click to toggle source
# File lib/soar_sc/rack/router.rb, line 26
def call(env)
  if route = @routes.detect { |r| r.matches?(env) }
    update_path_parameters!(env, route.extract_path_parameters(env))
    route.action.call(env)
  else
    @app.call(env)
  end
end

Private Instance Methods

add_route(route) click to toggle source

Called by BuilderSyntax methods

# File lib/soar_sc/rack/router.rb, line 38
def add_route(route)
  if include?(route)
    raise DuplicateRouteError.for_route(route)
  else
    @routes << route
  end
end
env_for_route(route) click to toggle source

Creates a minimal env that would be matched by the given route.

# File lib/soar_sc/rack/router.rb, line 53
def env_for_route(route)
  {
    Route::REQUEST_METHOD => (route.method == HttpMethod::ANY ? HttpMethod::GET : route.method),
    Route::SCRIPT_NAME => route.path,
    Route::PATH_INFO => EMPTY_STRING
  }
end
include?(route) click to toggle source
# File lib/soar_sc/rack/router.rb, line 46
def include?(route)
  @routes.detect do |other|
    route.matches?(env_for_route(other)) or other.matches?(env_for_route(route))
  end
end
update_path_parameters!(env, params) click to toggle source
# File lib/soar_sc/rack/router.rb, line 61
def update_path_parameters!(env, params)
  unless params.empty?
    req = ::Rack::Request.new(env)
    params.each { |p, v| req.update_param(p, v) }
  end
end