rack-useragent-filter 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,68 @@
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>- Cool!, what about something more dynamic... like ERB?</em>
22
+
23
+ Granted! You'll have to add to the config which template to use. In <tt>environment.rb</tt>:
24
+
25
+ config.middleware.use "Rack::UserAgent::Filter", [
26
+ {:browser => "Internet Explorer", :version => "7.0"}
27
+ ], :template => "#{RAILS_ROOT}/app/views/shared/upgrade.html.erb"
28
+
29
+ Then you could show the browser version as part of your message:
30
+
31
+ Sorry but <%= "#{@browser.browser} #{@browser.version}" %> is not supported
32
+
33
+ <em>- Can I use it with non-Rails Rack frameworks, like Sinatra?</em>
34
+
35
+ 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>:
36
+
37
+ require 'rack/user_agent'
38
+
39
+ use Rack::UserAgent::Filter, [
40
+ {:browser => "Internet Explorer", :version => "7.0"}
41
+ ], :template => File.dirname(__FILE__) + "/public/upgrade.html"
42
+
43
+
44
+ <em>- But... what if IE6 user do wants to see how page looks like in his favourite browser?</em>
45
+
46
+ You can show button "I take full responsibility of using IE6. Let me in!". User clicks button and with some little help
47
+ of Javascript cookie named i.e: "deprecated_browser" is saved on user's disk.
48
+ Next time user hits application, middleware checks request for inclusion of cookie "deprecated_browser".
49
+ If it exists unsupported browsers detection is disabled for this user.
50
+
51
+ config.middleware.use "Rack::UserAgent::Filter", [
52
+ {:browser => "Internet Explorer", :version => "7.0"}
53
+ ], :force_with_cookie => "deprecated_browser"
54
+
55
+ Enjoy it!
56
+
57
+ == Authors
58
+
59
+ * Sergio Gil <sgilperez@gmail.com>
60
+ * Luismi Cavallé <luismi@lmcavalle.com>
61
+
62
+ == Contributors
63
+
64
+ * Tomasz Mazur <defkode@gmail.com>
65
+
66
+ We're looking forward for your patches. Fork us!
67
+
68
+ Copyright (c) 2008 BeBanjo, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Run all tests'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ begin
17
+ require 'jeweler'
18
+ Jeweler::Tasks.new do |gemspec|
19
+ gemspec.name = "rack-useragent-filter"
20
+ gemspec.authors = ["Sergio Gil", "Luismi Cavallé", "Tomasz Mazur"]
21
+ gemspec.email = ["ballsbreaking@bebanjo.com", "defkode@gmail.com"]
22
+ gemspec.homepage = "http://github.com/defkode/rack-useragent"
23
+ gemspec.summary = "Rack Middleware for filtering by user agent"
24
+ gemspec.add_dependency('rack', '>= 0.9.1')
25
+ gemspec.add_dependency('useragent', '>=0.1.4')
26
+ end
27
+ rescue LoadError
28
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.3
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Your Browser is not fully Supported</title>
5
+ <script type='text/javascript'>
6
+ //<![CDATA[
7
+
8
+ // save cookie
9
+ function createCookie(name, value) {
10
+ document.cookie = name + "=" + value + "; path=/";
11
+ }
12
+
13
+ // creates cookie "deprecated_browser" with UserAgent as value and reloads the page
14
+ function continueWithUnsupportedBrowser(){
15
+ createCookie('deprecated_browser', window.navigator.userAgent);
16
+ document.location.reload(true);
17
+ return false
18
+ }
19
+ //]]>
20
+ </script>
21
+ </head>
22
+ <body>
23
+ This is upgrade.html
24
+ <button onclick='continueWithUnsupportedBrowser()'>I am aware of this restriction, I want to continue</button>
25
+ </body>
26
+ </html>
@@ -0,0 +1,4 @@
1
+ require 'rack'
2
+ require 'rack/mock'
3
+
4
+ require 'rack/user_agent/filter'
@@ -0,0 +1,45 @@
1
+ require 'user_agent'
2
+ require 'erb'
3
+ require 'ostruct'
4
+
5
+ module Rack::UserAgent
6
+ class Filter
7
+ def initialize(app, browsers = [], options = {})
8
+ @app = app
9
+ @browsers = browsers
10
+ @template = options[:template]
11
+ @force_with_cookie = options[:force_with_cookie]
12
+ end
13
+
14
+ def call(env)
15
+ request = Rack::Request.new(env)
16
+ browser = UserAgent.parse(env["HTTP_USER_AGENT"]) if env["HTTP_USER_AGENT"]
17
+ if !detection_disabled_by_cookie?(request.cookies) && unsupported?(browser)
18
+ content = render_page(browser)
19
+ [400, {"Content-Type" => "text/html", "Content-Length" => content.length.to_s}, content]
20
+ else
21
+ @app.call(env)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def unsupported?(browser)
28
+ browser && @browsers.map{ |hash| browser < OpenStruct.new(hash) }.any?
29
+ end
30
+
31
+ def detection_disabled_by_cookie?(cookies)
32
+ @force_with_cookie && cookies.keys.include?(@force_with_cookie)
33
+ end
34
+
35
+ def render_page(browser)
36
+ if @template && File.exists?(@template)
37
+ @browser = browser # for the template
38
+ ERB.new(File.read(@template)).result(binding)
39
+ else
40
+ "Sorry, your browser is not supported. Please upgrade"
41
+ end
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1 @@
1
+ Hello, <%= "#{@browser.browser} #{@browser.version}" %>!
@@ -0,0 +1,11 @@
1
+ require "rubygems"
2
+ require "rack/test"
3
+ require "shoulda"
4
+
5
+ require File.dirname(__FILE__) + '/../lib/rack/user_agent'
6
+
7
+ class SampleApp
8
+ def call(env)
9
+ [200, {}, "Sample Response"]
10
+ end
11
+ end
@@ -0,0 +1,81 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class UserAgentFilterTest < Test::Unit::TestCase
4
+ include Rack::Test::Methods
5
+
6
+ def setup
7
+ @outdated_browser = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)" # IE6
8
+ @modern_browser = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)" # IE8
9
+ end
10
+
11
+ def app
12
+ SampleApp.new
13
+ end
14
+
15
+ context "default page" do
16
+
17
+ setup do
18
+ def app
19
+ Rack::Builder.new do
20
+ use Rack::UserAgent::Filter, [{:browser => "Internet Explorer", :version => "7.0"}]
21
+ run SampleApp.new
22
+ end
23
+ end
24
+ end
25
+ should "pass" do
26
+ header "User-Agent", @modern_browser
27
+ get '/'
28
+ assert_equal "Sample Response", last_response.body
29
+ end
30
+
31
+ should "filter" do
32
+ header "User-Agent", @outdated_browser
33
+ get '/'
34
+ assert_equal "Sorry, your browser is not supported. Please upgrade", last_response.body
35
+ end
36
+
37
+ end
38
+
39
+ context "custom page" do
40
+ setup do
41
+ def app
42
+ Rack::Builder.new do
43
+ use Rack::UserAgent::Filter, [{:browser => "Internet Explorer", :version => "7.0"}], :template => File.dirname(__FILE__) + "/fixtures/upgrade.erb"
44
+ run SampleApp.new
45
+ end
46
+ end
47
+ end
48
+
49
+ should "work" do
50
+ header "User-Agent", @outdated_browser
51
+ get '/'
52
+ assert_equal "Hello, Internet Explorer 6.0!", last_response.body
53
+ end
54
+
55
+ end
56
+
57
+ context "cookie" do
58
+
59
+ setup do
60
+ def app
61
+ Rack::Builder.new do
62
+ use Rack::UserAgent::Filter, [{:browser => "Internet Explorer", :version => "7.0"}], :force_with_cookie => "browser"
63
+ run SampleApp.new
64
+ end
65
+ end
66
+ end
67
+
68
+ should "pass if cookie set" do
69
+ header "User-Agent", @outdated_browser
70
+ set_cookie "browser=Internet Explorer"
71
+ get '/'
72
+ assert_equal "Sample Response", last_response.body
73
+
74
+ clear_cookies
75
+ get '/'
76
+ assert_equal "Sorry, your browser is not supported. Please upgrade", last_response.body
77
+ end
78
+
79
+ end
80
+
81
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-useragent-filter
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 3
10
+ version: 0.1.3
11
+ platform: ruby
12
+ authors:
13
+ - Sergio Gil
14
+ - "Luismi Cavall\xC3\xA9"
15
+ - Tomasz Mazur
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2010-06-01 00:00:00 +02:00
21
+ default_executable:
22
+ dependencies:
23
+ - !ruby/object:Gem::Dependency
24
+ name: rack
25
+ prerelease: false
26
+ requirement: &id001 !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ hash: 57
32
+ segments:
33
+ - 0
34
+ - 9
35
+ - 1
36
+ version: 0.9.1
37
+ type: :runtime
38
+ version_requirements: *id001
39
+ - !ruby/object:Gem::Dependency
40
+ name: useragent
41
+ prerelease: false
42
+ requirement: &id002 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ hash: 19
48
+ segments:
49
+ - 0
50
+ - 1
51
+ - 4
52
+ version: 0.1.4
53
+ type: :runtime
54
+ version_requirements: *id002
55
+ description:
56
+ email:
57
+ - ballsbreaking@bebanjo.com
58
+ - defkode@gmail.com
59
+ executables: []
60
+
61
+ extensions: []
62
+
63
+ extra_rdoc_files:
64
+ - README.rdoc
65
+ files:
66
+ - README.rdoc
67
+ - Rakefile
68
+ - VERSION
69
+ - examples/upgrade.html
70
+ - lib/rack/user_agent.rb
71
+ - lib/rack/user_agent/filter.rb
72
+ - test/fixtures/upgrade.erb
73
+ - test/test_helper.rb
74
+ - test/user_agent_filter_test.rb
75
+ has_rdoc: true
76
+ homepage: http://github.com/defkode/rack-useragent
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options:
81
+ - --charset=UTF-8
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ requirements: []
103
+
104
+ rubyforge_project:
105
+ rubygems_version: 1.3.7
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: Rack Middleware for filtering by user agent
109
+ test_files:
110
+ - test/test_helper.rb
111
+ - test/user_agent_filter_test.rb