Class: Expressr::Router
- Inherits:
-
Object
- Object
- Expressr::Router
- Defined in:
- lib/expressr/router.rb
Instance Attribute Summary collapse
-
#server ⇒ Object
Returns the value of attribute server.
Instance Method Summary collapse
- #add_route(proc, options = {}) ⇒ Object
- #delete(path, &block) ⇒ Object
- #get(path, &block) ⇒ Object
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #param(name, &block) ⇒ Object
- #post(path, &block) ⇒ Object
- #put(path, &block) ⇒ Object
- #route(path) ⇒ Object
- #run(request, response) ⇒ Object
- #use(path = nil, &block) ⇒ Object
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
5 6 7 |
# File 'lib/expressr/router.rb', line 5 def initialize @nodes = [] end |
Instance Attribute Details
#server ⇒ Object
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, ={}) if [:path] [:path] = standardize_path([:path]) end callback = RouteItem.new(proc, ) 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 |