browsernizer 0.1.1 → 0.1.2

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.md CHANGED
@@ -28,13 +28,23 @@ Initializer file is pretty self explanatory. Here is the default one:
28
28
  config.supported "Firefox", "4"
29
29
  config.supported "Opera", "11.1"
30
30
  config.supported "Chrome", "7"
31
+
31
32
  config.location "/browser.html"
33
+ config.exclude %r{^/assets}
32
34
  end
33
35
 
34
36
  It states that IE9+, FF4+, Opera 11.1+ and Chrome 7+ are supported.
35
37
  Non listed browsers are considered to be supported regardless of their version.
36
38
  Unsupported browsers will be redirected to `/browser.html` page.
37
39
 
40
+ By default, only requests with `HTTP_ACCEPT` header set to `text/html` are
41
+ processed. Sometimes this header is not set by the browser. To overcome this
42
+ issue, the `exclude` option is added to the config.
43
+
44
+ You can specify which paths you wish to exclude with `exclude` method.
45
+ It accepts string or regular expression. You can specify multiple paths by
46
+ calling the `config.exclude` multiple times.
47
+
38
48
  If you wish to completely prevent some browsers from accessing website
39
49
  (regardless of their version), for now you can specify some really high
40
50
  version number (e.g. "666").
@@ -4,6 +4,7 @@ module Browsernizer
4
4
  def initialize
5
5
  @supported = []
6
6
  @location = nil
7
+ @exclusions = []
7
8
  @handler = lambda { }
8
9
  end
9
10
 
@@ -15,6 +16,10 @@ module Browsernizer
15
16
  @location = path
16
17
  end
17
18
 
19
+ def exclude(path)
20
+ @exclusions << path
21
+ end
22
+
18
23
  def get_supported
19
24
  @supported
20
25
  end
@@ -23,5 +28,20 @@ module Browsernizer
23
28
  @location
24
29
  end
25
30
 
31
+ def excluded?(path)
32
+ @exclusions.any? do |exclusion|
33
+ case exclusion
34
+ when String
35
+ exclusion == path
36
+ when Regexp
37
+ exclusion =~ path
38
+ end
39
+ end
40
+ end
41
+
42
+ def exclusions_defined?
43
+ @exclusions.length > 0
44
+ end
45
+
26
46
  end
27
47
  end
@@ -16,24 +16,33 @@ module Browsernizer
16
16
  "browser" => agent.browser.to_s,
17
17
  "version" => agent.version.to_s
18
18
  }
19
+ handle_request
20
+ end
19
21
 
20
- if html_request? && !on_redirection_path? && unsupported?
22
+ private
23
+ def handle_request
24
+ if !html_request? || path_excluded?
25
+ propagate_request
26
+ elsif !on_redirection_path? && unsupported?
21
27
  handle_unsupported
22
- elsif html_request? && on_redirection_path? && !unsupported?
28
+ elsif on_redirection_path? && !unsupported?
23
29
  handle_visits_by_accident
24
30
  else
25
- @app.call(env)
31
+ propagate_request
26
32
  end
27
33
  end
28
34
 
29
- private
35
+ def propagate_request
36
+ @app.call(@env)
37
+ end
38
+
30
39
  def handle_unsupported
31
40
  @env["browsernizer"]["supported"] = false
32
41
 
33
42
  if @config.get_location
34
43
  [307, {"Content-Type" => "text/plain", "Location" => @config.get_location}, []]
35
44
  else
36
- @app.call(@env)
45
+ propagate_request
37
46
  end
38
47
  end
39
48
 
@@ -41,10 +50,16 @@ module Browsernizer
41
50
  [303, {"Content-Type" => "text/plain", "Location" => "/"}, []]
42
51
  end
43
52
 
53
+ # if exclusions are defined, we'll be ignoring HTTP_ACCEPT header
44
54
  def html_request?
55
+ return true if @config.exclusions_defined?
45
56
  @env["HTTP_ACCEPT"] && @env["HTTP_ACCEPT"].include?("text/html")
46
57
  end
47
58
 
59
+ def path_excluded?
60
+ @config.excluded? @env["PATH_INFO"]
61
+ end
62
+
48
63
  def on_redirection_path?
49
64
  @env["PATH_INFO"] && @env["PATH_INFO"] == @config.get_location
50
65
  end
@@ -1,3 +1,3 @@
1
1
  module Browsernizer
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -3,5 +3,7 @@ Rails.application.config.middleware.use Browsernizer::Router do |config|
3
3
  config.supported "Firefox", "4"
4
4
  config.supported "Opera", "11.1"
5
5
  config.supported "Chrome", "7"
6
+
6
7
  config.location "/browser.html"
8
+ config.exclude %r{^/assets}
7
9
  end
@@ -19,4 +19,19 @@ describe Browsernizer::Config do
19
19
  end
20
20
  end
21
21
 
22
+ describe "exclude(path)" do
23
+ it "defines new excluded path" do
24
+ subject.exclude %r{^/assets}
25
+ subject.exclude "/foo/bar.html"
26
+
27
+ subject.excluded?("/assets/foo.jpg").should be_true
28
+ subject.excluded?("/Assets/foo.jpg").should be_false
29
+ subject.excluded?("/prefix/assets/foo.jpg").should be_false
30
+ subject.excluded?("/foo/bar.html").should be_true
31
+ subject.excluded?("/foo/bar2.html").should be_false
32
+
33
+ subject.exclusions_defined?.should be_true
34
+ end
35
+ end
36
+
22
37
  end
@@ -63,6 +63,19 @@ describe Browsernizer::Router do
63
63
  response[0].should == 307
64
64
  response[1]["Location"].should == "/browser.html"
65
65
  end
66
+
67
+ context "Excluded path" do
68
+ before do
69
+ subject.config.exclude %r{^/assets}
70
+ @env = @env.merge({
71
+ "PATH_INFO" => "/assets/foo.jpg",
72
+ })
73
+ end
74
+ it "propagates request" do
75
+ app.should_receive(:call).with(@env)
76
+ subject.call(@env)
77
+ end
78
+ end
66
79
  end
67
80
 
68
81
  context "Non-html request" do
@@ -75,8 +88,22 @@ describe Browsernizer::Router do
75
88
  app.should_receive(:call).with(@env)
76
89
  subject.call(@env)
77
90
  end
91
+
92
+ context "exclusions defined" do
93
+ before do
94
+ subject.config.exclude %r{^/assets}
95
+ subject.config.location "/browser.html"
96
+ end
97
+ it "handles the request" do
98
+ app.should_not_receive(:call).with(@env)
99
+ response = subject.call(@env)
100
+ response[0].should == 307
101
+ response[1]["Location"].should == "/browser.html"
102
+ end
103
+ end
78
104
  end
79
105
 
106
+
80
107
  context "Already on /browser.html page" do
81
108
  before do
82
109
  @env = @env.merge({
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: browsernizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-08 00:00:00.000000000Z
12
+ date: 2012-02-14 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70317243772500 !ruby/object:Gem::Requirement
16
+ requirement: &70284088116900 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70317243772500
24
+ version_requirements: *70284088116900
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70317243772080 !ruby/object:Gem::Requirement
27
+ requirement: &70284088116480 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70317243772080
35
+ version_requirements: *70284088116480
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: useragent
38
- requirement: &70317243771580 !ruby/object:Gem::Requirement
38
+ requirement: &70284088115980 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: 0.4.6
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70317243771580
46
+ version_requirements: *70284088115980
47
47
  description: Rack middleware for redirecting unsupported user agents to "please upgrade"
48
48
  page
49
49
  email: