Class: Expressr::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/expressr/router.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



5
6
7
# File 'lib/expressr/router.rb', line 5

def initialize
  @nodes = []
end

Instance Attribute Details

#serverObject

Returns the value of attribute server.



3
4
5
# File 'lib/expressr/router.rb', line 3

def server
  @server
end

Instance Method Details

#add_route(proc, options = {}) ⇒ Object



49
50
51
52
53
54
55
# 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) ⇒ Object



36
37
38
39
# File 'lib/expressr/router.rb', line 36

def delete(path, &block)
  add_route(block, method: 'DELETE', path: path)
  self
end

#get(path, &block) ⇒ Object



21
22
23
24
# File 'lib/expressr/router.rb', line 21

def get(path, &block)
  add_route(block, method: 'GET', path: path)
  self
end

#param(name, &block) ⇒ Object



13
14
15
# File 'lib/expressr/router.rb', line 13

def param(name, &block)
  add_route(block, param: name)
end

#post(path, &block) ⇒ Object



31
32
33
34
# File 'lib/expressr/router.rb', line 31

def post(path, &block)
  add_route(block, method: 'POST', path: path)
  self
end

#put(path, &block) ⇒ Object



26
27
28
29
# File 'lib/expressr/router.rb', line 26

def put(path, &block)
  add_route(block, method: 'PUT', path: path)
  self
end

#route(path) ⇒ Object



17
18
19
# File 'lib/expressr/router.rb', line 17

def route(path)
  Route.new(router: self, path: path)
end

#run(request, response) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/expressr/router.rb', line 41

def run(request, response)
  env = {
    request: request,
    response: response
  }
  emit('request', env)
end

#use(path = nil, &block) ⇒ Object



9
10
11
# File 'lib/expressr/router.rb', line 9

def use(path=nil, &block)
  add_route(block, path: path)
end