Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 00d5b6df12de5fab9a634ebd4702b0a15e3570e5 0 parents
@cavalle cavalle authored
0  README
No changes.
6 lib/rack/user_agent.rb
@@ -0,0 +1,6 @@
+require 'rack'
+
+module Rack::UserAgent
+ VERSION = "0.1.0"
+ autoload :Filter, 'rack/user_agent/filter'
+end
45 lib/rack/user_agent/filter.rb
@@ -0,0 +1,45 @@
+require 'rubygems'
+require 'user_agent'
+require 'erb'
+require 'ostruct'
+
+module Rack::UserAgent
+ class Filter
+ def initialize(app, config = [], options = {})
+ @app = app
+ @browsers = config
+ @template = options[:template]
+ end
+
+ def call(env)
+ if unsupported?(env["HTTP_USER_AGENT"])
+ @browser = UserAgent.parse(env["HTTP_USER_AGENT"])
+ # [200, {"Content-Type" => "text/html"}, [page(env['rack.locale'])]]
+ [400, {"Content-Type" => "text/html"}, page(env['rack.locale'])]
+ else
+ @app.call(env)
+ end
+ end
+
+ private
+
+ def unsupported?(user_agent)
+ user_agent && @browsers.any? { |browser| UserAgent.parse(user_agent) < OpenStruct.new(browser) }
+ end
+
+ def page(locale)
+ template_file = template_file(locale)
+ return "Sorry, your browser is not supported. Please upgrade" unless template_file
+ template = ERB.new(File.read(template_file))
+ template.result(binding)
+ end
+
+ def template_file(locale)
+ candidates = [ @template ]
+ candidates += [ File.join(RAILS_ROOT, "public", "upgrade.#{locale}.html"),
+ File.join(RAILS_ROOT, "public", "upgrade.html") ] if defined?(RAILS_ROOT)
+
+ candidates.compact.detect{ |template| File.exists?(template) }
+ end
+ end
+end
1  test/public/upgrade.es.html
@@ -0,0 +1 @@
+This is upgrade.es.html
1  test/public/upgrade.html
@@ -0,0 +1 @@
+This is upgrade.html
1  test/upgrade.erb
@@ -0,0 +1 @@
+Hello, <%= "#{@browser.browser} #{@browser.version}" %>!
75 test/user_agent_filter_test.rb
@@ -0,0 +1,75 @@
+require 'test/unit'
+require 'rubygems'
+require 'mocha'
+require 'context'
+
+require File.dirname(__FILE__) + '/../lib/rack/user_agent'
+
+class UserAgentFilterTest < Test::Unit::TestCase
+
+ STATUS = 0
+ HEADERS = 1
+ BODY = 2
+
+ def setup
+ @app_response = stub
+ @app = stub(:call => @app_response)
+ end
+
+ def teardown
+ Object.send(:remove_const, "RAILS_ROOT") if defined?(RAILS_ROOT)
+ end
+
+ def test_user_agent_supported
+ filter = Rack::UserAgent::Filter.new(@app, [{:browser => "Internet Explorer", :version => "7.0"}])
+ env = {"HTTP_USER_AGENT" => "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"}
+ @app.expects(:call).with(env).returns(@app_response)
+ response = filter.call(env)
+ assert_equal @app_response, response
+ end
+
+ def test_no_config_given
+ filter = Rack::UserAgent::Filter.new(@app)
+ response = filter.call({})
+ assert_equal @app_response, response
+ end
+
+ def test_user_agent_unsupported
+ filter = Rack::UserAgent::Filter.new(@app, [{:browser => "Internet Explorer", :version => "7.0"}])
+ response = filter.call("HTTP_USER_AGENT" => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)")
+ assert_equal 400, response[STATUS]
+ assert response[BODY].include?("Sorry, your browser is not supported. Please upgrade")
+ end
+
+ def test_user_agent_not_unsupported
+ filter = Rack::UserAgent::Filter.new(@app, [{:browser => "Internet Explorer", :version => "7.0"}])
+ response = filter.call("HTTP_USER_AGENT" => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14")
+ assert_equal @app_response, response
+ end
+
+ def test_upgrade_html_is_served_if_available
+ Object.const_set("RAILS_ROOT", File.dirname(__FILE__))
+ filter = Rack::UserAgent::Filter.new(@app, [{:browser => "Internet Explorer", :version => "7.0"}])
+ response = filter.call("HTTP_USER_AGENT" => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)")
+ assert_equal 400, response[STATUS]
+ assert_equal "This is upgrade.html", response[BODY]
+ end
+
+ def test_localized_upgrade_html
+ Object.const_set("RAILS_ROOT", File.dirname(__FILE__))
+ filter = Rack::UserAgent::Filter.new(@app, [{:browser => "Internet Explorer", :version => "7.0"}])
+ response = filter.call("HTTP_USER_AGENT" => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)",
+ "rack.locale" => "es")
+ assert_equal 400, response[STATUS]
+ assert_equal "This is upgrade.es.html", response[BODY]
+ end
+
+ def test_erb_template
+ filter = Rack::UserAgent::Filter.new(@app, [{:browser => "Internet Explorer", :version => "7.0"}],
+ :template => File.dirname(__FILE__) + "/upgrade.erb")
+ response = filter.call("HTTP_USER_AGENT" => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)")
+ assert_equal 400, response[STATUS]
+ assert_equal "Hello, Internet Explorer 6.0!", response[BODY]
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.