Skip to content
Browse files

option to exclude specific paths

  • Loading branch information...
1 parent c316c5e commit 84ac61a160caf83e651b5736bf2b6261820f8df2 @assembler committed
View
10 README.md
@@ -28,13 +28,23 @@ Initializer file is pretty self explanatory. Here is the default one:
config.supported "Firefox", "4"
config.supported "Opera", "11.1"
config.supported "Chrome", "7"
+
config.location "/browser.html"
+ config.exclude %r{^/assets}
end
It states that IE9+, FF4+, Opera 11.1+ and Chrome 7+ are supported.
Non listed browsers are considered to be supported regardless of their version.
Unsupported browsers will be redirected to `/browser.html` page.
+By default, only requests with `HTTP_ACCEPT` header set to `text/html` are
+processed. Sometimes this header is not set by the browser. To overcome this
+issue, the `exclude` option is added to the config.
+
+You can specify which paths you wish to exclude with `exclude` method.
+It accepts string or regular expression. You can specify multiple paths by
+calling the `config.exclude` multiple times.
+
If you wish to completely prevent some browsers from accessing website
(regardless of their version), for now you can specify some really high
version number (e.g. "666").
View
20 lib/browsernizer/config.rb
@@ -4,6 +4,7 @@ class Config
def initialize
@supported = []
@location = nil
+ @exclusions = []
@handler = lambda { }
end
@@ -15,6 +16,10 @@ def location(path)
@location = path
end
+ def exclude(path)
+ @exclusions << path
+ end
+
def get_supported
@supported
end
@@ -23,5 +28,20 @@ def get_location
@location
end
+ def excluded?(path)
+ @exclusions.any? do |exclusion|
+ case exclusion
+ when String
+ exclusion == path
+ when Regexp
+ exclusion =~ path
+ end
+ end
+ end
+
+ def exclusions_defined?
+ @exclusions.length > 0
+ end
+
end
end
View
25 lib/browsernizer/router.rb
@@ -16,24 +16,33 @@ def call(env)
"browser" => agent.browser.to_s,
"version" => agent.version.to_s
}
+ handle_request
+ end
- if html_request? && !on_redirection_path? && unsupported?
+ private
+ def handle_request
+ if !html_request? || path_excluded?
+ propagate_request
+ elsif !on_redirection_path? && unsupported?
handle_unsupported
- elsif html_request? && on_redirection_path? && !unsupported?
+ elsif on_redirection_path? && !unsupported?
handle_visits_by_accident
else
- @app.call(env)
+ propagate_request
end
end
- private
+ def propagate_request
+ @app.call(@env)
+ end
+
def handle_unsupported
@env["browsernizer"]["supported"] = false
if @config.get_location
[307, {"Content-Type" => "text/plain", "Location" => @config.get_location}, []]
else
- @app.call(@env)
+ propagate_request
end
end
@@ -41,10 +50,16 @@ def handle_visits_by_accident
[303, {"Content-Type" => "text/plain", "Location" => "/"}, []]
end
+ # if exclusions are defined, we'll be ignoring HTTP_ACCEPT header
def html_request?
+ return true if @config.exclusions_defined?
@env["HTTP_ACCEPT"] && @env["HTTP_ACCEPT"].include?("text/html")
end
+ def path_excluded?
+ @config.excluded? @env["PATH_INFO"]
+ end
+
def on_redirection_path?
@env["PATH_INFO"] && @env["PATH_INFO"] == @config.get_location
end
View
2 lib/browsernizer/version.rb
@@ -1,3 +1,3 @@
module Browsernizer
- VERSION = "0.1.1"
+ VERSION = "0.1.2"
end
View
2 lib/generators/templates/browsernizer.rb
@@ -3,5 +3,7 @@
config.supported "Firefox", "4"
config.supported "Opera", "11.1"
config.supported "Chrome", "7"
+
config.location "/browser.html"
+ config.exclude %r{^/assets}
end
View
15 spec/browsernizer/config_spec.rb
@@ -19,4 +19,19 @@
end
end
+ describe "exclude(path)" do
+ it "defines new excluded path" do
+ subject.exclude %r{^/assets}
+ subject.exclude "/foo/bar.html"
+
+ subject.excluded?("/assets/foo.jpg").should be_true
+ subject.excluded?("/Assets/foo.jpg").should be_false
+ subject.excluded?("/prefix/assets/foo.jpg").should be_false
+ subject.excluded?("/foo/bar.html").should be_true
+ subject.excluded?("/foo/bar2.html").should be_false
+
+ subject.exclusions_defined?.should be_true
+ end
+ end
+
end
View
27 spec/browsernizer/router_spec.rb
@@ -63,6 +63,19 @@
response[0].should == 307
response[1]["Location"].should == "/browser.html"
end
+
+ context "Excluded path" do
+ before do
+ subject.config.exclude %r{^/assets}
+ @env = @env.merge({
+ "PATH_INFO" => "/assets/foo.jpg",
+ })
+ end
+ it "propagates request" do
+ app.should_receive(:call).with(@env)
+ subject.call(@env)
+ end
+ end
end
context "Non-html request" do
@@ -75,8 +88,22 @@
app.should_receive(:call).with(@env)
subject.call(@env)
end
+
+ context "exclusions defined" do
+ before do
+ subject.config.exclude %r{^/assets}
+ subject.config.location "/browser.html"
+ end
+ it "handles the request" do
+ app.should_not_receive(:call).with(@env)
+ response = subject.call(@env)
+ response[0].should == 307
+ response[1]["Location"].should == "/browser.html"
+ end
+ end
end
+
context "Already on /browser.html page" do
before do
@env = @env.merge({

0 comments on commit 84ac61a

Please sign in to comment.
Something went wrong with that request. Please try again.