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
15
16
17
# File 'lib/fit_api/router.rb', line 5

def self.call(env)
  method, path = env['REQUEST_METHOD'], env['PATH_INFO']
  is_root = path == '/'

  if route = Router.find(method, path, !is_root)
    route.invoke(env)
  else
    status = is_root ? 200 : 404
    res    = is_root ? 'fit-api is working!' : 'Action not found'

    [ status, { 'Content-Type' => 'application/json'}, [ res.to_json ] ]
  end
end

.define(&block) ⇒ Object



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

def self.define(&block)
  mapper.instance_eval &block
end

.find(method, path, find_not_found_action = true) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fit_api/router.rb', line 19

def self.find(method, path, find_not_found_action = true)
  routes = mapper.routes[method.downcase]
  route = routes.find { |route| route.match? path }

  return route if route

  if find_not_found_action
    not_found = find('get', '/404', false)
    return not_found if not_found
  end
end

.mapperObject



35
36
37
# File 'lib/fit_api/router.rb', line 35

def self.mapper
  @mapper ||= Mapper.new
end