Sha256: 4f0031a13adf733af5296bd5c79fb02b58135acf56a18c426c2afef95c8e4b00
Contents?: true
Size: 1.25 KB
Versions: 2
Compression:
Stored size: 1.25 KB
Contents
require "uri" class Browser class Middleware # Detect the most common assets. ASSETS_REGEX = %r[\.(css|png|jpe?g|gif|js|svg|ico|flv|mov|m4v|ogg|swf)\z]i def initialize(app, &block) raise ArgumentError, "Browser::Middleware requires a block" unless block @app = app @block = block end 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 def resolve_redirection(env, current_path, path) uri = URI.parse(path) if uri.path == current_path run_app(env) else redirect(path) end end def redirect(path) [302, {"Content-Type" => "text/html", "Location" => path}, []] end def run_app(env) @app.call(env) end def html?(request) return if request.path.match(ASSETS_REGEX) request.env["HTTP_ACCEPT"].to_s.include?("text/html") end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
browser-0.4.0 | lib/browser/middleware.rb |
browser-0.3.2 | lib/browser/middleware.rb |