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

Modules: Params Classes: Mapper, Parser, Request, Route

Class Method Summary collapse

Class Method Details

.auto_load_path(path = nil) ⇒ Object



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

def auto_load_path(path = nil)
  return @auto_load_path unless path
  @auto_load_path = path
end

.call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fit_api/router.rb', line 9

def call(env)
  method, path = env["REQUEST_METHOD"], env["PATH_INFO"]
  is_root = path == "/"

  if route = find(method, path, !is_root)
    return route.invoke(env)
  end

  status = is_root ? 200 : 404
  res    = is_root ? "fit-api is working!" : "Action not found"

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

.define(&block) ⇒ Object



38
39
40
# File 'lib/fit_api/router.rb', line 38

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

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



23
24
25
26
27
28
29
30
31
# File 'lib/fit_api/router.rb', line 23

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

  if fetch_not_found
    not_found = find("get", "/404", false)
    return not_found if not_found
  end
end

.mapperObject



42
43
44
# File 'lib/fit_api/router.rb', line 42

def mapper
  @mapper ||= Mapper.new
end