Class: Liberty::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/liberty/router.rb,
lib/liberty/router/node.rb,
lib/liberty/router/trie.rb,
lib/liberty/router/printer.rb

Defined Under Namespace

Classes: Node, Printer, Trie

Constant Summary collapse

GET =
"GET"
HEAD =
"HEAD"
POST =
"POST"
PUT =
"PUT"
PATCH =
"PATCH"
DELETE =
"DELETE"
REQUEST_METHOD =
"REQUEST_METHOD"
PATH_INFO =
"PATH_INFO"
ROUTER_PARAMS =
"router.params"
DYNAMIC_PREFIX =
/:/
NOT_FOUND_RESPONSE =
[404, {"Content-Length" => "9"}, ["Not Found"]].freeze

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



20
21
22
23
24
# File 'lib/liberty/router.rb', line 20

def initialize
  @apps = {}
  @static = Hash.new { |h, k| h[k] = {} }
  @dynamic = {}
end

Instance Method Details

#call(env) ⇒ Object



47
48
49
50
# File 'lib/liberty/router.rb', line 47

def call(env)
  endpoint = find_endpoint(env[REQUEST_METHOD], env[PATH_INFO]) { |params| env[ROUTER_PARAMS] = params }
  endpoint ? endpoint.call(env) : NOT_FOUND_RESPONSE
end

#delete(path, to:) ⇒ Object



43
44
45
# File 'lib/liberty/router.rb', line 43

def delete(path, to:)
  register_route(DELETE, path, to)
end

#get(path, to:) ⇒ Object



26
27
28
29
# File 'lib/liberty/router.rb', line 26

def get(path, to:)
  register_route(GET, path, to)
  register_route(HEAD, path, to)
end

#patch(path, to:) ⇒ Object



39
40
41
# File 'lib/liberty/router.rb', line 39

def patch(path, to:)
  register_route(PATCH, path, to)
end

#post(path, to:) ⇒ Object



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

def post(path, to:)
  register_route(POST, path, to)
end


52
53
54
# File 'lib/liberty/router.rb', line 52

def print(stdout = $stdout)
  Printer.new(@static, @dynamic, stdout).print
end

#put(path, to:) ⇒ Object



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

def put(path, to:)
  register_route(PUT, path, to)
end