Class: Browser::Middleware
- Inherits:
-
Object
- Object
- Browser::Middleware
- Defined in:
- lib/browser/middleware.rb,
lib/browser/middleware/context.rb,
lib/browser/middleware/context/additions.rb,
lib/browser/middleware/context/url_methods.rb
Defined Under Namespace
Classes: Context
Instance Method Summary collapse
- #call(env) ⇒ Object
- #html?(request) ⇒ Boolean
-
#initialize(app, &block) ⇒ Middleware
constructor
A new instance of Middleware.
- #redirect(path) ⇒ Object
- #resolve_redirection(env, current_path, path) ⇒ Object
- #run_app(env) ⇒ Object
Constructor Details
#initialize(app, &block) ⇒ Middleware
Returns a new instance of Middleware.
5 6 7 8 9 10 |
# File 'lib/browser/middleware.rb', line 5 def initialize(app, &block) raise ArgumentError, "Browser::Middleware requires a block" unless block @app = app @block = block end |
Instance Method Details
#call(env) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/browser/middleware.rb', line 12 def call(env) request = Rack::Request.new(env) # Only apply verification on HTML requests. # This ensures that images, CSS and JavaScript # will be rendered. return run_app(env) unless html?(request) path = catch(:redirected) do Context.new(request).instance_eval(&@block) end # No path, no match. return run_app(env) unless path resolve_redirection(env, request.path, path) end |
#html?(request) ⇒ Boolean
48 49 50 |
# File 'lib/browser/middleware.rb', line 48 def html?(request) request.env["HTTP_ACCEPT"].to_s.include?("text/html") end |
#redirect(path) ⇒ Object
40 41 42 |
# File 'lib/browser/middleware.rb', line 40 def redirect(path) [302, {"Content-Type" => "text/html", "Location" => path}, []] end |
#resolve_redirection(env, current_path, path) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/browser/middleware.rb', line 30 def resolve_redirection(env, current_path, path) uri = URI.parse(path) if uri.path == current_path run_app(env) else redirect(path) end end |
#run_app(env) ⇒ Object
44 45 46 |
# File 'lib/browser/middleware.rb', line 44 def run_app(env) @app.call(env) end |