rack-denyie 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,5 +1,7 @@
1
1
  == IE Filter on Rack
2
2
 
3
+ v1.1 = Fixed CSS issues with IE 4/5, Rewrote Tests
4
+
3
5
  === What?
4
6
  ===
5
7
 
@@ -33,7 +35,7 @@ With custom template and modified minimum version:
33
35
  :template => "/full/path/to/your/template.html" # Custom HTML template
34
36
  }
35
37
 
36
- With redirection set (not well tested):
38
+ With redirection set:
37
39
 
38
40
  use Rack::DenyIE, {
39
41
  :redirect_url => "http://browsehappy.com/"
@@ -60,8 +62,7 @@ Any key/value you pass in will be available to your ERB template.
60
62
  ===
61
63
 
62
64
  * Allow HAML templates
63
- * Write tests for url redirection
64
- * Tests need improvement
65
+ * More Tests
65
66
 
66
67
  == Copyright
67
68
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 1.1.1
data/lib/rack-denyie.rb CHANGED
@@ -1,99 +1,81 @@
1
1
  require 'rack'
2
- require 'erb'
3
2
 
4
3
  module Rack
5
-
6
- class DenyIE
7
-
8
- URL_REGEX = %r{\b(([\w-]+://?|[\w\d]+[.])[^\s()<>]+(?:(?:\([\w\d)]+\)[^\s()<>]*)+|([^[:punct:]\s]|/)))} # http://alanstorm.com/url_regex_explained
9
-
10
- # Default Options
11
- DEFAULT_MIN_VERSION = 7
12
- DEFAULT_HEADLINE = "This version of Internet Explorer is not supported."
13
- DEFAULT_SITE = "this site"
14
- DEFAULT_TEMPLATE_PATH = ::File.join(::File.dirname(__FILE__), 'html', 'default.erb')
15
-
16
- DEFAULT_REDIRECT_MESSAGE = "Your browser is unsupported."
17
-
18
- def initialize(app, options = {})
19
- @app = app
20
- @options = {
21
- :min_version => DEFAULT_MIN_VERSION, # The minimum version of IE you want allowed through
22
- :headline => DEFAULT_HEADLINE, # The headline message displayed to filtered IE users
23
- :site_name => DEFAULT_SITE, # Your site name.
24
- :template => DEFAULT_TEMPLATE_PATH, # A custom template, either ERB or HTML
25
- :redirect_url => nil }.merge options # Redirects users to a new location rather than displaying a template
26
- end
27
-
28
- def call(env)
29
- status, @headers, content = @app.call(env)
30
- is_unsupported?(env) && is_content_html? ? act_on_ie : @app.call(env)
31
- end
32
-
33
- private
34
-
35
- # Uses a regex to filter IE versions
36
- def is_unsupported?(env)
37
- minimum_version = (@options[:min_version].to_i - 1)
38
- env["HTTP_USER_AGENT"].match(/MSIE [0-#{minimum_version}]/) # \.[0-9][0-9]?
39
- end
40
-
41
- # Checks if the content is html, this middleware ONLY filters content that is HTML
42
- def is_content_html?
43
- @headers["Content-Type"] && @headers["Content-Type"].include?("text/html")
44
- end
45
-
46
- # Decides which action to take, either redirect or display template
47
- def act_on_ie
48
- should_redirect? ? redirect_browser : display_template
49
- end
50
-
51
- # Checks if redirection is set
52
- def should_redirect?
53
- !@options[:redirect_url].nil? && @options[:redirect_url].match(URL_REGEX)
54
- end
55
-
56
- # Redirects the browser to a new location
57
- def redirect_browser
58
- [302, { 'Location' => @options[:redirect_url] }, DEFAULT_REDIRECT_MESSAGE ]
59
- end
60
-
61
- # Displays the template
62
- def display_template
63
- response = (::File.extname(@options[:template]) == ".erb") ? ProcessERB.new(@options).bound : ::File.read(@options[:template])
64
- [200, @headers, response]
65
- end
4
+ class DenyIE
66
5
 
6
+ URL_REGEX = %r{\b(([\w-]+://?|[\w\d]+[.])[^\s()<>]+(?:(?:\([\w\d)]+\)[^\s()<>]*)+|([^[:punct:]\s]|/)))} # http://alanstorm.com/url_regex_explained
7
+
8
+ # Default Options
9
+ DEFAULT_MIN_VERSION = 7
10
+ DEFAULT_HEADLINE = "This version of Internet Explorer is not supported."
11
+ DEFAULT_SITE = "this site"
12
+ DEFAULT_TEMPLATE_PATH = ::File.join(::File.dirname(__FILE__), 'html', 'default.erb')
13
+
14
+ DEFAULT_REDIRECT_MESSAGE = "Your browser is unsupported."
15
+
16
+ def initialize(app, options = {})
17
+ @app = app
18
+ @options = {
19
+ :min_version => DEFAULT_MIN_VERSION, # The minimum version of IE you want allowed through
20
+ :headline => DEFAULT_HEADLINE, # The headline message displayed to filtered IE users
21
+ :site_name => DEFAULT_SITE, # Your site name.
22
+ :template => DEFAULT_TEMPLATE_PATH, # A custom template, either ERB or HTML
23
+ :redirect_url => nil }.merge options # Redirects users to a new location rather than displaying a template
24
+ end
25
+
26
+ def call(env)
27
+ status, @headers, content = @app.call(env)
28
+ is_unsupported?(env) && is_content_html? ? act_on_ie : @app.call(env)
29
+ end
30
+
31
+ private
32
+
33
+ # Uses a regex to filter IE versions
34
+ def is_unsupported?(env)
35
+ minimum_version = (@options[:min_version].to_i - 1)
36
+ env["HTTP_USER_AGENT"].match(/MSIE [0-#{minimum_version}]/) # \.[0-9][0-9]?
37
+ end
38
+
39
+ # Checks if the content is html, this middleware ONLY filters content that is HTML
40
+ def is_content_html?
41
+ @headers["Content-Type"] && @headers["Content-Type"].include?("text/html")
67
42
  end
68
-
69
- class ProcessERB
70
-
71
- def initialize(options = {})
72
- @options = options
73
- @erb = ERB.new(::File.read(@options[:template]), nil, "%<>")
74
- end
75
-
76
- def bound
77
- binding = TemplateVars.new(@options).get_binding # Variables passed to ERB template
78
- @template = @erb.result(binding)
79
- @template.gsub(/\n$/,'')
80
- end
81
-
43
+
44
+ # Decides which action to take, either redirect or display template
45
+ def act_on_ie
46
+ should_redirect? ? redirect_browser : display_template
82
47
  end
83
-
84
- # Binds the options as template variables (NOTE: This is probably a better way of doing this, I just don't know it.)
85
- class TemplateVars
86
-
87
- def initialize(options = {})
88
- options.each_pair do |key, value|
89
- self.instance_variable_set("@#{key}", value)
90
- end
91
- end
92
-
93
- def get_binding
94
- binding
95
- end
96
-
48
+
49
+ # Checks if redirection is set
50
+ def should_redirect?
51
+ !@options[:redirect_url].nil? && @options[:redirect_url].match(URL_REGEX)
97
52
  end
98
-
53
+
54
+ # Redirects the browser to a new location
55
+ def redirect_browser
56
+ [302, { 'Location' => @options[:redirect_url] }, DEFAULT_REDIRECT_MESSAGE ]
57
+ end
58
+
59
+ # Displays the template
60
+ def display_template
61
+ response = (::File.extname(@options[:template]) == ".erb") ? ProcessERB.new(@options).bound : ::File.read(@options[:template])
62
+ [200, @headers, response]
63
+ end
64
+
65
+ end
66
+
67
+ class ProcessERB
68
+ require 'erb'
69
+
70
+ def initialize(options = {})
71
+ @options = options
72
+ @erb = ERB.new(::File.read(@options[:template]), nil, "%<>")
73
+ end
74
+
75
+ def bound
76
+ @template = @erb.result(binding)
77
+ @template.gsub(/\n$/,'')
78
+ end
79
+
80
+ end
99
81
  end
data/rack-denyie.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rack-denyie}
8
- s.version = "1.1.0"
8
+ s.version = "1.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Brad Whitcomb"]
data/test/test_helper.rb CHANGED
@@ -4,6 +4,5 @@ require 'shoulda'
4
4
 
5
5
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
6
  $LOAD_PATH.unshift(File.dirname(__FILE__))
7
- require 'rack-denyie'
8
7
 
9
- class Test::Unit::TestCase; end
8
+ require 'rack-denyie'
@@ -1,5 +1,5 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
1
2
  require 'test_helper'
2
- require 'erb'
3
3
 
4
4
  class TestRackDenyIE < Test::Unit::TestCase
5
5
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-denyie
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brad Whitcomb