browser_detect 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  *.gemspec
2
2
  .svn
3
3
  .yardoc/
4
- .bundle/
4
+ .bundle/
5
+ .rvmrc
data/History.txt ADDED
@@ -0,0 +1,29 @@
1
+ === 0.0.5 2011-10-6
2
+
3
+ * 2 Bugs fixed:
4
+ * Fixed Rails 3 Railtie support (thanks to eric1234)
5
+ * Fix detection of Safari
6
+
7
+ === 0.0.4 2010-11-07
8
+
9
+ * 1 major enhancement:
10
+ * Initial support for Rails 3 via Railtie
11
+
12
+ === 0.0.3 2010-11-03
13
+
14
+ * 2 major enhancements:
15
+ * Moved documentation from RDoc to Yard
16
+ * Changed Module name to BrowserDetect
17
+
18
+ === 0.0.2 2010-11-01
19
+
20
+ * 4 major enhancements:
21
+ * fixed webkit detection and added webkit version querying
22
+ * merge changes with tmlee
23
+ * added ios name and changed browser_name to return 'unknown' if nothing matches
24
+ * corrected issues with test mocking, added tests for robots
25
+
26
+ === 0.0.1 2010-10-21
27
+
28
+ * 1 major enhancement:
29
+ * Added chrome, iphone, ipad to list of browsers and added support for these browsers to the browser_is? method
data/Manifest.txt ADDED
@@ -0,0 +1,11 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/browser_detect_test.rb
7
+ script/console
8
+ script/destroy
9
+ script/generate
10
+ test/test_browser_detect_test.rb
11
+ test/test_helper.rb
data/README.md CHANGED
@@ -1,77 +1,69 @@
1
- Browser Detect
2
- ==============
1
+ # Browser Detect
3
2
  _It's like a crystal ball for user agents._
4
3
 
5
- Detect it.
6
- ----------
4
+ Browser Detect identifies the client browser using the user agent string that was supplied in the page request. Browser Detect searches the user agent string for any string you provide, and it also supports some special groupings and shortcuts:
7
5
 
8
- Detects the client browser using the user agent string that was used to make the page request.
6
+ * ie (Any version of IE, excluding browsers like Opera and WebTV that identify themselves as IE)
7
+ * ie{integer} (A specific major version of IE — "ie6", "ie7", "ie8", etc.)
8
+ * robot (Google bot, MSN bot, Yahoo! bot)
9
+ * ios (iPhone, iPod, iPad)
10
+ * webkit (Any WebKit based browser)
11
+ * mobile (Any modern mobile browser — iOS, Android, Palm webOS)
9
12
 
10
- Browser Detect currently supports the following browsers and platforms:
13
+ ## Install it.
11
14
 
12
- Browsers:
15
+ Using Bundler, all you need to do is add the source to your Gemfile:
13
16
 
14
- * ie
15
- * gecko
16
- * konqueror
17
- * opera
18
- * ipad
19
- * ipod
20
- * iphone
21
- * chrome
22
- * safari
17
+ gem "browser_detect"
23
18
 
24
- Robots:
19
+ Then run:
25
20
 
26
- * googlebot
27
- * msnbot
28
- * yahoo!
21
+ bundle install
29
22
 
30
- Operating Systems:
23
+ or, install it as a plugin using Rails 2:
31
24
 
32
- * TODO
25
+ script/plugin install git://github.com/traction/browser_detect.git
33
26
 
34
- Install it:
35
- -----------
27
+ ## Wield it.
36
28
 
37
- Using Bundler, all you need to do is add the source to your Gemfile:
29
+ To check if a particular browser made the request, use browser_is?(name)
38
30
 
39
- gem "browser_detect", :require => "browser_detect", :git => 'git://github.com/traction/browser_detect.git'
31
+ def index
32
+ if browser_is?("chrome")
33
+ # load some chrome-specific content
34
+ end
35
+ end
40
36
 
41
- Then run:
37
+ or in a view:
42
38
 
43
- bundle install
44
-
45
- or, install it as a plugin:
39
+ <%= browser_is?(:chrome) ? "secrets" : "buzz off" %>
46
40
 
47
- script/plugin install git://github.com/traction/browser_detect.git
48
-
49
- Wield it.
50
- ---------
41
+ Don't forget you can use the special groupings listed above to target IE, robots, iOS, etc.
42
+
43
+ <%= stylesheet_link_tag "ugly_styles" if browser_is?('ie') %>
44
+ <%= stylesheet_link_tag "even_uglier_styles" if browser_is?('ie6') %>
45
+ <%= render "seo_spam_content" if browser_is?('robot') %>
51
46
 
52
- To check the type of browser, use browser_name
53
- @browser_name will return either 'ie', 'gecko', 'opera', 'konqueror', 'ipod', 'ipad', 'iphone', 'chrome', 'safari', 'googlebot', 'msnbot', 'yahoobot'
47
+ There's also a convenience method `browser_is_mobile?` which is just a shortcut to `browser_is?('mobile')`
54
48
 
55
49
  def index
56
- @browser_name = browser_name
50
+ if browser_is_mobile?
51
+ # redirect to the mobile site
52
+ end
57
53
  end
58
54
 
59
- To check if a particular browser made the request, use browser_name?(name)
60
- If the browser used is chrome, it will return True
55
+ For WebKit-based browsers, you can also check the WebKit version using `browser_webkit_version`. This can be useful for determining what rendering capabilities the browser has. For instance, early versions of Mobile WebKit crash intermittently when you use multiple faces or weights of web fonts. So you might prevent your web fonts from rendering on old WebKit versions:
61
56
 
62
- def index
63
- @browser_type = browser_is?("chrome")
64
- end
65
-
66
- or in a view using ERB:
67
-
68
- <%= browser_is?(:chrome) ? "secrets" : "buzz off" %>
69
-
70
- Or choose from the following groupings:
57
+ <%= stylesheet_link_tag "fonts" unless (browser_is?('ios') and browser_webkit_version < 532) %>
58
+
59
+ ## Contribute!
60
+
61
+ Additions to the text fixtures (list of user agent strings for testing) are always welcome, as are new definitions of browser groupings. Please fork and submit a pull request!
62
+
63
+ ## Authors
71
64
 
72
- * mozilla
73
- * ie
74
- * webkit
75
- * robots
76
-
65
+ Originally based on work by [rlivsey](http://github.com/rlivsey). Current contributors:
77
66
 
67
+ * [faunzy](http://github.com/faunzy)
68
+ * [ggilder](http://github.com/ggilder)
69
+ * [tmlee](http://github.com/tmlee)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.0.5
@@ -1,73 +1,56 @@
1
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
2
+ # Define browser groupings (mobile, robots, etc.)
3
+ # Also define complex queries like IE where we weed out user agents that pose as IE
4
+ # The default case just checks if the user agent contains the query string
5
+ def browser_is? query
6
+ query = query.to_s.strip.downcase
7
+ result = case query
8
+ when /^ie(\d+)$/
9
+ ua.index("msie #{$1}") && !ua.index('opera') && !ua.index('webtv')
10
+ when 'ie'
11
+ ua.match(/msie \d/) && !ua.index('opera') && !ua.index('webtv')
12
+ when 'yahoobot'
13
+ ua.index('yahoo! slurp')
14
+ when 'mozilla'
15
+ ua.index('gecko') || ua.index('mozilla')
16
+ when 'webkit'
17
+ ua.match(/webkit|iphone|ipad|ipod/)
18
+ when 'safari'
19
+ ua.index('safari') && !ua.index('chrome')
20
+ when 'ios'
21
+ ua.match(/iphone|ipad|ipod/)
22
+ when /^robot(s?)$/
23
+ ua.match(/googlebot|msnbot/) || browser_is?('yahoobot')
24
+ when 'mobile'
25
+ browser_is?('ios') || ua.match(/android|webos|mobile/)
26
+ else
27
+ ua.index(query)
28
+ end
29
+ not (result.nil? || result == false)
30
+ end
12
31
 
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
32
+ # Determine the version of webkit.
33
+ # Useful for determing rendering capabilities
34
+ # For instance, Mobile Webkit versions lower than 532 don't handle webfonts very well (intermittent crashing when using multiple faces/weights)
35
+ def browser_webkit_version
36
+ if browser_is? 'webkit'
37
+ match = ua.match(%r{\bapplewebkit/([\d\.]+)\b})
38
+ match[1].to_f if (match)
39
+ end or 0
40
+ end
46
41
 
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
42
+ def browser_is_mobile?
43
+ browser_is? 'mobile'
44
+ end
64
45
 
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
46
+ # Gather the user agent and store it for use.
47
+ def ua
48
+ @ua ||= begin
49
+ request.env['HTTP_USER_AGENT'].downcase
50
+ rescue
51
+ ''
52
+ end
53
+ end
54
+ end
55
+
56
+ require 'railtie' if defined? Rails
data/lib/railtie.rb CHANGED
@@ -1,10 +1,6 @@
1
- require 'browser_detect'
2
- require 'rails'
3
-
4
1
  # lib/browser_detect/railtie.rb
5
2
  module BrowserDetectHelper
6
3
  class Railtie < Rails::Railtie
7
- railtie_name :browser_detect
8
4
  initializer "browser_detect.configure_rails_initialization" do
9
5
  ApplicationController.send(:include, BrowserDetect)
10
6
  end
@@ -2,54 +2,76 @@ require File.expand_path(File.dirname(__FILE__)+'/test_helper')
2
2
  require '../lib/browser_detect'
3
3
 
4
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
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_is?('something')
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(mock.browser_is?('googlebot'))
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
+
36
+ must "correctly identify webkit versions" do
37
+ mock = mock_browser("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")
38
+ assert(mock.browser_is?('webkit'))
39
+ assert_equal(533.4, mock.browser_webkit_version)
40
+ end
41
+
42
+ must "not identify chrome as safari" do
43
+ mock = mock_browser("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.81 Safari/535.2")
44
+ assert(mock.browser_is?('webkit'))
45
+ assert_equal(false, mock.browser_is?('safari'))
46
+ assert(mock.browser_is?('chrome'))
47
+
48
+ assert_equal(535.2, mock.browser_webkit_version)
49
+ end
50
+
51
+ must "handle strange user agent strings for iOS apps" do
52
+ mock = mock_browser("Times/(null) (iPad; http://www.acrylicapps.com/pulp/)")
53
+ assert(mock.browser_is?('ios'))
54
+ assert(mock.browser_is?('webkit'))
55
+ assert_equal(0, mock.browser_webkit_version)
56
+ end
35
57
  end
36
58
 
37
59
  class BrowserDetectMock
38
60
  include BrowserDetect
39
61
 
40
- def initialize(user_agent=nil)
41
- @user_agent = user_agent
42
- end
62
+ def initialize(user_agent=nil)
63
+ @user_agent = user_agent
64
+ end
43
65
 
44
- def request
45
- @req ||= mock_req
46
- end
66
+ def request
67
+ @req ||= mock_req
68
+ end
47
69
 
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
70
+ def mock_req
71
+ req = Object.new
72
+ metaclass = class << req; self; end
73
+ user_agent = @user_agent
74
+ metaclass.send :define_method, :env, Proc.new { {'HTTP_USER_AGENT' => user_agent} }
75
+ req
76
+ end
55
77
  end
@@ -26,6 +26,7 @@ browsers:
26
26
  name:
27
27
  - msnbot
28
28
  - robots
29
+ - robot
29
30
 
30
31
  - nickname: yahoobot
31
32
  ua: "Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)"
@@ -33,15 +34,72 @@ browsers:
33
34
  - yahoobot
34
35
  - robots
35
36
 
36
- - nickname: iphone3
37
+ - nickname: "iphone 3"
37
38
  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
39
  name:
39
40
  - iphone
40
41
  - ios
41
42
  - webkit
43
+ - mobile
44
+ - safari
45
+
46
+ - nickname: "iphone 4"
47
+ ua: "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7"
48
+ name:
49
+ - iphone
50
+ - ios
51
+ - webkit
52
+ - mobile
53
+ - safari
42
54
 
43
- - nickname: safari4_1
55
+ - nickname: "safari 4.1"
44
56
  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
57
  name:
46
58
  - safari
47
- - webkit
59
+ - webkit
60
+
61
+ - nickname: "safari 4.0.4"
62
+ ua: "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/531.21.11 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10"
63
+ name:
64
+ - safari
65
+ - webkit
66
+
67
+ - nickname: "Android G1"
68
+ ua: "Mozilla/5.0 (Linux; U; Android 1.1; en-gb; dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2"
69
+ name:
70
+ - webkit
71
+ - safari
72
+ - mobile
73
+ - android
74
+
75
+ - nickname: "Android Emulator"
76
+ ua: "Mozilla/5.0 (Linux; U; Android 1.0; en-us; generic) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2"
77
+ name:
78
+ - webkit
79
+ - safari
80
+ - mobile
81
+ - android
82
+
83
+ - nickname: "Android Nexus"
84
+ ua: "Mozilla/5.0 (Linux; U; Android 2.1; en-us; Nexus One Build/ERD62) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17"
85
+ name:
86
+ - webkit
87
+ - safari
88
+ - mobile
89
+ - android
90
+
91
+ - nickname: "Palm Pre"
92
+ ua: "Mozilla/5.0 (webOS/1.4.0; U; en-US) AppleWebKit/532.2 (KHTML, like Gecko) Version/1.0 Safari/532.2 Pre/1.1"
93
+ name:
94
+ - webkit
95
+ - safari
96
+ - webos
97
+ - mobile
98
+
99
+ - nickname: "Palm Pixi"
100
+ ua: "Mozilla/5.0 (webOS/1.4.0; U; en-US) AppleWebKit/532.2 (KHTML, like Gecko) Version/1.0 Safari/532.2 Pixi/1.1"
101
+ name:
102
+ - webkit
103
+ - safari
104
+ - webos
105
+ - mobile
metadata CHANGED
@@ -1,14 +1,10 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: browser_detect
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 4
9
- version: 0.0.4
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - rlivsey
13
9
  - faunzy
14
10
  - tmlee
@@ -16,40 +12,31 @@ authors:
16
12
  autorequire:
17
13
  bindir: bin
18
14
  cert_chain: []
19
-
20
- date: 2010-11-08 00:00:00 -05:00
21
- default_executable:
22
- dependencies:
23
- - !ruby/object:Gem::Dependency
15
+ date: 2011-10-06 00:00:00.000000000Z
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
24
18
  name: bundler
25
- prerelease: false
26
- requirement: &id001 !ruby/object:Gem::Requirement
19
+ requirement: &70213866353640 !ruby/object:Gem::Requirement
27
20
  none: false
28
- requirements:
29
- - - ">="
30
- - !ruby/object:Gem::Version
31
- segments:
32
- - 1
33
- - 0
34
- - 0
35
- - rc
36
- - 6
21
+ requirements:
22
+ - - ! '>='
23
+ - !ruby/object:Gem::Version
37
24
  version: 1.0.0.rc.6
38
25
  type: :development
39
- version_requirements: *id001
40
- description: "\tSimple rails browser detection based on original plugin by Richard Livsey\"\n"
26
+ prerelease: false
27
+ version_requirements: *70213866353640
28
+ description: ! "\tSimple rails browser detection based on original plugin by Richard
29
+ Livsey\"\n"
41
30
  email: []
42
-
43
31
  executables: []
44
-
45
32
  extensions: []
46
-
47
33
  extra_rdoc_files: []
48
-
49
- files:
34
+ files:
50
35
  - .gitignore
51
36
  - .yardopts
52
37
  - Gemfile
38
+ - History.txt
39
+ - Manifest.txt
53
40
  - README.md
54
41
  - Rakefile
55
42
  - VERSION
@@ -85,39 +72,28 @@ files:
85
72
  - test/browser_detect_test.rb
86
73
  - test/fixtures/user_agents.yml
87
74
  - test/test_helper.rb
88
- has_rdoc: true
89
75
  homepage: http://github.com/traction/browser_detect
90
76
  licenses: []
91
-
92
77
  post_install_message:
93
78
  rdoc_options: []
94
-
95
- require_paths:
79
+ require_paths:
96
80
  - lib
97
- required_ruby_version: !ruby/object:Gem::Requirement
81
+ required_ruby_version: !ruby/object:Gem::Requirement
98
82
  none: false
99
- requirements:
100
- - - ">="
101
- - !ruby/object:Gem::Version
102
- segments:
103
- - 0
104
- version: "0"
105
- required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
88
  none: false
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- segments:
111
- - 1
112
- - 3
113
- - 6
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
114
92
  version: 1.3.6
115
93
  requirements: []
116
-
117
94
  rubyforge_project: browser_detect
118
- rubygems_version: 1.3.7
95
+ rubygems_version: 1.8.10
119
96
  signing_key:
120
97
  specification_version: 3
121
98
  summary: A simple rails browser detection plugin
122
99
  test_files: []
123
-