Module: FitApi::Router
- Defined in:
- lib/fit_api/router.rb,
lib/fit_api/router/route.rb,
lib/fit_api/router/mapper.rb,
lib/fit_api/router/params.rb,
lib/fit_api/router/parser.rb
Defined Under Namespace
Classes: Mapper, Params, Parser, Request, Route
Class Method Summary
collapse
Class Method Details
.call(env) ⇒ Object
5
6
7
8
9
10
11
12
13
14
|
# File 'lib/fit_api/router.rb', line 5
def self.call(env)
method, path = env['REQUEST_METHOD'], env['PATH_INFO']
route = find method, path, path != '/'
return route.invoke(env) if route
res = path == '/' ? { message: 'fit-api is working!' } : { error: 'action not found' }
[ res[:error] ? 404 : 200, { 'Content-Type' => 'application/json'}, [ res.to_json ] ]
end
|
.define(&block) ⇒ Object
28
29
30
|
# File 'lib/fit_api/router.rb', line 28
def self.define(&block)
mapper.instance_eval &block
end
|
.find(method, path, find_error = true) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/fit_api/router.rb', line 16
def self.find(method, path, find_error = true)
routes = mapper.routes[method.downcase]
route = routes.find { |route| route.match? path }
return route if route
if find_error
not_found = find('get', '/404', false)
return not_found if not_found
end
end
|
.mapper ⇒ Object
32
33
34
|
# File 'lib/fit_api/router.rb', line 32
def self.mapper
@mapper ||= Mapper.new
end
|