bebanjo-rack-useragent 0.0.1

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.rdoc ADDED
@@ -0,0 +1,58 @@
1
+ = Rack::UserAgent
2
+
3
+ Rack Middleware for filtering by user agent
4
+
5
+ == Use
6
+
7
+ Let's say you don't support IE6 and want to prevent IE6 users from accessing to your Rails app. You're in luck. Just add this to your <tt>config/environment.rb</tt>:
8
+
9
+ config.gem "bebanjo-rack-useragent", :lib => "rack/user_agent", :source => "http://gems.github.com"
10
+
11
+ config.middleware.use "Rack::UserAgent::Filter", [
12
+ {:browser => "Internet Explorer", :version => "7.0"}
13
+ ]
14
+
15
+ And, of course, install the gem:
16
+
17
+ $ [sudo] rake gems:install
18
+
19
+ Done! From now on users with IE version lower than 7.0 will get a nice error message saying that their browsers need to be updated... Ok, ok, not so nice message, you can personalize it by putting an <tt>upgrade.html</tt> file inside the <tt>/public</tt> directory of your application.
20
+
21
+ <em>- Hey, I need to show localized versions of the message to my french and spanish users!</em>
22
+
23
+ No problem, just add <tt>upgrade.es.html</tt> and <tt>upgrade.fr.html</tt> to <tt>/public</tt> (Note that <tt>rack.locale</tt> env variable needs to be previously set for this to work. <tt>Rack::Locale</tt> in rack-contrib[http://github.com/rack/rack-contrib] can do that for you)
24
+
25
+ <em>- Cool!, what about something more dynamic... like ERB?</em>
26
+
27
+ Granted! You'll have to add to the config which template to use. In <tt>environment.rb</tt>:
28
+
29
+ config.middleware.use "Rack::UserAgent::Filter", [
30
+ {:browser => "Internet Explorer", :version => "7.0"}
31
+ ], :template => "#{RAILS_ROOT}/app/views/shared/upgrade.html.erb"
32
+
33
+ Then you could show the browser version as part of your message:
34
+
35
+ Sorry but <%= "#{@browser.browser} #{@browser.version}" %> is not supported
36
+
37
+ <em>- Can I use it with non-Rails Rack frameworks, like Sinatra?</em>
38
+
39
+ Sure. You only have to take into account that you'll always have to set which template to use. i.e: In <tt>config.ru</tt>:
40
+
41
+ require 'rack/user_agent'
42
+
43
+ use Rack::UserAgent::Filter, [
44
+ {:browser => "Internet Explorer", :version => "7.0"}
45
+ ], :template => File.dirname(__FILE__) + "/public/upgrade.html"
46
+
47
+ Enjoy it!
48
+
49
+ == Authors
50
+
51
+ * Sergio Gil <sgilperez@gmail.com>
52
+ * Luismi Cavallé <luismi@lmcavalle.com>
53
+
54
+ == Contributors
55
+
56
+ We're looking forward for your patches. Fork us!
57
+
58
+ Copyright (c) 2008 BeBanjo, released under the MIT license
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'user_agent'
3
+ require 'erb'
4
+ require 'ostruct'
5
+
6
+ module Rack::UserAgent
7
+ class Filter
8
+ def initialize(app, config = [], options = {})
9
+ @app = app
10
+ @browsers = config
11
+ @template = options[:template]
12
+ end
13
+
14
+ def call(env)
15
+ browser = UserAgent.parse(env["HTTP_USER_AGENT"]) if env["HTTP_USER_AGENT"]
16
+ if unsupported?(browser)
17
+ [400, {"Content-Type" => "text/html"}, page(env['rack.locale'], browser)]
18
+ else
19
+ @app.call(env)
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def unsupported?(browser)
26
+ browser && @browsers.any? { |hash| browser < OpenStruct.new(hash) }
27
+ end
28
+
29
+ def page(locale, browser)
30
+ return "Sorry, your browser is not supported. Please upgrade" unless template = template_file(locale)
31
+ @browser = browser # for the template
32
+ ERB.new(File.read(template)).result(binding)
33
+ end
34
+
35
+ def template_file(locale)
36
+ candidates = [ @template ]
37
+
38
+ if defined?(RAILS_ROOT)
39
+ candidates += [ File.join(RAILS_ROOT, "public", "upgrade.#{locale}.html"),
40
+ File.join(RAILS_ROOT, "public", "upgrade.html") ]
41
+ end
42
+
43
+ candidates.compact.detect{ |template| File.exists?(template) }
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,6 @@
1
+ require 'rack'
2
+
3
+ module Rack::UserAgent
4
+ VERSION = "0.1.0"
5
+ autoload :Filter, 'rack/user_agent/filter'
6
+ end
@@ -0,0 +1,50 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{rack-useragent}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Sergio Gil", "Luismi Cavall\303\251"]
9
+ s.date = %q{2009-05-22}
10
+ s.email = %q{ballsbreaking@bebanjo.com}
11
+ s.extra_rdoc_files = [
12
+ "README.rdoc"
13
+ ]
14
+ s.files = [
15
+ "README.rdoc",
16
+ "VERSION",
17
+ "lib/rack/user_agent.rb",
18
+ "lib/rack/user_agent/filter.rb",
19
+ "rack-useragent.gemspec",
20
+ "test/public/upgrade.es.html",
21
+ "test/public/upgrade.html",
22
+ "test/upgrade.erb",
23
+ "test/user_agent_filter_test.rb"
24
+ ]
25
+ s.has_rdoc = true
26
+ s.homepage = %q{http://github.com/bebanjo/rack-useragent}
27
+ s.rdoc_options = ["--charset=UTF-8"]
28
+ s.require_paths = ["lib"]
29
+ s.rubygems_version = %q{1.3.1}
30
+ s.summary = %q{Rack Middleware for filtering by user agent}
31
+ s.test_files = [
32
+ "test/user_agent_filter_test.rb"
33
+ ]
34
+
35
+ if s.respond_to? :specification_version then
36
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
37
+ s.specification_version = 2
38
+
39
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
40
+ s.add_runtime_dependency(%q<rack>, [">= 0.9.1"])
41
+ s.add_runtime_dependency(%q<josh-useragent>, [">= 0"])
42
+ else
43
+ s.add_dependency(%q<rack>, [">= 0.9.1"])
44
+ s.add_dependency(%q<josh-useragent>, [">= 0"])
45
+ end
46
+ else
47
+ s.add_dependency(%q<rack>, [">= 0.9.1"])
48
+ s.add_dependency(%q<josh-useragent>, [">= 0"])
49
+ end
50
+ end
@@ -0,0 +1 @@
1
+ This is upgrade.es.html
@@ -0,0 +1 @@
1
+ This is upgrade.html
data/test/upgrade.erb ADDED
@@ -0,0 +1 @@
1
+ Hello, <%= "#{@browser.browser} #{@browser.version}" %>!
@@ -0,0 +1,81 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'mocha'
4
+
5
+ require File.dirname(__FILE__) + '/../lib/rack/user_agent'
6
+
7
+ class UserAgentFilterTest < Test::Unit::TestCase
8
+
9
+ STATUS = 0
10
+ HEADERS = 1
11
+ BODY = 2
12
+
13
+ def setup
14
+ @app_response = stub
15
+ @app = stub(:call => @app_response)
16
+ end
17
+
18
+ def teardown
19
+ Object.send(:remove_const, "RAILS_ROOT") if defined?(RAILS_ROOT)
20
+ end
21
+
22
+ def test_user_agent_supported
23
+ filter = Rack::UserAgent::Filter.new(@app, [{:browser => "Internet Explorer", :version => "7.0"}])
24
+ env = {"HTTP_USER_AGENT" => "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"}
25
+ @app.expects(:call).with(env).returns(@app_response)
26
+ response = filter.call(env)
27
+ assert_equal @app_response, response
28
+ end
29
+
30
+ def test_no_config_given
31
+ filter = Rack::UserAgent::Filter.new(@app)
32
+ env = {"HTTP_USER_AGENT" => "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"}
33
+ response = filter.call(env)
34
+ assert_equal @app_response, response
35
+ end
36
+
37
+ def test_no_user_agent
38
+ filter = Rack::UserAgent::Filter.new(@app, [{:browser => "Internet Explorer", :version => "7.0"}])
39
+ response = filter.call({})
40
+ assert_equal @app_response, response
41
+ end
42
+
43
+ def test_user_agent_unsupported
44
+ filter = Rack::UserAgent::Filter.new(@app, [{:browser => "Internet Explorer", :version => "7.0"}])
45
+ response = filter.call("HTTP_USER_AGENT" => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)")
46
+ assert_equal 400, response[STATUS]
47
+ assert response[BODY].include?("Sorry, your browser is not supported. Please upgrade")
48
+ end
49
+
50
+ def test_user_agent_not_unsupported
51
+ filter = Rack::UserAgent::Filter.new(@app, [{:browser => "Internet Explorer", :version => "7.0"}])
52
+ 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")
53
+ assert_equal @app_response, response
54
+ end
55
+
56
+ def test_upgrade_html_is_served_if_available
57
+ Object.const_set("RAILS_ROOT", File.dirname(__FILE__))
58
+ filter = Rack::UserAgent::Filter.new(@app, [{:browser => "Internet Explorer", :version => "7.0"}])
59
+ response = filter.call("HTTP_USER_AGENT" => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)")
60
+ assert_equal 400, response[STATUS]
61
+ assert_equal "This is upgrade.html", response[BODY]
62
+ end
63
+
64
+ def test_localized_upgrade_html
65
+ Object.const_set("RAILS_ROOT", File.dirname(__FILE__))
66
+ filter = Rack::UserAgent::Filter.new(@app, [{:browser => "Internet Explorer", :version => "7.0"}])
67
+ response = filter.call("HTTP_USER_AGENT" => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)",
68
+ "rack.locale" => "es")
69
+ assert_equal 400, response[STATUS]
70
+ assert_equal "This is upgrade.es.html", response[BODY]
71
+ end
72
+
73
+ def test_erb_template
74
+ filter = Rack::UserAgent::Filter.new(@app, [{:browser => "Internet Explorer", :version => "7.0"}],
75
+ :template => File.dirname(__FILE__) + "/upgrade.erb")
76
+ response = filter.call("HTTP_USER_AGENT" => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)")
77
+ assert_equal 400, response[STATUS]
78
+ assert_equal "Hello, Internet Explorer 6.0!", response[BODY]
79
+ end
80
+
81
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bebanjo-rack-useragent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sergio Gil
8
+ - "Luismi Cavall\xC3\xA9"
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-05-22 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rack
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 0.9.1
25
+ version:
26
+ - !ruby/object:Gem::Dependency
27
+ name: josh-useragent
28
+ type: :runtime
29
+ version_requirement:
30
+ version_requirements: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ version:
36
+ description:
37
+ email: ballsbreaking@bebanjo.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - README.rdoc
44
+ files:
45
+ - README.rdoc
46
+ - VERSION
47
+ - lib/rack/user_agent.rb
48
+ - lib/rack/user_agent/filter.rb
49
+ - rack-useragent.gemspec
50
+ - test/public/upgrade.es.html
51
+ - test/public/upgrade.html
52
+ - test/upgrade.erb
53
+ - test/user_agent_filter_test.rb
54
+ has_rdoc: true
55
+ homepage: http://github.com/bebanjo/rack-useragent
56
+ post_install_message:
57
+ rdoc_options:
58
+ - --charset=UTF-8
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.2.0
77
+ signing_key:
78
+ specification_version: 2
79
+ summary: Rack Middleware for filtering by user agent
80
+ test_files:
81
+ - test/user_agent_filter_test.rb