class Expressr::Router

Attributes

server[RW]

Public Class Methods

new() click to toggle source
# File lib/expressr/router.rb, line 5
def initialize
  @nodes = []
end

Public Instance Methods

add_route(proc, options={}) click to toggle source
# File lib/expressr/router.rb, line 49
def add_route(proc, options={})
  if options[:path]
    options[:path] = standardize_path(options[:path])
  end
  callback = RouteItem.new(proc, options)
  server.add_listener('request', callback)
end
delete(path, &block) click to toggle source
# File lib/expressr/router.rb, line 36
def delete(path, &block)
  add_route(block, method: 'DELETE', path: path)
  self
end
get(path, &block) click to toggle source
# File lib/expressr/router.rb, line 21
def get(path, &block)
  add_route(block, method: 'GET', path: path)
  self
end
param(name, &block) click to toggle source
# File lib/expressr/router.rb, line 13
def param(name, &block)
  add_route(block, param: name)
end
post(path, &block) click to toggle source
# File lib/expressr/router.rb, line 31
def post(path, &block)
  add_route(block, method: 'POST', path: path)
  self
end
put(path, &block) click to toggle source
# File lib/expressr/router.rb, line 26
def put(path, &block)
  add_route(block, method: 'PUT', path: path)
  self
end
route(path) click to toggle source
# File lib/expressr/router.rb, line 17
def route(path)
  Route.new(router: self, path: path)
end
run(request, response) click to toggle source
# File lib/expressr/router.rb, line 41
def run(request, response)
  env = {
    request: request,
    response: response
  }
  emit('request', env)
end
use(path=nil, &block) click to toggle source
# File lib/expressr/router.rb, line 9
def use(path=nil, &block)
  add_route(block, path: path)
end

Protected Instance Methods

standardize_path(path) click to toggle source
# File lib/expressr/router.rb, line 59
def standardize_path(path)
  if path == '*'
    path = nil
  elsif path.is_a?(String) && path.include?('*')
    path = Regexp.new("^#{path.gsub('*', '.*')}$")
  end
  path
end