class Spine::Routing::Router

Constants

NOT_FOUND_RESPONSE
PARAMETERS
PATH
VERB

Public Instance Methods

add(route) click to toggle source
# File lib/spine/routing/router.rb, line 31
def add(route)
  routes << route
end
build(pattern, options) click to toggle source
# File lib/spine/routing/router.rb, line 18
def build(pattern, options)
  Route.new(
    '',
    normalize_path(pattern),
    options.fetch(:to),
    options.fetch(:verb)
  )
end
call(env) click to toggle source
# File lib/spine/routing/router.rb, line 43
def call(env)
  verb = env[VERB].downcase.gsub('head', 'get').to_sym
  route = recognise(verb, env[PATH])
  if route
    env[PARAMETERS] = Rack::Request.new(env).params
    env[PARAMETERS].merge!(route.parameters)
    run(route.app, env)
  else
    NOT_FOUND_RESPONSE # TODO: set default app
  end
end
configure(&block) click to toggle source
# File lib/spine/routing/router.rb, line 27
def configure(&block)
  instance_eval &block
end
recognise(verb, path) click to toggle source
# File lib/spine/routing/router.rb, line 35
def recognise(verb, path)
  routes.each do |route|
    match = route.match(verb, path)
    return match if match
  end
  nil
end
routes() click to toggle source
# File lib/spine/routing/router.rb, line 14
def routes
  @routes ||= [] # TODO: use set instead of array
end
run(app, env) click to toggle source
# File lib/spine/routing/router.rb, line 55
def run(app, env)
  app.call(env)
end

Protected Instance Methods

normalize_path(path) click to toggle source
# File lib/spine/routing/router.rb, line 61
def normalize_path(path)
  '/' + path.split('/')
    .map(&:strip)
    .select { |part| part && part != '' }
    .join('/')
end