Class: Navigable::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/navigable/router.rb,
lib/navigable/router/node.rb,
lib/navigable/router/trie.rb,
lib/navigable/router/printer.rb,
lib/navigable/router/version.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 =
/:/.freeze
NOT_FOUND_RESPONSE =
[404, { 'Content-Length' => '9' }, ['Not Found']].freeze
VERSION =
"0.2.0"

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



23
24
25
26
# File 'lib/navigable/router.rb', line 23

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

Instance Method Details

#call(env) ⇒ Object



49
50
51
52
# File 'lib/navigable/router.rb', line 49

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



45
46
47
# File 'lib/navigable/router.rb', line 45

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

#get(path, to:) ⇒ Object



28
29
30
31
# File 'lib/navigable/router.rb', line 28

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

#patch(path, to:) ⇒ Object



41
42
43
# File 'lib/navigable/router.rb', line 41

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

#post(path, to:) ⇒ Object



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

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


54
55
56
# File 'lib/navigable/router.rb', line 54

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

#put(path, to:) ⇒ Object



37
38
39
# File 'lib/navigable/router.rb', line 37

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