Class: Rack::SimpleRedirect
- Inherits:
-
Object
- Object
- Rack::SimpleRedirect
- Defined in:
- lib/rack/simple_redirect.rb
Instance Method Summary collapse
- #_check_routes_opts(h) ⇒ Object
- #_matches(path, env, route) ⇒ Object
- #call(env) ⇒ Object
-
#initialize(app, routes: {}, status: 302) ⇒ SimpleRedirect
constructor
The keys in
routes
can be strings, regular expressions, or callables.
Constructor Details
#initialize(app, routes: {}, status: 302) ⇒ SimpleRedirect
The keys in routes
can be strings, regular expressions, or callables. For a key that matches (using == for strings, .match?(path) for regexes, or call(env) for callables), the Location in the redirect is equal to the value of the key, or if the value is a callable, the returned result of calling the value with env.
13 14 15 16 17 |
# File 'lib/rack/simple_redirect.rb', line 13 def initialize(app, routes: {}, status: 302) @app = app @routes = routes @status = status end |
Instance Method Details
#_check_routes_opts(h) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rack/simple_redirect.rb', line 38 def _check_routes_opts(h) h.each do |k, v| case k when String, Regexp, Proc nil else raise "SimpleRedirect routes keys must be strings, regexes, or procs" end case v when String, Proc nil else raise "SimpleRedirect routes values must be strings or procs" end end end |
#_matches(path, env, route) ⇒ Object
32 33 34 35 36 |
# File 'lib/rack/simple_redirect.rb', line 32 def _matches(path, env, route) return route == path if route.is_a?(String) return route.match?(path) if route.is_a?(Regexp) return route.call(env) end |
#call(env) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rack/simple_redirect.rb', line 19 def call(env) path = env["PATH_INFO"] loc = nil @routes.each do |route, result| if self._matches(path, env, route) loc = result.respond_to?(:call) ? result[env] : result break end end return @app.call(env) if loc.nil? return [@status, {"Location" => loc}, []] end |