forked from jerodsanto/rack-noie6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bouncer.rb
53 lines (43 loc) · 1.17 KB
/
bouncer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# encoding: UTF-8
module Rack
class Bouncer
VERSION = "1.2"
def initialize(app, options = {})
@app = app
@options = options
@options[:redirect] ||= "http://browsehappy.com/"
@options[:minimum] ||= 8.0
end
def call(env)
if ie6_found_in?(env) || aol_found_in?(env)
kick_it
else
@app.call(env)
end
end
private
def ie6_found_in?(env)
if env["HTTP_USER_AGENT"]
is_ie?(env["HTTP_USER_AGENT"]) and ie_version(env["HTTP_USER_AGENT"]) < @options[:minimum] and @options[:redirect] != env["PATH_INFO"]
end
end
def is_ie?(ua_string)
# We need at least one digit to be able to get the version, hence the \d
ua_string.match(/MSIE \d/) ? true : false
end
def aol_found_in?(env)
if env["HTTP_USER_AGENT"]
is_aol?(env["HTTP_USER_AGENT"])
end
end
def is_aol?(ua_string)
ua_string.match(/AOL \d/) ? true : false
end
def ie_version(ua_string)
ua_string.match(/MSIE (\S+)/)[1].to_f
end
def kick_it
[302, {"Location" => @options[:redirect], "Content-Type" => "text/html"}, "Browser not supported"]
end
end
end