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,
lib/rackr/router/errors/dev_html.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.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
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_scope(name, scope_befores: [], scope_afters: []) ⇒ Object
- #call(env) ⇒ Object
- #clear_last_scope ⇒ 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 32 33 34 35 |
# File 'lib/rackr/router.rb', line 12 def initialize(config = {}, before: [], after: []) @instance_routes = {} %w[GET POST DELETE PUT TRACE OPTIONS PATCH].each do |method| @instance_routes[method] = { __instances: [] } end http_methods = HTTP_METHODS.map { |m| m.downcase.to_sym } @routes = Struct.new(*http_methods).new http_methods.each do |method| @routes.send("#{method}=", Hash.new do |_hash, key| raise(Errors::UndefinedNamedRouteError, "Undefined named route: '#{key}'") end) end @dev_mode = ENV['RACK_ENV'] == 'development' @config = config @scopes = [] @befores = ensure_array(before) @scopes_befores = {} @afters = ensure_array(after) @scopes_afters = {} @error = proc { |_req, e| raise e } @not_found = proc { [404, {}, ['Not found']] } @splitted_request_path_info = [] @current_request_path_info = nil 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 |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
10 11 12 |
# File 'lib/rackr/router.rb', line 10 def routes @routes end |
Instance Method Details
#add(method, path, endpoint, as: nil, route_befores: [], route_afters: []) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rackr/router.rb', line 82 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 wildcard = path == '*' path = path.is_a?(Symbol) ? path.inspect : path.sub(%r{\A/}, '') path_with_scopes = "/#{not_empty_scopes.join('/')}#{put_path_slash(path)}" add_named_route(method, path_with_scopes, as) route_instance = Route.new( path_with_scopes, endpoint, befores: @befores + ensure_array(route_befores), afters: @afters + ensure_array(route_afters), wildcard: wildcard ) return push_to_scope(method.to_s.upcase, route_instance) if @scopes.size >= 1 @instance_routes[method.to_s.upcase][:__instances].push(route_instance) end |
#add_error(endpoint) ⇒ Object
116 117 118 119 120 |
# File 'lib/rackr/router.rb', line 116 def add_error(endpoint) Errors.check_endpoint(endpoint, 'error') @error = endpoint end |
#add_not_found(endpoint) ⇒ Object
110 111 112 113 114 |
# File 'lib/rackr/router.rb', line 110 def add_not_found(endpoint) Errors.check_endpoint(endpoint, 'not_found') @not_found = endpoint end |
#append_scope(name, scope_befores: [], scope_afters: []) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/rackr/router.rb', line 122 def append_scope(name, scope_befores: [], scope_afters: []) Errors.check_scope_name(name) Errors.check_scope_slashes(name) Errors.check_callbacks(scope_befores, name) Errors.check_callbacks(scope_afters, name) name = ":#{name}" if name.is_a? Symbol @scopes.push(name) scope_befores = ensure_array(scope_befores) @befores.concat(scope_befores) @scopes_befores[name] = scope_befores scope_afters = ensure_array(scope_afters) @afters.concat(scope_afters) @scopes_afters[name] = scope_afters end |
#call(env) ⇒ Object
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 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rackr/router.rb', line 37 def call(env) path_info = env['PATH_INFO'] @splitted_request_path_info = path_info.split('/') @current_request_path_info = path_info == '/' ? path_info : path_info.chomp('/') # remove trailing "/" request_builder = BuildRequest.new(env, @splitted_request_path_info) env['REQUEST_METHOD'] = 'GET' if env['REQUEST_METHOD'] == 'HEAD' route_instance = match_route(env['REQUEST_METHOD']) return call_endpoint(@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 call_endpoint(@not_found, request_builder.call) rescue Exception => e return @error.call(request_builder.call, e) if !@dev_mode || ENV['RACKR_ERROR_DEV'] call_endpoint(Errors::DevHtml, env.merge({ 'error' => e })) end |
#clear_last_scope ⇒ Object
141 142 143 144 145 |
# File 'lib/rackr/router.rb', line 141 def clear_last_scope @befores -= @scopes_befores[@scopes.last] @afters -= @scopes_afters[@scopes.last] @scopes = @scopes.first(@scopes.size - 1) end |