rack-denyie 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Brad Whitcomb, Digitalstar Studios
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,36 @@
1
+ == IE Filter on Rack
2
+
3
+ Use rack to filter out unsupported IE versions.
4
+ Use the default template, a custom template or redirect users to another location.
5
+
6
+ Because dealing with IE should be easy.
7
+ Because you can support what you want (if you're lucky).
8
+
9
+ === How?
10
+
11
+ # Default Options, IE < 7 & Default Template
12
+ use Rack::NoIE
13
+
14
+ use Rack::NoIE, {
15
+ :version => 7, # IE < 8
16
+ :template => '/full/path/to/your/template.html'
17
+ }
18
+
19
+ === Why?
20
+
21
+ 1. IE 6 was released in 2001.
22
+ 2. Developing for IE 6 costs extra time + money.
23
+ 3. Because they're releasing the alpha version of IE 9 next year.
24
+
25
+ === Release Years
26
+ ===
27
+
28
+ * IE 9 ~ 2010
29
+ * IE 8 = 2009
30
+ * IE 7 = 2006
31
+ * IE 6 = 2001 <= Should be unsupported
32
+ * IE 5 = 1999 <= Should be unsupported
33
+
34
+ == Copyright
35
+
36
+ Copyright (c) 2009 Brad Whitcomb, Digitalstar Studios. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rack-denyie"
8
+ gem.summary = "Use rack to filter out unsupported IE versions."
9
+ gem.description = "Use rack to filter out unsupported IE versions. Use default/custom template or redirect users to another place."
10
+ gem.email = "brad@digitalstarstudios.com"
11
+ gem.homepage = "http://github.com/whitcomb/rack-denyie"
12
+ gem.authors = ["Brad Whitcomb"]
13
+ gem.add_dependency "rack", "1.0.0"
14
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "rack-noie #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,17 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
+ <html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
5
+ <title>Please Upgrade</title>
6
+ <style type="text/css">
7
+ h1 {
8
+ color: red;
9
+ font-size: 22px;
10
+ }
11
+ </style>
12
+ </head>
13
+ <body>
14
+ <h1>Your browser is unsupported.</h1>
15
+ <p>Please upgrade to the latest version of <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx" title="Download Internet Explorer">Internet Explorer</a></p>
16
+ </body>
17
+ </html>
@@ -0,0 +1,47 @@
1
+ require 'rack'
2
+
3
+ module Rack
4
+ class DenyIE
5
+
6
+ DEFAULT_TEMPLATE_PATH = ::File.join(::File.dirname(__FILE__), 'html', 'default.html')
7
+
8
+ def initialize(app, options = {})
9
+ @app = app
10
+ @options = { :min_version => 6, :template_path => DEFAULT_TEMPLATE_PATH, :redirect_url => nil }.merge options
11
+ end
12
+
13
+ def call(env)
14
+ status, @headers, content = @app.call(env)
15
+ is_browser_supported?(env) && is_content_html? ? act_on_ie : @app.call(env)
16
+ end
17
+
18
+ private
19
+
20
+ def is_browser_supported?(env)
21
+ env["HTTP_USER_AGENT"].match(/MSIE [0-#{@options[:min_version]}]\.[0-9][0-9]?/)
22
+ end
23
+
24
+ def is_content_html?
25
+ @headers["Content-Type"] && @headers["Content-Type"].include?("text/html")
26
+ end
27
+
28
+ def act_on_ie
29
+ should_redirect? ? redirect_browser : display_template
30
+ end
31
+
32
+ def should_redirect?
33
+ !@options[:redirect_url].nil? && @options[:redirect_url].match(/(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix)
34
+ end
35
+
36
+ def redirect_browser
37
+ [302, { 'Location' => @options[:redirect_url] }, "Your browser is unsupported." ]
38
+ end
39
+
40
+ def display_template
41
+ response = ::File.read(@options[:template_path]) # TODO: Add check to see if template points to an actual file
42
+ @headers['Content-Length'] = Rack::Utils.bytesize(response.to_s).to_s
43
+ [200, @headers, response]
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
+ <html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
5
+ <title>Please Upgrade</title>
6
+ <style type="text/css">
7
+ h1 {
8
+ color: red;
9
+ font-size: 22px;
10
+ }
11
+ </style>
12
+ </head>
13
+ <body>
14
+ <h1>This is a custom template!</h1>
15
+ </body>
16
+ </html>
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'rack-denyie'
8
+
9
+ class Test::Unit::TestCase; end
@@ -0,0 +1,110 @@
1
+ require 'test_helper'
2
+
3
+ class TestRackDenyIE < Test::Unit::TestCase
4
+
5
+ BODY = "OK"
6
+
7
+ CUSTOM_TEMPLATE_PATH = File.expand_path(File.join(File.dirname(__FILE__), 'html', 'custom_template.html'))
8
+
9
+ def get_response(user_agent, body, content_type = 'text/html', options = {})
10
+ app = Rack::Builder.new do
11
+ use Rack::DenyIE, options
12
+ run lambda { |env| env["HTTP_USER_AGENT"] = user_agent; [200, {'Content-Type' => content_type}, [body] ] }
13
+ end
14
+ Rack::MockRequest.new(app).get('/')
15
+ end
16
+
17
+ context "When Rack::DenyIE" do
18
+
19
+ user_agents = [
20
+ "Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.1.5) Gecko/Firefox/3.5.5",
21
+ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; fi-fi) AppleWebKit/531.9 (KHTML, like Gecko) Version/4.0.3 Safari/531.9",
22
+ "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/417.9 (KHTML, like Gecko) Safari/417.8",
23
+ "Opera/9.64 (X11; Linux i686; U; tr) Presto/2.1.1"
24
+ ]
25
+
26
+ user_agents.each do |agent|
27
+ context "encounters a user agent like #{agent}" do
28
+ setup do
29
+ @response = get_response(agent, BODY)
30
+ end
31
+ should "return original response" do
32
+ assert_equal BODY, @response.body
33
+ end
34
+ end
35
+ end
36
+
37
+ context "encounters supported IE 7" do
38
+ setup do
39
+ user_agent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; Media Center PC 5.0; .NET CLR 1.1.4322; .NET CLR 3.5.21022)"
40
+ @response = get_response(user_agent, BODY, "text/html", { :version => 6 })
41
+ end
42
+ should "return original response" do
43
+ assert_equal BODY, @response.body
44
+ end
45
+ end
46
+
47
+ context "encounters supported IE 8" do
48
+ setup do
49
+ user_agent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729)"
50
+ @response = get_response(user_agent, BODY, "text/html", { :version => 7 })
51
+ end
52
+ should "return original response" do
53
+ assert_equal BODY, @response.body
54
+ end
55
+ end
56
+
57
+ context "encounters unsupported IE 6" do
58
+ setup do
59
+ user_agent = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"
60
+ @response = get_response(user_agent, BODY, "text/html", { :min_version => 6 })
61
+ end
62
+ should "return default IE template" do
63
+ default_template = File.read(Rack::DenyIE::DEFAULT_TEMPLATE_PATH)
64
+
65
+ assert_equal default_template, @response.body
66
+ end
67
+ end
68
+
69
+ context "encounters unsupported IE 7" do
70
+ setup do
71
+ user_agent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; Media Center PC 5.0; .NET CLR 1.1.4322; .NET CLR 3.5.21022)"
72
+ @response = get_response(user_agent, BODY, "text/html", { :min_version => 7 })
73
+ end
74
+ should "return default IE template" do
75
+ default_template = File.read(Rack::DenyIE::DEFAULT_TEMPLATE_PATH)
76
+
77
+ assert_equal default_template, @response.body
78
+ end
79
+ end
80
+
81
+ context "encounters unsupported IE 6 and custom template" do
82
+ setup do
83
+ user_agent = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"
84
+ @response = get_response(user_agent, BODY, "text/html", { :min_version => 6, :template_path => CUSTOM_TEMPLATE_PATH })
85
+ end
86
+ should "return custom IE template" do
87
+ default_template = File.read(Rack::DenyIE::DEFAULT_TEMPLATE_PATH)
88
+ custom_template = File.read(CUSTOM_TEMPLATE_PATH)
89
+
90
+ assert_not_equal default_template, @response.body
91
+ assert_equal custom_template, @response.body
92
+ end
93
+ end
94
+
95
+ context "encounters unsupported IE 7 and custom template" do
96
+ setup do
97
+ user_agent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; Media Center PC 5.0; .NET CLR 1.1.4322; .NET CLR 3.5.21022)"
98
+ @response = get_response(user_agent, BODY, "text/html", { :min_version => 7, :template_path => CUSTOM_TEMPLATE_PATH })
99
+ end
100
+ should "return custom IE template" do
101
+ default_template = File.read(Rack::DenyIE::DEFAULT_TEMPLATE_PATH)
102
+ custom_template = File.read(CUSTOM_TEMPLATE_PATH)
103
+
104
+ assert_not_equal default_template, @response.body
105
+ assert_equal custom_template, @response.body
106
+ end
107
+ end
108
+
109
+ end
110
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-denyie
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brad Whitcomb
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-24 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - "="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.0.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: thoughtbot-shoulda
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: Use rack to filter out unsupported IE versions. Use default/custom template or redirect users to another place.
36
+ email: brad@digitalstarstudios.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/html/default.html
52
+ - lib/rack-denyie.rb
53
+ - test/html/custom_template.html
54
+ - test/test_helper.rb
55
+ - test/test_rack-denyie.rb
56
+ has_rdoc: true
57
+ homepage: http://github.com/whitcomb/rack-denyie
58
+ licenses: []
59
+
60
+ post_install_message:
61
+ rdoc_options:
62
+ - --charset=UTF-8
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ version:
77
+ requirements: []
78
+
79
+ rubyforge_project:
80
+ rubygems_version: 1.3.5
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Use rack to filter out unsupported IE versions.
84
+ test_files:
85
+ - test/test_helper.rb
86
+ - test/test_rack-denyie.rb