Skip to content

Commit

Permalink
option to exclude specific paths
Browse files Browse the repository at this point in the history
  • Loading branch information
assembler committed Feb 14, 2012
1 parent c316c5e commit 84ac61a
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 6 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand Down
20 changes: 20 additions & 0 deletions lib/browsernizer/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Config
def initialize
@supported = []
@location = nil
@exclusions = []
@handler = lambda { }
end

Expand All @@ -15,6 +16,10 @@ def location(path)
@location = path
end

def exclude(path)
@exclusions << path
end

def get_supported
@supported
end
Expand All @@ -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
25 changes: 20 additions & 5 deletions lib/browsernizer/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,50 @@ 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

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
Expand Down
2 changes: 1 addition & 1 deletion lib/browsernizer/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Browsernizer
VERSION = "0.1.1"
VERSION = "0.1.2"
end
2 changes: 2 additions & 0 deletions lib/generators/templates/browsernizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions spec/browsernizer/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 27 additions & 0 deletions spec/browsernizer/router_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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({
Expand Down

0 comments on commit 84ac61a

Please sign in to comment.