Class: Rackr::Router
- Inherits:
-
Object
- Object
- Rackr::Router
- Defined in:
- lib/rackr/router.rb,
lib/rackr/router/route.rb,
lib/rackr/router/errors.rb,
lib/rackr/router/build_request.rb
Defined Under Namespace
Modules: Errors Classes: BuildRequest, Route
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#not_found ⇒ Object
writeonly
Sets the attribute not_found.
-
#route ⇒ Object
readonly
Returns the value of attribute route.
Instance Method Summary collapse
- #add(method, path, endpoint, as: nil, route_befores: [], route_afters: []) ⇒ Object
- #add_error(endpoint) ⇒ Object
- #add_not_found(endpoint) ⇒ Object
- #append_branch(name, branch_befores: [], branch_afters: [], as: nil) ⇒ Object
- #call(env) ⇒ Object
- #clear_last_branch ⇒ Object
-
#initialize(config = {}, before: [], after: []) ⇒ Router
constructor
A new instance of Router.
Constructor Details
#initialize(config = {}, before: [], after: []) ⇒ Router
Returns a new instance of Router.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rackr/router.rb', line 12 def initialize(config = {}, before: [], after: []) @routes = {} %w[GET POST DELETE PUT TRACE OPTIONS PATCH].each do |method| @routes[method] = { __instances: [] } end @route = Hash.new do |_hash, key| raise(Errors::UndefinedNamedRouteError, "Undefined named route: '#{key}'") end @config = config @branches = [] @befores = ensure_array(before) @branches_befores = {} @afters = ensure_array(after) @branches_afters = {} @nameds_as = [] @branches_named_as = {} @error = proc { |_req, e| raise e } @not_found = proc { [404, {}, ['Not found']] } end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
10 11 12 |
# File 'lib/rackr/router.rb', line 10 def config @config end |
#not_found=(value) ⇒ Object (writeonly)
Sets the attribute not_found
9 10 11 |
# File 'lib/rackr/router.rb', line 9 def not_found=(value) @not_found = value end |
#route ⇒ Object (readonly)
Returns the value of attribute route.
10 11 12 |
# File 'lib/rackr/router.rb', line 10 def route @route end |
Instance Method Details
#add(method, path, endpoint, as: nil, route_befores: [], route_afters: []) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rackr/router.rb', line 71 def add(method, path, endpoint, as: nil, route_befores: [], route_afters: []) Errors.check_path(path) Errors.check_endpoint(endpoint, path) Errors.check_as(as, path) Errors.check_callbacks(route_befores, path) Errors.check_callbacks(route_afters, path) method = :get if method == :head path_with_branches = "/#{@branches.join('/')}#{put_path_slash(path)}" add_named_route(path_with_branches, as) route_instance = Route.new( path_with_branches, endpoint, befores: @befores + ensure_array(route_befores), afters: @afters + ensure_array(route_afters) ) return push_to_branch(method.to_s.upcase, route_instance) if @branches.size >= 1 @routes[method.to_s.upcase][:__instances].push(route_instance) end |
#add_error(endpoint) ⇒ Object
102 103 104 105 106 |
# File 'lib/rackr/router.rb', line 102 def add_error(endpoint) Errors.check_endpoint(endpoint, 'error') @error = endpoint end |
#add_not_found(endpoint) ⇒ Object
96 97 98 99 100 |
# File 'lib/rackr/router.rb', line 96 def add_not_found(endpoint) Errors.check_endpoint(endpoint, 'not_found') @not_found = endpoint end |
#append_branch(name, branch_befores: [], branch_afters: [], as: nil) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/rackr/router.rb', line 108 def append_branch(name, branch_befores: [], branch_afters: [], as: nil) Errors.check_branch_name(name) Errors.check_as(as, @branches.join('/')) Errors.check_callbacks(branch_befores, name) Errors.check_callbacks(branch_afters, name) @branches.push(name) branch_befores = ensure_array(branch_befores) @befores.concat(branch_befores) @branches_befores[name] = branch_befores branch_afters = ensure_array(branch_afters) @afters.concat(branch_afters) @branches_afters[name] = branch_afters @nameds_as.push(as) @branches_named_as[name] = as end |
#call(env) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rackr/router.rb', line 33 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) rack_request = before_result i += 1 end endpoint_result = call_endpoint(route_instance.endpoint, before_result || rack_request) afters = route_instance.afters i = 0 while i < afters.size call_endpoint(afters[i], endpoint_result) i += 1 end endpoint_result rescue Rackr::NotFound render_not_found(request_builder.call) rescue Exception => e @error.call(request_builder.call, e) end |
#clear_last_branch ⇒ Object
128 129 130 131 132 133 |
# File 'lib/rackr/router.rb', line 128 def clear_last_branch @befores -= @branches_befores[@branches.last] @afters -= @branches_afters[@branches.last] @nameds_as -= [@branches_named_as[@branches.last]] @branches = @branches.first(@branches.size - 1) end |