Rack::UserAgent::Filter

Rack Middleware for filtering by user agent

Use

Let’s say you don’t support IE6 and want to prevent IE6 users from accessing to your Rack app. You’re in luck. Just add this to your Gemfile:

gem "rack-useragent-filter", :require => "rack/useragent/filter"

in config.ru:

use Rack::UserAgent::Filter, [
  ["Internet Explorer", "7.0"]
]

Done! From now on users with IE version lower than 7.0 will get a nice error message saying that their browsers need to be updated… Ok, ok, not so nice message, you can personalize it by passing custom message, as shown below:

use Rack::UserAgent::Filter, [
  ["Internet Explorer", "7.0"]
], :custom_message => "You are using old browser..."

What about not just one sentence message ?

You can specify source url of template

use Rack::UserAgent::Filter, [
  ["Internet Explorer", "7.0"]
], :template => "http://www.example.com/not_supported.html"

url should be valid http||https

- Cool!, what about something more dynamic… like ERB or HAML?

Granted! You’ll have to add to the config which template to use. In environment.rb:

use Rack::UserAgent::Filter, [
  ["Internet Explorer", "7.0"]
], :template => File.dirname(__FILE__) + "/app/views/shared/upgrade.html.erb"

Then you could show the browser version as part of your message:

Sorry but <%= "#{@browser.browser} #{@browser.version}" %> is not supported

- But… what if IE6 user do wants to see how page looks like in his favourite browser?

You can show button “I take full responsibility of using IE6. Let me in!”. User clicks button and with some little help of Javascript cookie named i.e: “deprecated_browser” is saved on user’s disk. Next time user hits application, middleware checks request for inclusion of cookie “deprecated_browser”. If it exists unsupported browsers detection is disabled for this user.

config.middleware.use “Rack::UserAgent::Filter”, [

["Internet Explorer", "7.0"]

], :force_with_cookie => “deprecated_browser”

Authors

Contributors