Class: NavyKit
- Inherits:
-
Object
- Object
- NavyKit
- Defined in:
- lib/navykit.rb
Constant Summary collapse
- @@version =
"1.0.2"
Instance Method Summary collapse
- #get(path, handler = nil, &block) ⇒ Object
-
#initialize ⇒ NavyKit
constructor
A new instance of NavyKit.
- #notfound(handler = nil, &block) ⇒ Object
- #post(path, handler = nil, &block) ⇒ Object
- #route(method, path, handler = nil, &block) ⇒ Object
- #use(req, res) ⇒ Object
Constructor Details
#initialize ⇒ NavyKit
Returns a new instance of NavyKit.
65 66 67 68 |
# File 'lib/navykit.rb', line 65 def initialize @routes = [] @fallback = nil end |
Instance Method Details
#get(path, handler = nil, &block) ⇒ Object
101 102 103 |
# File 'lib/navykit.rb', line 101 def get(path, handler=nil, &block) route("GET", path, handler, &block) end |
#notfound(handler = nil, &block) ⇒ Object
88 89 90 |
# File 'lib/navykit.rb', line 88 def notfound(handler=nil, &block) @fallback = block_given? ? block : handler end |
#post(path, handler = nil, &block) ⇒ Object
105 106 107 |
# File 'lib/navykit.rb', line 105 def post(path, handler=nil, &block) route("POST", path, handler, &block) end |
#route(method, path, handler = nil, &block) ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/navykit.rb', line 92 def route(method, path, handler=nil, &block) segments = parse_path(path) @routes << { path: segments, method: method, handler: block_given? ? block : handler } end |
#use(req, res) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/navykit.rb', line 70 def use(req, res) path = req.path method = req.request_method response = Response.new(res) _route = find_route(method, path) if _route != nil rpath = _route[:path] rhandler = _route[:handler] params = get_params(path, rpath) rhandler.call(Request.new(req, params), response) elsif @fallback != nil @fallback.call(Request.new(req), response) else native_fallback(req, res) end end |