Class: Rack::HttpRouter::Router
- Inherits:
-
Object
- Object
- Rack::HttpRouter::Router
show all
- Defined in:
- lib/rack-http_router/router.rb,
lib/rack-http_router/router/route.rb,
lib/rack-http_router/router/build_request.rb
Defined Under Namespace
Classes: BuildRequest, Route, UndefinedNamedRoute
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(config = {}) ⇒ Router
Returns a new instance of Router.
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/rack-http_router/router.rb', line 12
def initialize(config = {})
@routes = {}
%w[GET POST DELETE PUT TRACE OPTIONS PATCH].each do |method|
@routes[method] = { __instances: [] }
end
@route =
Hash.new do |_hash, key|
raise(UndefinedNamedRoute, "Undefined named route: '#{key}'")
end
@config = config
@scopes = []
@befores = []
@scopes_befores = {}
@error = proc { |_req, e| raise e }
@not_found = proc { [404, {}, ['Not found']] }
end
|
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
10
11
12
|
# File 'lib/rack-http_router/router.rb', line 10
def config
@config
end
|
#not_found=(value) ⇒ Object
Sets the attribute not_found
9
10
11
|
# File 'lib/rack-http_router/router.rb', line 9
def not_found=(value)
@not_found = value
end
|
#route ⇒ Object
Returns the value of attribute route.
10
11
12
|
# File 'lib/rack-http_router/router.rb', line 10
def route
@route
end
|
Instance Method Details
#add(method, path, endpoint, as = nil) ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/rack-http_router/router.rb', line 64
def add(method, path, endpoint, as = nil)
method = :get if method == :head
path_with_scopes = "/#{@scopes.join('/')}#{put_path_slash(path)}"
@route[as] = path_with_scopes if as
route_instance = Route.new(path_with_scopes, endpoint, @befores)
if @scopes.size >= 1
return push_to_scope(method.to_s.upcase, route_instance)
end
@routes[method.to_s.upcase][:__instances].push(route_instance)
end
|
#add_error(endpoint) ⇒ Object
83
84
85
|
# File 'lib/rack-http_router/router.rb', line 83
def add_error(endpoint)
@error = endpoint
end
|
#add_not_found(endpoint) ⇒ Object
79
80
81
|
# File 'lib/rack-http_router/router.rb', line 79
def add_not_found(endpoint)
@not_found = endpoint
end
|
#append_scope(name, befores = []) ⇒ Object
87
88
89
90
91
92
|
# File 'lib/rack-http_router/router.rb', line 87
def append_scope(name, befores = [])
@scopes.push(name)
befores = [befores] unless befores.is_a?(Array)
@befores.concat(befores)
@scopes_befores[name] = befores
end
|
#call(env) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/rack-http_router/router.rb', line 29
def call(env)
request_builder = BuildRequest.new(env)
env['REQUEST_METHOD'] = 'GET' if env['REQUEST_METHOD'] == 'HEAD'
route_instance = match_route(env)
return render_not_found(request_builder.call) if route_instance.nil?
rack_request = request_builder.call(route_instance)
befores = route_instance.befores
before_result = nil
i = 0
while i < befores.size
before_result = call_endpoint(befores[i], rack_request)
return before_result unless before_result.is_a?(Rack::Request)
i += 1
end
call_endpoint(route_instance.endpoint, before_result || rack_request)
rescue Exception => e
@error.call(request_builder.call, e)
end
|
#call_endpoint(endpoint, rack_request) ⇒ Object
54
55
56
57
58
59
60
61
62
|
# File 'lib/rack-http_router/router.rb', line 54
def call_endpoint(endpoint, rack_request)
return endpoint.call(rack_request) if endpoint.respond_to?(:call)
if endpoint.include?(Rack::HttpRouter::Action)
return endpoint.new(route: @route, config: @config).call(rack_request)
end
endpoint.new.call(rack_request)
end
|
#clear_last_scope ⇒ Object
94
95
96
97
|
# File 'lib/rack-http_router/router.rb', line 94
def clear_last_scope
@befores -= @scopes_befores[@scopes.last]
@scopes = @scopes.first(@scopes.size - 1)
end
|