Class: NavyKit

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

Constant Summary collapse

@@version =
"1.1.0"

Instance Method Summary collapse

Constructor Details

#initializeNavyKit

Returns a new instance of NavyKit.



66
67
68
69
# File 'lib/navykit.rb', line 66

def initialize
  @routes = []
  @fallback = nil
end

Instance Method Details

#get(path, handler = nil, &block) ⇒ Object



102
103
104
# File 'lib/navykit.rb', line 102

def get(path, handler=nil, &block)
  route("GET", path, handler, &block)
end

#notfound(handler = nil, &block) ⇒ Object



89
90
91
# File 'lib/navykit.rb', line 89

def notfound(handler=nil, &block)
  @fallback = block_given? ? block : handler
end

#post(path, handler = nil, &block) ⇒ Object



106
107
108
# File 'lib/navykit.rb', line 106

def post(path, handler=nil, &block)
  route("POST", path, handler, &block)
end

#route(method, path, handler = nil, &block) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/navykit.rb', line 93

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/navykit.rb', line 71

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