Class: Rack::Router::Middleware
- Inherits:
-
Object
- Object
- Rack::Router::Middleware
- Defined in:
- lib/rack/router/middleware.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #after_route {|env, route| ... } ⇒ Object
- #call(env) ⇒ Object
- #define_path(http_method, path, value, options = {}) ⇒ Object
-
#initialize(app, logger: nil, &block) ⇒ Middleware
constructor
A new instance of Middleware.
- #match(path, value, options = {}) ⇒ Object
- #namespace(path, &block) ⇒ Object
- #nested(path, options = {}) ⇒ Object
- #not_found(response) ⇒ Object
- #with_accept(accept, &block) ⇒ Object
- #with_constraint(constraint, &block) ⇒ Object
- #with_content_type(content_type, &block) ⇒ Object
Constructor Details
#initialize(app, logger: nil, &block) ⇒ Middleware
Returns a new instance of Middleware.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/rack/router/middleware.rb', line 8 def initialize(app, logger: nil, &block) @app = app @routes = RouteBuilder.new @current_path = nil = {} @not_found = nil @logger = logger || Logger.new(STDOUT) @callbacks = [] instance_exec(&block) end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
6 7 8 |
# File 'lib/rack/router/middleware.rb', line 6 def logger @logger end |
Instance Method Details
#after_route {|env, route| ... } ⇒ Object
104 105 106 107 108 |
# File 'lib/rack/router/middleware.rb', line 104 def after_route(&block) raise ArgumentError, 'block must be given' unless block_given? @callbacks.push(block) end |
#call(env) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rack/router/middleware.rb', line 19 def call(env) route = @routes.match(env) env[RACK_ROUTER_PATH] = route&.value env[RACK_ROUTER_PATH_HASH] = route&.path_hash env[Rack::PATH_INFO] run_after_route(env, route) if route.nil? && !@not_found.nil? logger.debug { "#{self.class}#call route was not found\n#{@routes.print_routes}" } return render_not_found(env) end @app.call(env) end |
#define_path(http_method, path, value, options = {}) ⇒ Object
89 90 91 92 93 |
# File 'lib/rack/router/middleware.rb', line 89 def define_path(http_method, path, value, = {}) = .dup.merge() path = [@current_path, path].compact.join('/') @routes.add(http_method, path, value, ) end |
#match(path, value, options = {}) ⇒ Object
44 45 46 |
# File 'lib/rack/router/middleware.rb', line 44 def match(path, value, = {}) define_path(nil, path, value, ) end |
#namespace(path, &block) ⇒ Object
64 65 66 |
# File 'lib/rack/router/middleware.rb', line 64 def namespace(path, &block) nested(path, {}, &block) end |
#nested(path, options = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rack/router/middleware.rb', line 52 def nested(path, = {}) old_current_path = @current_path = @current_path = [@current_path, path].compact.join('/') = .dup.merge() yield ensure @current_path = old_current_path = end |
#not_found(response) ⇒ Object
95 96 97 98 99 |
# File 'lib/rack/router/middleware.rb', line 95 def not_found(response) response = default_not_found if response == :default response = nil unless response @not_found = response end |
#with_accept(accept, &block) ⇒ Object
79 80 81 |
# File 'lib/rack/router/middleware.rb', line 79 def with_accept(accept, &block) nested(nil, accept: accept, &block) end |
#with_constraint(constraint, &block) ⇒ Object
69 70 71 |
# File 'lib/rack/router/middleware.rb', line 69 def with_constraint(constraint, &block) nested(nil, constraint: constraint, &block) end |
#with_content_type(content_type, &block) ⇒ Object
74 75 76 |
# File 'lib/rack/router/middleware.rb', line 74 def with_content_type(content_type, &block) nested(nil, content_type: content_type, &block) end |