juliocesar-rack-noie 1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.markdown ADDED
@@ -0,0 +1,31 @@
1
+ # rack-noie
2
+
3
+ rack-noie is the coolest rack middleware ever created. And it is so because it does _everyone_
4
+ a favor: it shows the way out of your website to poor souls out there using Internet Explorer 6.
5
+
6
+ # usage
7
+
8
+ just
9
+
10
+ require 'noie' # or 'juliocesar-noie' when i gemify it
11
+
12
+ and
13
+
14
+ use NoIE, :redirect => '/noieplease.html'
15
+
16
+ the above will redirect to a page noieplease.html in your website. You can redirect to
17
+ a URL as well, like so
18
+
19
+ use NoIE, :redirect => 'http://slashdot.org'
20
+
21
+ or let the default kick in
22
+
23
+ use NoIE
24
+
25
+ # disclaimer
26
+
27
+ I'm a nice guy. I'm so nice that the default URL points to Microsoft's IE8 upgrade page.
28
+
29
+ # license
30
+
31
+ MIT, as usual.
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require 'rake/testtask'
2
+
3
+ task :default => [:test]
4
+
5
+ Rake::TestTask.new do |task|
6
+ task.pattern = 'test/noie_test.rb'
7
+ task.warning, task.verbose = true, true
8
+ end
9
+
10
+ begin
11
+ require 'jeweler'
12
+ Jeweler::Tasks.new do |gemspec|
13
+ gemspec.name = "rack-noie"
14
+ gemspec.summary = "A Rack middleware to redirect IE users out of your website"
15
+ gemspec.email = "julioody@gmail.com"
16
+ gemspec.homepage = "http://github.com/juliocesar/rack-noie"
17
+ gemspec.authors = ["Julio Cesar Ody"]
18
+ end
19
+ rescue LoadError
20
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
21
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 0
3
+ :major: 1
4
+ :minor: 0
data/lib/noie.rb ADDED
@@ -0,0 +1,34 @@
1
+ module Rack
2
+ class NoIE
3
+ def initialize(app, options = {})
4
+ @app = app
5
+ @options = options
6
+ @options[:redirect] ||= 'http://www.microsoft.com/windows/internet-explorer/default.aspx'
7
+ @options[:minimum] ||= 7.0
8
+ end
9
+
10
+ def call(env)
11
+ ie_found_in?(env) ? kick_it : @app.call(env)
12
+ end
13
+
14
+ private
15
+ def ie_found_in?(env)
16
+ if env['HTTP_USER_AGENT']
17
+ is_ie?(env['HTTP_USER_AGENT']) and ie_version(env['HTTP_USER_AGENT']) < @options[:minimum] and @options[:redirect] != env['PATH_INFO']
18
+ end
19
+ end
20
+
21
+ def is_ie?(ua_string)
22
+ # We need at least one digit to be able to get the version, hence the \d
23
+ ua_string.match(/MSIE \d/) ? true : false
24
+ end
25
+
26
+ def ie_version(ua_string)
27
+ ua_string.match(/MSIE (\S+)/)[1].to_f
28
+ end
29
+
30
+ def kick_it
31
+ [301, {'Location' => @options[:redirect]}, 'Fail browser is fail']
32
+ end
33
+ end
34
+ end
data/test/noie_test.rb ADDED
@@ -0,0 +1,60 @@
1
+ require 'test/unit'
2
+
3
+ require 'rubygems'
4
+ require 'rack/mock'
5
+
6
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'noie')
7
+
8
+ class TestApp
9
+ def call(env)
10
+ [200, {}, 'Hi Internets!']
11
+ end
12
+ end
13
+
14
+ class NoieTest < Test::Unit::TestCase
15
+
16
+ def test_redirects_to_where_it_should_if_ie
17
+ request = Rack::MockRequest.new(Rack::NoIE.new(TestApp.new, {:redirect => 'http://slashdot.org'}))
18
+ response = request.get('/', {'HTTP_USER_AGENT' => 'MSIE 6.0' })
19
+ assert_equal 301, response.status
20
+ assert_equal response.location, 'http://slashdot.org'
21
+ end
22
+
23
+ def test_redirects_to_where_it_should_if_user_specified_minimum_not_met
24
+ request = Rack::MockRequest.new(Rack::NoIE.new(TestApp.new, {:redirect => 'http://slashdot.org', :minimum => 6.0}))
25
+ response = request.get('/', {'HTTP_USER_AGENT' => 'Mozilla/4.0 (compatible; MSIE 5.5b1; Mac_PowerPC)' })
26
+ assert_equal 301, response.status
27
+ assert_equal response.location, 'http://slashdot.org'
28
+ end
29
+
30
+ def test_redirects_to_local_urls
31
+ request = Rack::MockRequest.new(Rack::NoIE.new(TestApp.new, {:redirect => '/foo'}))
32
+ response = request.get('/foo', {'HTTP_USER_AGENT' => 'MSIE 6.0' })
33
+ assert_equal "Hi Internets!", response.body
34
+ end
35
+
36
+ def test_allows_if_not_ie
37
+ request = Rack::MockRequest.new(Rack::NoIE.new(TestApp.new, {:redirect => 'http://slashdot.org'}))
38
+ response = request.get('/', {'HTTP_USER_AGENT' => 'Mozilla/5.0'})
39
+ assert_equal "Hi Internets!", response.body
40
+ end
41
+
42
+ def test_allows_if_UA_version_greater_than_minimum
43
+ request = Rack::MockRequest.new(Rack::NoIE.new(TestApp.new, {:redirect => 'http://slashdot.org'}))
44
+ response = request.get('/', {'HTTP_USER_AGENT' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows XP)'})
45
+ assert_equal "Hi Internets!", response.body
46
+ end
47
+
48
+ def test_allows_if_no_UA_version_no_available
49
+ request = Rack::MockRequest.new(Rack::NoIE.new(TestApp.new, {:redirect => 'http://slashdot.org'}))
50
+ response = request.get('/', {'HTTP_USER_AGENT' => 'Mozilla/4.0 (compatible; MSIE l4me; Windows XP)'})
51
+ assert_equal "Hi Internets!", response.body
52
+ end
53
+
54
+ def test_allows_if_no_user_agent_specified
55
+ request = Rack::MockRequest.new(Rack::NoIE.new(TestApp.new, {:redirect => 'http://slashdot.org'}))
56
+ response = request.get('/')
57
+ assert_equal "Hi Internets!", response.body
58
+ end
59
+
60
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: juliocesar-rack-noie
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
5
+ platform: ruby
6
+ authors:
7
+ - Julio Cesar Ody
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-11 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: julioody@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.markdown
24
+ files:
25
+ - README.markdown
26
+ - Rakefile
27
+ - VERSION.yml
28
+ - lib/noie.rb
29
+ - test/noie_test.rb
30
+ has_rdoc: true
31
+ homepage: http://github.com/juliocesar/rack-noie
32
+ post_install_message:
33
+ rdoc_options:
34
+ - --charset=UTF-8
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.2.0
53
+ signing_key:
54
+ specification_version: 2
55
+ summary: A Rack middleware to redirect IE6 users out of your website
56
+ test_files:
57
+ - test/noie_test.rb