browser_detect 0.0.1 → 0.0.4
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/.gitignore +4 -0
- data/.yardopts +5 -0
- data/README.md +77 -0
- data/VERSION +1 -0
- data/browser_detect.gemspec +12 -11
- data/doc/BrowserDetect.html +456 -0
- data/doc/BrowserDetectHelperTest.html +207 -0
- data/doc/BrowserDetectMock.html +313 -0
- data/doc/BrowserDetectMock/req;.html +151 -0
- data/doc/Test.html +157 -0
- data/doc/Test/Unit.html +160 -0
- data/doc/Test/Unit/TestCase.html +270 -0
- data/doc/Test/browser_detect_test_rb.html +54 -0
- data/doc/Test/test_helper_rb.html +58 -0
- data/doc/_index.html +94 -0
- data/doc/class_list.html +36 -0
- data/doc/css/common.css +1 -0
- data/doc/css/full_list.css +53 -0
- data/doc/css/style.css +307 -0
- data/doc/file.README.html +144 -0
- data/doc/file_list.html +38 -0
- data/doc/frames.html +13 -0
- data/doc/index.html +144 -0
- data/doc/init_rb.html +54 -0
- data/doc/js/app.js +202 -0
- data/doc/js/full_list.js +149 -0
- data/doc/js/jquery.js +154 -0
- data/doc/lib/version_rb.html +52 -0
- data/doc/method_list.html +67 -0
- data/doc/top-level-namespace.html +88 -0
- data/init.rb +5 -0
- data/lib/browser_detect.rb +73 -2
- data/lib/railtie.rb +13 -0
- data/test/browser_detect_test.rb +55 -0
- data/test/fixtures/user_agents.yml +47 -0
- data/test/test_helper.rb +45 -0
- metadata +43 -10
- data/README +0 -0
- data/lib/browser_detect/browser_detect_helper.rb +0 -43
- data/lib/browser_detect/version.rb +0 -3
data/init.rb
ADDED
data/lib/browser_detect.rb
CHANGED
@@ -1,2 +1,73 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module BrowserDetect
|
2
|
+
#Browser groupings
|
3
|
+
def browser_is? name
|
4
|
+
name = name.to_s.strip
|
5
|
+
return true if browser_name == name
|
6
|
+
return true if name == 'mozilla' && browser_name == 'gecko'
|
7
|
+
return true if name == 'ie' && browser_name.index('ie')
|
8
|
+
return true if name == 'webkit' && %w{safari chrome iphone ipad ipod}.include?(browser_name)
|
9
|
+
return true if name == 'ios' && %w{iphone ipad ipod}.include?(browser_name)
|
10
|
+
return true if name == 'robots' && %w{googlebot msnbot yahoobot}.include?(browser_name)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Returns the user agent string as determined by the plugin
|
14
|
+
def browser_name
|
15
|
+
@browser_name ||= begin
|
16
|
+
if ua.index('msie') && !ua.index('opera') && !ua.index('webtv')
|
17
|
+
'ie'+ua[ua.index('msie')+5].chr
|
18
|
+
elsif ua.index('gecko/')
|
19
|
+
'gecko'
|
20
|
+
elsif ua.index('opera')
|
21
|
+
'opera'
|
22
|
+
elsif ua.index('konqueror')
|
23
|
+
'konqueror'
|
24
|
+
elsif ua.index('ipod')
|
25
|
+
'ipod'
|
26
|
+
elsif ua.index('ipad')
|
27
|
+
'ipad'
|
28
|
+
elsif ua.index('iphone')
|
29
|
+
'iphone'
|
30
|
+
elsif ua.index('chrome/')
|
31
|
+
'chrome'
|
32
|
+
elsif ua.index('applewebkit/')
|
33
|
+
'safari'
|
34
|
+
elsif ua.index('googlebot/')
|
35
|
+
'googlebot'
|
36
|
+
elsif ua.index('msnbot')
|
37
|
+
'msnbot'
|
38
|
+
elsif ua.index('yahoo! slurp')
|
39
|
+
'yahoobot'
|
40
|
+
#Everything thinks it's mozilla, so this goes last
|
41
|
+
elsif ua.index('mozilla/')
|
42
|
+
'gecko'
|
43
|
+
else
|
44
|
+
'unknown'
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Determine the version of webkit.
|
51
|
+
# Useful for determing rendering capabilties
|
52
|
+
def browser_webkit_version
|
53
|
+
if browser_is? 'webkit'
|
54
|
+
match = ua.match(%r{\bapplewebkit/([\d\.]+)\b})
|
55
|
+
if (match)
|
56
|
+
match[1].to_f
|
57
|
+
else
|
58
|
+
nil
|
59
|
+
end
|
60
|
+
else
|
61
|
+
nil
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
#Gather the user agent and store it for use.
|
66
|
+
def ua
|
67
|
+
@ua ||= begin
|
68
|
+
request.env['HTTP_USER_AGENT'].downcase
|
69
|
+
rescue
|
70
|
+
''
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/lib/railtie.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'browser_detect'
|
2
|
+
require 'rails'
|
3
|
+
|
4
|
+
# lib/browser_detect/railtie.rb
|
5
|
+
module BrowserDetectHelper
|
6
|
+
class Railtie < Rails::Railtie
|
7
|
+
railtie_name :browser_detect
|
8
|
+
initializer "browser_detect.configure_rails_initialization" do
|
9
|
+
ApplicationController.send(:include, BrowserDetect)
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__)+'/test_helper')
|
2
|
+
require '../lib/browser_detect'
|
3
|
+
|
4
|
+
class BrowserDetectTest < Test::Unit::TestCase
|
5
|
+
fixtures :user_agents
|
6
|
+
|
7
|
+
def mock_browser(ua=nil)
|
8
|
+
BrowserDetectMock.new(ua)
|
9
|
+
end
|
10
|
+
|
11
|
+
must "deal with nil user agent gracefully" do
|
12
|
+
assert_nothing_raised do
|
13
|
+
mock_browser.browser_name
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
must "correctly mock a user agent string" do
|
18
|
+
mock = mock_browser("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)")
|
19
|
+
assert_equal("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)", mock.request.env['HTTP_USER_AGENT'])
|
20
|
+
end
|
21
|
+
|
22
|
+
must "identify googlebot" do
|
23
|
+
mock = mock_browser("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)")
|
24
|
+
assert_equal('googlebot', mock.browser_name)
|
25
|
+
end
|
26
|
+
|
27
|
+
must "correctly identify known user agents" do
|
28
|
+
user_agents(:browsers).each do |browser|
|
29
|
+
mock = mock_browser(browser['ua'])
|
30
|
+
browser['name'].each do |name|
|
31
|
+
assert(mock.browser_is?(name), "Browser '#{browser['nickname']}' did not match name '#{name}'!")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class BrowserDetectMock
|
38
|
+
include BrowserDetect
|
39
|
+
|
40
|
+
def initialize(user_agent=nil)
|
41
|
+
@user_agent = user_agent
|
42
|
+
end
|
43
|
+
|
44
|
+
def request
|
45
|
+
@req ||= mock_req
|
46
|
+
end
|
47
|
+
|
48
|
+
def mock_req
|
49
|
+
req = Object.new
|
50
|
+
metaclass = class << req; self; end
|
51
|
+
user_agent = @user_agent
|
52
|
+
metaclass.send :define_method, :env, Proc.new { {'HTTP_USER_AGENT' => user_agent} }
|
53
|
+
req
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
browsers:
|
2
|
+
- nickname: ie6
|
3
|
+
ua: "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
|
4
|
+
name:
|
5
|
+
- ie
|
6
|
+
- ie6
|
7
|
+
- nickname: ie7
|
8
|
+
ua: "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"
|
9
|
+
name:
|
10
|
+
- ie
|
11
|
+
- ie7
|
12
|
+
- nickname: ie8
|
13
|
+
ua: "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"
|
14
|
+
name:
|
15
|
+
- ie
|
16
|
+
- ie8
|
17
|
+
|
18
|
+
- nickname: googlebot
|
19
|
+
ua: "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
|
20
|
+
name:
|
21
|
+
- googlebot
|
22
|
+
- robots
|
23
|
+
|
24
|
+
- nickname: msnbot
|
25
|
+
ua: "msnbot/1.1 (+http://search.msn.com/msnbot.htm)"
|
26
|
+
name:
|
27
|
+
- msnbot
|
28
|
+
- robots
|
29
|
+
|
30
|
+
- nickname: yahoobot
|
31
|
+
ua: "Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)"
|
32
|
+
name:
|
33
|
+
- yahoobot
|
34
|
+
- robots
|
35
|
+
|
36
|
+
- nickname: iphone3
|
37
|
+
ua: "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16"
|
38
|
+
name:
|
39
|
+
- iphone
|
40
|
+
- ios
|
41
|
+
- webkit
|
42
|
+
|
43
|
+
- nickname: safari4_1
|
44
|
+
ua: "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_7; en-us) AppleWebKit/533.4 (KHTML, like Gecko) Version/4.1 Safari/533.4"
|
45
|
+
name:
|
46
|
+
- safari
|
47
|
+
- webkit
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + '/../lib')
|
6
|
+
|
7
|
+
module Test::Unit
|
8
|
+
# @ see http://gist.github.com/316780
|
9
|
+
# Used to fix a minor minitest/unit incompatibility in flexmock
|
10
|
+
# AssertionFailedError = Class.new(StandardError)
|
11
|
+
|
12
|
+
class TestCase
|
13
|
+
|
14
|
+
def self.must(name, &block)
|
15
|
+
test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
|
16
|
+
defined = instance_method(test_name) rescue false
|
17
|
+
raise "#{test_name} is already defined in #{self}" if defined
|
18
|
+
if block_given?
|
19
|
+
define_method(test_name, &block)
|
20
|
+
else
|
21
|
+
define_method(test_name) do
|
22
|
+
flunk "No implementation provided for #{name}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# @see http://push.cx/2007/fixtures-in-ruby-unit-tests
|
31
|
+
class Test::Unit::TestCase
|
32
|
+
@@fixtures = {}
|
33
|
+
def self.fixtures *args
|
34
|
+
[args].flatten.each do |fixture|
|
35
|
+
self.class_eval do
|
36
|
+
# add a method name for this fixture type
|
37
|
+
define_method(fixture) do |item|
|
38
|
+
# load and cache the YAML
|
39
|
+
@@fixtures[fixture] ||= YAML::load_file("./fixtures/#{fixture.to_s}.yml")
|
40
|
+
@@fixtures[fixture][item.to_s]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
metadata
CHANGED
@@ -5,16 +5,19 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
|
+
- rlivsey
|
12
13
|
- faunzy
|
14
|
+
- tmlee
|
15
|
+
- ggilder
|
13
16
|
autorequire:
|
14
17
|
bindir: bin
|
15
18
|
cert_chain: []
|
16
19
|
|
17
|
-
date: 2010-
|
20
|
+
date: 2010-11-08 00:00:00 -05:00
|
18
21
|
default_executable:
|
19
22
|
dependencies:
|
20
23
|
- !ruby/object:Gem::Dependency
|
@@ -34,7 +37,7 @@ dependencies:
|
|
34
37
|
version: 1.0.0.rc.6
|
35
38
|
type: :development
|
36
39
|
version_requirements: *id001
|
37
|
-
description:
|
40
|
+
description: "\tSimple rails browser detection based on original plugin by Richard Livsey\"\n"
|
38
41
|
email: []
|
39
42
|
|
40
43
|
executables: []
|
@@ -44,16 +47,46 @@ extensions: []
|
|
44
47
|
extra_rdoc_files: []
|
45
48
|
|
46
49
|
files:
|
50
|
+
- .gitignore
|
51
|
+
- .yardopts
|
47
52
|
- Gemfile
|
48
|
-
- README
|
53
|
+
- README.md
|
49
54
|
- Rakefile
|
50
|
-
-
|
55
|
+
- VERSION
|
51
56
|
- browser_detect.gemspec
|
57
|
+
- doc/BrowserDetect.html
|
58
|
+
- doc/BrowserDetectHelperTest.html
|
59
|
+
- doc/BrowserDetectMock.html
|
60
|
+
- doc/BrowserDetectMock/req;.html
|
61
|
+
- doc/Test.html
|
62
|
+
- doc/Test/Unit.html
|
63
|
+
- doc/Test/Unit/TestCase.html
|
64
|
+
- doc/Test/browser_detect_test_rb.html
|
65
|
+
- doc/Test/test_helper_rb.html
|
66
|
+
- doc/_index.html
|
67
|
+
- doc/class_list.html
|
68
|
+
- doc/css/common.css
|
69
|
+
- doc/css/full_list.css
|
70
|
+
- doc/css/style.css
|
71
|
+
- doc/file.README.html
|
72
|
+
- doc/file_list.html
|
73
|
+
- doc/frames.html
|
74
|
+
- doc/index.html
|
75
|
+
- doc/init_rb.html
|
76
|
+
- doc/js/app.js
|
77
|
+
- doc/js/full_list.js
|
78
|
+
- doc/js/jquery.js
|
79
|
+
- doc/lib/version_rb.html
|
80
|
+
- doc/method_list.html
|
81
|
+
- doc/top-level-namespace.html
|
82
|
+
- init.rb
|
52
83
|
- lib/browser_detect.rb
|
53
|
-
- lib/
|
54
|
-
-
|
84
|
+
- lib/railtie.rb
|
85
|
+
- test/browser_detect_test.rb
|
86
|
+
- test/fixtures/user_agents.yml
|
87
|
+
- test/test_helper.rb
|
55
88
|
has_rdoc: true
|
56
|
-
homepage: http://
|
89
|
+
homepage: http://github.com/traction/browser_detect
|
57
90
|
licenses: []
|
58
91
|
|
59
92
|
post_install_message:
|
@@ -85,6 +118,6 @@ rubyforge_project: browser_detect
|
|
85
118
|
rubygems_version: 1.3.7
|
86
119
|
signing_key:
|
87
120
|
specification_version: 3
|
88
|
-
summary:
|
121
|
+
summary: A simple rails browser detection plugin
|
89
122
|
test_files: []
|
90
123
|
|
data/README
DELETED
File without changes
|
@@ -1,43 +0,0 @@
|
|
1
|
-
module BrowserDetect
|
2
|
-
module BrowserDetectHelper
|
3
|
-
|
4
|
-
def browser_is? name
|
5
|
-
|
6
|
-
name = name.to_s.strip
|
7
|
-
|
8
|
-
return true if browser_name == name
|
9
|
-
return true if name == 'mozilla' && browser_name == 'gecko'
|
10
|
-
return true if name == 'ie' && browser_name.index('ie')
|
11
|
-
return true if name == 'webkit' && browser_name == ('safari' or 'chrome' or 'iphone' or 'ipad')
|
12
|
-
end
|
13
|
-
|
14
|
-
def browser_name
|
15
|
-
@browser_name ||= begin
|
16
|
-
|
17
|
-
ua = request.env['HTTP_USER_AGENT'].downcase
|
18
|
-
|
19
|
-
if ua.index('msie') && !ua.index('opera') && !ua.index('webtv')
|
20
|
-
'ie'+ua[ua.index('msie')+5].chr
|
21
|
-
elsif ua.index('gecko/')
|
22
|
-
'gecko'
|
23
|
-
elsif ua.index('opera')
|
24
|
-
'opera'
|
25
|
-
elsif ua.index('konqueror')
|
26
|
-
'konqueror'
|
27
|
-
elsif ua.index('ipad')
|
28
|
-
'ipad'
|
29
|
-
elsif ua.index('iphone')
|
30
|
-
'iphone'
|
31
|
-
elsif ua.index('chrome/')
|
32
|
-
'chrome'
|
33
|
-
elsif ua.index('applewebkit/')
|
34
|
-
'safari'
|
35
|
-
elsif ua.index('mozilla/')
|
36
|
-
'gecko'
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
end
|
43
|
-
end
|