Class: Schmobile::Middleware
- Inherits:
-
Object
- Object
- Schmobile::Middleware
- Defined in:
- lib/schmobile/middleware.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ Middleware
constructor
A new instance of Middleware.
-
#redirect?(request) ⇒ Boolean
Returns true if this middleware has been configured with a redirect_to and the requested path is not already below the configured redirect_to.
- #redirect_location(request) ⇒ Object
- #redirect_with(request) ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ Middleware
Returns a new instance of Middleware.
4 5 6 7 |
# File 'lib/schmobile/middleware.rb', line 4 def initialize(app, = {}) @app = app @options = end |
Instance Method Details
#call(env) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/schmobile/middleware.rb', line 9 def call(env) request = Rack::Request.new(env) if request.is_mobile? && redirect?(request) [ 301, { 'Location' => redirect_location(request) }, [] ] else @app.call(env) end end |
#redirect?(request) ⇒ Boolean
Returns true if this middleware has been configured with a redirect_to and the requested path is not already below the configured redirect_to
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/schmobile/middleware.rb', line 21 def redirect?(request) redirecting = true if @options.key?(:redirect_if) redirecting = @options[:redirect_if].call(request) end if @options.key?(:redirect_to) redirecting &&= request.path !~ /^#{@options[:redirect_to]}/ else redirecting = false end redirecting end |
#redirect_location(request) ⇒ Object
37 38 39 |
# File 'lib/schmobile/middleware.rb', line 37 def redirect_location(request) "#{@options[:redirect_to]}#{redirect_with(request)}" end |
#redirect_with(request) ⇒ Object
41 42 43 |
# File 'lib/schmobile/middleware.rb', line 41 def redirect_with(request) build_path(@options[:redirect_with].to_s, request) end |