rack-useragent-filter 0.1.4 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/README.rdoc +13 -26
- data/Rakefile +3 -3
- data/VERSION +1 -1
- data/lib/rack/useragent/filter.rb +49 -0
- data/rack-useragent-filter.gemspec +59 -0
- data/test/test_helper.rb +1 -1
- data/test/user_agent_filter_test.rb +4 -4
- metadata +10 -8
- data/lib/rack/user_agent.rb +0 -4
- data/lib/rack/user_agent/filter.rb +0 -46
data/.gitignore
ADDED
data/README.rdoc
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
-
= Rack::UserAgent
|
1
|
+
= Rack::UserAgent::Filter
|
2
2
|
|
3
3
|
Rack Middleware for filtering by user agent
|
4
4
|
|
5
5
|
== Use
|
6
6
|
|
7
|
-
Let's say you don't support IE6 and want to prevent IE6 users from accessing to your
|
7
|
+
Let's say you don't support IE6 and want to prevent IE6 users from accessing to your Rack app. You're in luck. Just add this to your Gemfile:
|
8
|
+
|
9
|
+
gem "rack-useragent-filter", :require => "rack/useragent/filter"
|
8
10
|
|
9
|
-
config.gem "rack-useragent-filter", :lib => "rack/user_agent"
|
10
11
|
|
11
|
-
config.
|
12
|
-
|
12
|
+
in config.ru:
|
13
|
+
|
14
|
+
use Rack::UserAgent::Filter, [
|
15
|
+
["Internet Explorer", "7.0"]
|
13
16
|
]
|
14
17
|
|
15
|
-
And, of course, install the gem:
|
16
|
-
|
17
|
-
$ [sudo] rake gems:install
|
18
18
|
|
19
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
20
|
|
@@ -22,23 +22,15 @@ Done! From now on users with IE version lower than 7.0 will get a nice error mes
|
|
22
22
|
|
23
23
|
Granted! You'll have to add to the config which template to use. In <tt>environment.rb</tt>:
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
], :template => "
|
25
|
+
use Rack::UserAgent::Filter, [
|
26
|
+
["Internet Explorer", "7.0"]
|
27
|
+
], :template => File.dirname(__FILE__) + "/app/views/shared/upgrade.html.erb"
|
28
28
|
|
29
29
|
Then you could show the browser version as part of your message:
|
30
30
|
|
31
31
|
Sorry but <%= "#{@browser.browser} #{@browser.version}" %> is not supported
|
32
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
33
|
|
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
34
|
|
43
35
|
|
44
36
|
<em>- But... what if IE6 user do wants to see how page looks like in his favourite browser?</em>
|
@@ -49,20 +41,15 @@ Next time user hits application, middleware checks request for inclusion of cook
|
|
49
41
|
If it exists unsupported browsers detection is disabled for this user.
|
50
42
|
|
51
43
|
config.middleware.use "Rack::UserAgent::Filter", [
|
52
|
-
|
44
|
+
["Internet Explorer", "7.0"]
|
53
45
|
], :force_with_cookie => "deprecated_browser"
|
54
|
-
|
55
|
-
Enjoy it!
|
56
46
|
|
57
47
|
== Authors
|
58
48
|
|
49
|
+
* Tomasz Mazur <defkode@gmail.com>
|
59
50
|
* Sergio Gil <sgilperez@gmail.com>
|
60
51
|
* Luismi Cavallé <luismi@lmcavalle.com>
|
61
52
|
|
62
53
|
== Contributors
|
63
54
|
|
64
55
|
* 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
CHANGED
@@ -17,9 +17,9 @@ begin
|
|
17
17
|
require 'jeweler'
|
18
18
|
Jeweler::Tasks.new do |gemspec|
|
19
19
|
gemspec.name = "rack-useragent-filter"
|
20
|
-
gemspec.authors = ["Sergio Gil", "Luismi Cavallé"]
|
21
|
-
gemspec.email = "
|
22
|
-
gemspec.homepage = "http://github.com/
|
20
|
+
gemspec.authors = ["Tomasz Mazur", "Sergio Gil", "Luismi Cavallé"]
|
21
|
+
gemspec.email = "defkode@gmail.com"
|
22
|
+
gemspec.homepage = "http://github.com/defkode/rack-useragent"
|
23
23
|
gemspec.summary = "Rack Middleware for filtering by user agent"
|
24
24
|
gemspec.add_dependency('rack', '>= 0.9.1')
|
25
25
|
gemspec.add_dependency('useragent', '>= 0.1.5')
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.7
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'user_agent'
|
2
|
+
require 'erb'
|
3
|
+
require 'tilt'
|
4
|
+
require 'ostruct'
|
5
|
+
|
6
|
+
module Rack
|
7
|
+
module UserAgent
|
8
|
+
class Filter
|
9
|
+
|
10
|
+
def initialize(app, required_browsers = [], options = {})
|
11
|
+
@app = app
|
12
|
+
@browsers = required_browsers.map{ |b| OpenStruct.new(:browser => b[0], :version => b[1]) }
|
13
|
+
@template = options[:template]
|
14
|
+
@force_with_cookie = options[:force_with_cookie]
|
15
|
+
end
|
16
|
+
|
17
|
+
def call(env)
|
18
|
+
request = Rack::Request.new(env)
|
19
|
+
useragent = ::UserAgent.parse(env["HTTP_USER_AGENT"])
|
20
|
+
if !detection_disabled_by_cookie?(request.cookies) && unsupported?(useragent)
|
21
|
+
content = render_page(useragent)
|
22
|
+
[400, {"Content-Type" => "text/html", "Content-Length" => content.length.to_s}, content]
|
23
|
+
else
|
24
|
+
@app.call(env)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def unsupported?(useragent)
|
31
|
+
useragent && @browsers.detect { |browser| useragent < browser }
|
32
|
+
end
|
33
|
+
|
34
|
+
def detection_disabled_by_cookie?(cookies)
|
35
|
+
@force_with_cookie && cookies.keys.include?(@force_with_cookie)
|
36
|
+
end
|
37
|
+
|
38
|
+
def render_page(useragent)
|
39
|
+
if @template && ::File.exists?(@template)
|
40
|
+
@browser = useragent # for the template
|
41
|
+
Tilt.new(@template).render(self)
|
42
|
+
else
|
43
|
+
"Sorry, your browser is not supported. Please upgrade"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{rack-useragent-filter}
|
8
|
+
s.version = "0.1.7"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Tomasz Mazur", "Sergio Gil", "Luismi Cavall\303\251"]
|
12
|
+
s.date = %q{2010-09-03}
|
13
|
+
s.email = %q{defkode@gmail.com}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"README.rdoc"
|
16
|
+
]
|
17
|
+
s.files = [
|
18
|
+
".gitignore",
|
19
|
+
"README.rdoc",
|
20
|
+
"Rakefile",
|
21
|
+
"VERSION",
|
22
|
+
"examples/upgrade.html",
|
23
|
+
"lib/rack/useragent/filter.rb",
|
24
|
+
"rack-useragent-filter.gemspec",
|
25
|
+
"test/fixtures/upgrade.erb",
|
26
|
+
"test/fixtures/upgrade.haml",
|
27
|
+
"test/test_helper.rb",
|
28
|
+
"test/user_agent_filter_test.rb"
|
29
|
+
]
|
30
|
+
s.homepage = %q{http://github.com/defkode/rack-useragent}
|
31
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
32
|
+
s.require_paths = ["lib"]
|
33
|
+
s.rubygems_version = %q{1.3.7}
|
34
|
+
s.summary = %q{Rack Middleware for filtering by user agent}
|
35
|
+
s.test_files = [
|
36
|
+
"test/test_helper.rb",
|
37
|
+
"test/user_agent_filter_test.rb"
|
38
|
+
]
|
39
|
+
|
40
|
+
if s.respond_to? :specification_version then
|
41
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
42
|
+
s.specification_version = 3
|
43
|
+
|
44
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
45
|
+
s.add_runtime_dependency(%q<rack>, [">= 0.9.1"])
|
46
|
+
s.add_runtime_dependency(%q<useragent>, [">= 0.1.5"])
|
47
|
+
s.add_runtime_dependency(%q<tilt>, [">= 1.0.0"])
|
48
|
+
else
|
49
|
+
s.add_dependency(%q<rack>, [">= 0.9.1"])
|
50
|
+
s.add_dependency(%q<useragent>, [">= 0.1.5"])
|
51
|
+
s.add_dependency(%q<tilt>, [">= 1.0.0"])
|
52
|
+
end
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<rack>, [">= 0.9.1"])
|
55
|
+
s.add_dependency(%q<useragent>, [">= 0.1.5"])
|
56
|
+
s.add_dependency(%q<tilt>, [">= 1.0.0"])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
data/test/test_helper.rb
CHANGED
@@ -17,7 +17,7 @@ class UserAgentFilterTest < Test::Unit::TestCase
|
|
17
17
|
setup do
|
18
18
|
def app
|
19
19
|
Rack::Builder.new do
|
20
|
-
use Rack::UserAgent::Filter, [
|
20
|
+
use Rack::UserAgent::Filter, [["Internet Explorer", "7.0"]]
|
21
21
|
run SampleApp.new
|
22
22
|
end
|
23
23
|
end
|
@@ -40,7 +40,7 @@ class UserAgentFilterTest < Test::Unit::TestCase
|
|
40
40
|
setup do
|
41
41
|
def app
|
42
42
|
Rack::Builder.new do
|
43
|
-
use Rack::UserAgent::Filter, [
|
43
|
+
use Rack::UserAgent::Filter, [["Internet Explorer", "7.0"]], :template => File.dirname(__FILE__) + "/fixtures/upgrade.erb"
|
44
44
|
run SampleApp.new
|
45
45
|
end
|
46
46
|
end
|
@@ -58,7 +58,7 @@ class UserAgentFilterTest < Test::Unit::TestCase
|
|
58
58
|
setup do
|
59
59
|
def app
|
60
60
|
Rack::Builder.new do
|
61
|
-
use Rack::UserAgent::Filter, [
|
61
|
+
use Rack::UserAgent::Filter, [["Internet Explorer", "7.0"]], :template => File.dirname(__FILE__) + "/fixtures/upgrade.haml"
|
62
62
|
run SampleApp.new
|
63
63
|
end
|
64
64
|
end
|
@@ -77,7 +77,7 @@ class UserAgentFilterTest < Test::Unit::TestCase
|
|
77
77
|
setup do
|
78
78
|
def app
|
79
79
|
Rack::Builder.new do
|
80
|
-
use Rack::UserAgent::Filter, [
|
80
|
+
use Rack::UserAgent::Filter, [["Internet Explorer", "7.0"]], :force_with_cookie => "browser"
|
81
81
|
run SampleApp.new
|
82
82
|
end
|
83
83
|
end
|
metadata
CHANGED
@@ -1,22 +1,23 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-useragent-filter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 7
|
10
|
+
version: 0.1.7
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
|
+
- Tomasz Mazur
|
13
14
|
- Sergio Gil
|
14
15
|
- "Luismi Cavall\xC3\xA9"
|
15
16
|
autorequire:
|
16
17
|
bindir: bin
|
17
18
|
cert_chain: []
|
18
19
|
|
19
|
-
date: 2010-
|
20
|
+
date: 2010-09-03 00:00:00 +02:00
|
20
21
|
default_executable:
|
21
22
|
dependencies:
|
22
23
|
- !ruby/object:Gem::Dependency
|
@@ -68,7 +69,7 @@ dependencies:
|
|
68
69
|
type: :runtime
|
69
70
|
version_requirements: *id003
|
70
71
|
description:
|
71
|
-
email:
|
72
|
+
email: defkode@gmail.com
|
72
73
|
executables: []
|
73
74
|
|
74
75
|
extensions: []
|
@@ -76,18 +77,19 @@ extensions: []
|
|
76
77
|
extra_rdoc_files:
|
77
78
|
- README.rdoc
|
78
79
|
files:
|
80
|
+
- .gitignore
|
79
81
|
- README.rdoc
|
80
82
|
- Rakefile
|
81
83
|
- VERSION
|
82
84
|
- examples/upgrade.html
|
83
|
-
- lib/rack/
|
84
|
-
-
|
85
|
+
- lib/rack/useragent/filter.rb
|
86
|
+
- rack-useragent-filter.gemspec
|
85
87
|
- test/fixtures/upgrade.erb
|
86
88
|
- test/fixtures/upgrade.haml
|
87
89
|
- test/test_helper.rb
|
88
90
|
- test/user_agent_filter_test.rb
|
89
91
|
has_rdoc: true
|
90
|
-
homepage: http://github.com/
|
92
|
+
homepage: http://github.com/defkode/rack-useragent
|
91
93
|
licenses: []
|
92
94
|
|
93
95
|
post_install_message:
|
data/lib/rack/user_agent.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
require 'user_agent'
|
2
|
-
require 'erb'
|
3
|
-
require 'tilt'
|
4
|
-
require 'ostruct'
|
5
|
-
|
6
|
-
module Rack::UserAgent
|
7
|
-
class Filter
|
8
|
-
def initialize(app, browsers = [], options = {})
|
9
|
-
@app = app
|
10
|
-
@browsers = browsers
|
11
|
-
@template = options[:template]
|
12
|
-
@force_with_cookie = options[:force_with_cookie]
|
13
|
-
end
|
14
|
-
|
15
|
-
def call(env)
|
16
|
-
request = Rack::Request.new(env)
|
17
|
-
browser = UserAgent.parse(env["HTTP_USER_AGENT"]) if env["HTTP_USER_AGENT"]
|
18
|
-
if !detection_disabled_by_cookie?(request.cookies) && unsupported?(browser)
|
19
|
-
content = render_page(browser)
|
20
|
-
[400, {"Content-Type" => "text/html", "Content-Length" => content.length.to_s}, content]
|
21
|
-
else
|
22
|
-
@app.call(env)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
private
|
27
|
-
|
28
|
-
def unsupported?(browser)
|
29
|
-
browser && @browsers.map{ |hash| browser < OpenStruct.new(hash) }.any?
|
30
|
-
end
|
31
|
-
|
32
|
-
def detection_disabled_by_cookie?(cookies)
|
33
|
-
@force_with_cookie && cookies.keys.include?(@force_with_cookie)
|
34
|
-
end
|
35
|
-
|
36
|
-
def render_page(browser)
|
37
|
-
if @template && File.exists?(@template)
|
38
|
-
@browser = browser # for the template
|
39
|
-
Tilt.new(@template).render(self)
|
40
|
-
else
|
41
|
-
"Sorry, your browser is not supported. Please upgrade"
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
46
|
-
end
|