rack-useragent 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +3 -0
- data/README.rdoc +58 -0
- data/Rakefile +13 -0
- data/lib/rack/user_agent/filter.rb +46 -0
- data/lib/rack/user_agent/version.rb +5 -0
- data/lib/rack/user_agent.rb +6 -0
- data/rack-useragent.gemspec +23 -0
- data/test/public/upgrade.es.html +1 -0
- data/test/public/upgrade.html +1 -0
- data/test/upgrade.erb +1 -0
- data/test/user_agent_filter_test.rb +81 -0
- metadata +126 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
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/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
desc 'Default: run unit tests.'
|
5
|
+
task :default => :test
|
6
|
+
|
7
|
+
require 'rake/testtask'
|
8
|
+
desc 'Run all tests'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
@@ -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,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "rack/user_agent/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "rack-useragent"
|
7
|
+
s.version = Rack::UserAgent::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Sergio Gil", "Luismi Cavallé"]
|
10
|
+
s.email = ["team@bebanjo.com"]
|
11
|
+
s.homepage = "http://github.com/bebanjo/rack-useragent"
|
12
|
+
s.summary = %q{Rack Middleware for filtering by user agent}
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
s.add_dependency 'rack', '>= 0.9.1'
|
20
|
+
s.add_dependency 'useragent'
|
21
|
+
|
22
|
+
s.add_development_dependency 'mocha'
|
23
|
+
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,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-useragent
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Sergio Gil
|
14
|
+
- "Luismi Cavall\xC3\xA9"
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2011-04-08 00:00:00 +02:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
name: rack
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 57
|
31
|
+
segments:
|
32
|
+
- 0
|
33
|
+
- 9
|
34
|
+
- 1
|
35
|
+
version: 0.9.1
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id001
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: useragent
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 3
|
47
|
+
segments:
|
48
|
+
- 0
|
49
|
+
version: "0"
|
50
|
+
type: :runtime
|
51
|
+
version_requirements: *id002
|
52
|
+
- !ruby/object:Gem::Dependency
|
53
|
+
name: mocha
|
54
|
+
prerelease: false
|
55
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
type: :development
|
65
|
+
version_requirements: *id003
|
66
|
+
description:
|
67
|
+
email:
|
68
|
+
- team@bebanjo.com
|
69
|
+
executables: []
|
70
|
+
|
71
|
+
extensions: []
|
72
|
+
|
73
|
+
extra_rdoc_files: []
|
74
|
+
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- README.rdoc
|
79
|
+
- Rakefile
|
80
|
+
- lib/rack/user_agent.rb
|
81
|
+
- lib/rack/user_agent/filter.rb
|
82
|
+
- lib/rack/user_agent/version.rb
|
83
|
+
- rack-useragent.gemspec
|
84
|
+
- test/public/upgrade.es.html
|
85
|
+
- test/public/upgrade.html
|
86
|
+
- test/upgrade.erb
|
87
|
+
- test/user_agent_filter_test.rb
|
88
|
+
has_rdoc: true
|
89
|
+
homepage: http://github.com/bebanjo/rack-useragent
|
90
|
+
licenses: []
|
91
|
+
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
hash: 3
|
103
|
+
segments:
|
104
|
+
- 0
|
105
|
+
version: "0"
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
hash: 3
|
112
|
+
segments:
|
113
|
+
- 0
|
114
|
+
version: "0"
|
115
|
+
requirements: []
|
116
|
+
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 1.6.2
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: Rack Middleware for filtering by user agent
|
122
|
+
test_files:
|
123
|
+
- test/public/upgrade.es.html
|
124
|
+
- test/public/upgrade.html
|
125
|
+
- test/upgrade.erb
|
126
|
+
- test/user_agent_filter_test.rb
|