class Browser::Middleware
Constants
- ACCEPT_REGEX
Detect the ACCEPT header. IE8 send /.
- ASSETS_REGEX
Detect the most common assets.
Public Class Methods
new(app, &block)
click to toggle source
# File lib/browser/middleware.rb, line 13 def initialize(app, &block) raise ArgumentError, "Browser::Middleware requires a block" unless block @app = app @block = block end
Public Instance Methods
assets?(request)
click to toggle source
# File lib/browser/middleware.rb, line 64 def assets?(request) request.path.match(ASSETS_REGEX) end
call(env)
click to toggle source
# File lib/browser/middleware.rb, line 20 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 process?(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)
click to toggle source
# File lib/browser/middleware.rb, line 60 def html?(request) request.env["HTTP_ACCEPT"].to_s.match(ACCEPT_REGEX) end
process?(request)
click to toggle source
# File lib/browser/middleware.rb, line 56 def process?(request) html?(request) && !assets?(request) end
redirect(path)
click to toggle source
# File lib/browser/middleware.rb, line 48 def redirect(path) [302, {"Content-Type" => "text/html", "Location" => path}, []] end
resolve_redirection(env, current_path, path)
click to toggle source
# File lib/browser/middleware.rb, line 38 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)
click to toggle source
# File lib/browser/middleware.rb, line 52 def run_app(env) @app.call(env) end