useragent2css 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2008 Los Angeles Times (http://www.latimes.com)
2
+ Copyright (c) 2012 Lawrence Pit (http://lawrencepit.com)
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # User-Agent to CSS [![Build Status](https://secure.travis-ci.org/lawrencepit/useragent2css.png)](http://travis-ci.org/lawrencepit/useragent2css?branch=master) [![Dependency Status](https://gemnasium.com/lawrencepit/useragent2css.png)](https://gemnasium.com/lawrencepit/useragent2css)
2
+
3
+ Parse an HTTP User-Agent header string and get a bunch of CSS classes back identifying the browser and OS of the client. That's it. Nothing more.
4
+
5
+ _Why determine this on the server instead of on the client using javascript?_
6
+
7
+ Because the javascript code will run after the browser has already started rendering the HTML code. By having the appropriate CSS classes injected in the HTML source as early as possible, the browser will render faster and slow browsers will not 'flicker'.
8
+
9
+
10
+ Installation and Usage
11
+ ----------------------
12
+
13
+ If you use Rails, add this to your Gemfile:
14
+
15
+ gem 'useragent2css'
16
+
17
+ In your view code, e.g.:
18
+
19
+ <body class="<%= useragent2css %>">
20
+
21
+ Example result:
22
+
23
+ <body class="webkit chrome chrome16 mac">
24
+
25
+ Or:
26
+
27
+ gem install useragent2css
28
+
29
+ And in your view code, e.g.:
30
+
31
+ <body class="<%= UserAgent.css(request["HTTP_USER_AGENT"]) %>">
32
+
33
+
34
+ Thanks
35
+ ------------
36
+
37
+ [Rafael Lima](https://github.com/rafaelp/css_browser_selector) - for the original javascript implementation.
38
+
39
+ [LATimes](https://github.com/latimes/css_browser_selector) - for the original ruby implementation.
40
+
41
+
42
+ Author
43
+ ----------
44
+
45
+ Lawrence Pit, lawrence.pit@gmail.com, [lawrencepit.com](http://lawrencepit.com), [@lawrencepit](http://twitter.com/lawrencepit)
46
+
47
+
48
+ Copyright
49
+ -----------
50
+
51
+ Copyright (c) 2012 Lawrence Pit. See MIT-LICENSE for details.
@@ -0,0 +1,4 @@
1
+ module UserAgent
2
+ VERSION = '1.0'
3
+ end
4
+
data/lib/user_agent.rb ADDED
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+ module UserAgent
3
+
4
+ def self.css(ua)
5
+ ua = (ua||"").downcase
6
+ br = case ua
7
+ when /opera[\/,\s+](\d+)/
8
+ o = %W(opera opera#{$1})
9
+ o << "mobile" if ua.include?('mini')
10
+ o.join(" ")
11
+ when /webtv/ ; "gecko"
12
+ when /msie (\d)/ ; "ie ie#{$1}"
13
+ when %r{firefox/2} ; "gecko ff2"
14
+ when %r{firefox/3.5} ; "gecko ff3 ff3_5"
15
+ when %r{firefox/3} ; "gecko ff3"
16
+ when %r{firefox/4} ; "gecko ff4"
17
+ when /konqueror/ ; "konqueror"
18
+ when /applewebkit\/([\d.]+).? \([^)]*\) ?(?:version\/(\d+))?.*$/
19
+ o = %W(webkit)
20
+ if ua.include?('iron')
21
+ o << 'iron'
22
+ elsif ua.include?('chrome')
23
+ ua =~ /chrome\/(\d+)/
24
+ o << "chrome chrome#{$1}"
25
+ else
26
+ o << "safari safari"+ ($2 || (($1.to_i >= 400) ? '2' : '1'))
27
+ end
28
+ o.join(" ")
29
+ when /gecko/, /mozilla/ ; "gecko"
30
+ end
31
+ os = ua.include?('mac') || ua.include?('darwin') ?
32
+ ua.include?('iphone') ? 'iphone' : ua.include?('ipod') ? 'ipod' : ua.include?('ipad') ? 'ipad' : 'mac' :
33
+ ua.include?('x11') || ua.include?('linux') ? 'linux' :
34
+ ua.include?('win') ? 'win' : nil
35
+ "#{br}#{" " unless br.nil? or os.nil?}#{os}"
36
+ end
37
+
38
+ def useragent2css(ua = request.env["HTTP_USER_AGENT"])
39
+ UserAgent.css(ua)
40
+ end
41
+
42
+ end
43
+
44
+ ActionView::Base.send(:include, UserAgent) if defined?(::ActionView::Base)
45
+
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
3
+ $LOAD_PATH.unshift File.dirname(__FILE__)
4
+
5
+ require 'rubygems'
6
+ require 'test/unit'
7
+ require 'rspec'
8
+
9
+ require 'action_controller'
10
+ require 'action_view'
11
+ puts "Testing with ActionPack #{ActionPack::VERSION::STRING}"
12
+
13
+ require 'user_agent'
14
+
@@ -0,0 +1,217 @@
1
+ # encoding: utf-8
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ describe UserAgent do
5
+
6
+ it "is usable from Rails action view" do
7
+ view = ActionView::Base.new(nil, {})
8
+ view.request = ActionView::TestCase::TestController.new.request
9
+ view.request.env["HTTP_USER_AGENT"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.7 (Gecko) Safari/535.7"
10
+ view.useragent2css.should == "webkit safari safari2 mac"
11
+ view.useragent2css("mozilla/5.0 firefox").should == "gecko"
12
+ end
13
+
14
+ it "should handle blank arguments" do
15
+ UserAgent.css(nil).should == ""
16
+ UserAgent.css("").should == ""
17
+ end
18
+
19
+ it "iron" do
20
+ UserAgent.css("mozilla/5.0 (windows; u; windows nt 5.1; en-us) applewebkit/530.1 (khtml, like gecko) iron/2.0.168.0 safari/530.1").should == "webkit iron win"
21
+ end
22
+
23
+ it "camino" do
24
+ assert_browser_strings({
25
+ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en; rv:1.8.1.6) Gecko/20070809 Camino/1.5.1" => "gecko mac", # camino
26
+ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.0.1) Gecko/20060118 Camino/1.0b2+" => "gecko mac", # camino nightly
27
+ "Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.5b) Gecko/20030917 Camino/0.7+" => "gecko mac", # camino
28
+ "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-US; rv:1.0.1) Gecko/20021104 Chimera/0.6" => "gecko mac" # camino chimera
29
+ })
30
+ end
31
+
32
+ it "chrome" do
33
+ assert_browser_strings({
34
+ "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/10.A.B.C Safari/525.13" => "webkit chrome chrome10 win",
35
+ "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/14.2.149.27 Safari/525.13" => "webkit chrome chrome14 win",
36
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7" => "webkit chrome chrome16 mac"
37
+ })
38
+ end
39
+
40
+ it "firefox" do
41
+ assert_browser_strings({
42
+ "mozilla/5.0 (macintosh; u; intel mac os x 10.5; en-gb; rv:1.9.1) gecko/20090624 firefox/3.5" => "gecko ff3 ff3_5 mac", # firefox 3.5 mac
43
+ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6" => "gecko ff2 mac", # firefox 2 mac
44
+ "Mozilla/5.0 (X11; U; Darwin Power Macintosh; en-US; rv:1.8.0.12) Gecko/20070803 Firefox/1.5.0.12 Fink Community Edition" => "gecko mac", # firefox 1.5 darwin
45
+ "Mozilla/5.0 (Windows; u; Windows NT 5.1; en-us; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1" => "gecko ff3 win", # firefox 3 win
46
+ "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9a7) Gecko/2007080210 GranParadiso/3.0a7" => "gecko win", # firefox dev
47
+ "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9b2pre) Gecko/2007120505 Minefield/3.0b2pre" => "gecko win", # firefox dev
48
+ "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11" => "gecko ff2 win", # firefox 2 vista
49
+ "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.13) Gecko/20060410 Firefox/1.0.8" => "gecko win", # firefox 1 xp
50
+ "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.3) Gecko/20041002 Firefox/0.10.1" => "gecko win", # firefox pre v1
51
+ "Mozilla/5.0 (X11; U; SunOS sun4m; en-US; rv:1.4b) Gecko/20030517 Mozilla Firebird/0.6" => "gecko linux", # firefox firebird
52
+ "Mozilla/5.0 (Windows; U; WinNT4.0; en-US; rv:1.3a) Gecko/20021207 Phoenix/0.5" => "gecko win", # firefox phoenix
53
+ "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.2b) Gecko/20020923 Phoenix/0.1" => "gecko win", # firefox phoenix xp
54
+ "Mozilla/3.0 (x86 [en] Windows NT 5.1; Sun)" => "gecko win" # hotjava
55
+ })
56
+ end
57
+
58
+ it "navigator" do
59
+ assert_browser_strings({
60
+ "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.8pre) Gecko/20071019 Firefox/2.0.0.8 Navigator/9.0.0.1" => "gecko ff2 win", # Navigator 9 with Firefox fixes
61
+ "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.0" => "opera opera9 win", # Opera 9
62
+ "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.5) Gecko/20050519 Netscape/8.0.1" => "gecko win", # A real Firefox based Netscape 8 with a security patch (already) on Win 2K
63
+ "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)" => "gecko win", # Netscape 7.2 and still we're wondering what's the (ax)?
64
+ "Mozilla/5.0 (Windows; U; WinNT4.0; en-CA; rv:0.9.4) Gecko/20011128 Netscape6/6.2.1" => "gecko win", # NS 6.2.1 on NT4.0.
65
+ "Mozilla/4.8 [en] (X11; U; Linux 2.4.20-8 i686)" => "gecko linux", # NS 4.8 on Redhat 9
66
+ "Mozilla/3.01 (WinNT; I) [AXP]" => "gecko win", # NS 3.01 on DEC ALPHA under NT
67
+ "Mozilla/2.02 [fr] (WinNT; I)" => "gecko win", # NS 2.02 on MS NT 4.0.
68
+ "Mozilla/0.91 Beta (Windows)" => "gecko win", # The ex-new dinosaur string. Netscape 0.91, a pre-1.0 beta release from 1994.
69
+ "Mozilla/0.6 Beta (Windows)" => "gecko win" # The new dinosaur string. Netscape 0.6 on WfWG 3.11.
70
+ })
71
+ end
72
+
73
+ it "konqueror" do
74
+ assert_browser_strings({
75
+ "Mozilla/5.0 (compatible; Konqueror/4.0; Microsoft Windows) KHTML/4.0.80 (like Gecko)" => "konqueror win", # konqueror win
76
+ "Mozilla/5.0 (compatible; Konqueror/3.92; Microsoft Windows) KHTML/3.92.0 (like Gecko)" => "konqueror win", # konqueror win
77
+ "Mozilla/5.0 (compatible; Konqueror/3.5; Darwin) KHTML/3.5.6 (like Gecko)" => "konqueror mac", # konqueror mac
78
+ "Mozilla/5.0 (compatible; Konqueror/3.5; Darwin 8.10.0; X11; Power Macintosh; en_US)KHTML/3.5.6 (like Gecko)" => "konqueror mac", # konqueror darwin
79
+ "Mozilla/5.0 (compatible; Konqueror/3.5; Linux; X11; x86_64) KHTML/3.5.6 (like Gecko) (Kubuntu)" => "konqueror linux", # konqueror linux
80
+ "Mozilla/5.0 (compatible; Konqueror/3.4; CYGWIN_NT-5.1) KHTML/3.4.89 (like Gecko)" => "konqueror win", # konqueror cygwin
81
+ "Mozilla/5.0 (compatible; Konqueror/3.5; Linux 2.6.14-kanotix-6; X11) KHTML/3.5.3 (like Gecko) (Debian package 4:3.5.3-1)" => "konqueror linux", # konqueror linux
82
+ "Mozilla/5.0 (compatible; Konqueror/3.5; Linux; X11; i686; en_US) KHTML/3.5.3 (like Gecko)" => "konqueror linux", # konqueror suse linux
83
+ "Mozilla/5.0 (compatible; Konqueror/3.4; Linux) KHTML/3.4.3 (like Gecko) (Kubuntu package 4:3.4.3-0ubuntu1)" => "konqueror linux", # konqueror ubuntu
84
+ "Mozilla/5.0 (compatible; Konqueror/3.4; FreeBSD) KHTML/3.4.3 (like Gecko)" => "konqueror", # konqueror freebsd
85
+ "Mozilla/5.0 (compatible; Konqueror/3.4; Linux 2.6.8; X11; i686; en_US) KHTML/3.4.0 (like Gecko)" => "konqueror linux", # konqueror mandriva
86
+ "Mozilla/5.0 (compatible; Konqueror/3.4; Linux) KHTML/3.4.1 (like Gecko)" => "konqueror linux", # konqueror mandriva
87
+ "Mozilla/5.0 (compatible; Konqueror/3.4; Linux 2.6.8; X11; i686; en_US) KHTML/3.4.0 (like Gecko)" => "konqueror linux", # konqueror Slaware Linux 10
88
+ "Mozilla/5.0 (compatible; Konqueror/3.3; Linux 2.6.8.1-24mdk; X11; i686; en_GB, en_US) (KHTML, like Gecko)" => "konqueror linux", # Konqueror/KDE Version 3.3 on Linux Mandrake 10.1
89
+ "Mozilla/5.0 (compatible; Konqueror/3.3; Linux) (KHTML, like Gecko)" => "konqueror linux", # Konqueror/KDE Version 3.3 on Linux Mandrake 10.1
90
+ "Mozilla/5.0 (compatible; Konqueror/3.2; Linux 2.6.7-3ajp; X11; i686) (KHTML, like Gecko)" => "konqueror linux", # Konqueror/KDE Version 3.2 on Linux Mandrake 10.0
91
+ "Mozilla/5.0 (compatible; Konqueror/3.2; FreeBSD) (KHTML, like Gecko)" => "konqueror", # Konqueror/KDE Version 3.2 on FreeBSD
92
+ "Mozilla/5.0 (compatible; Konqueror/3.1; Linux 2.4.20)" => "konqueror linux", # Konqueror/KDE Version 3.1 on Linux
93
+ "Mozilla/5.0 (compatible; Konqueror/3.1; Linux; X11; i686)" => "konqueror linux", # Konqueror Linux Mandrake 9.0 under X windows
94
+ "Mozilla/5.0 (compatible; Konqueror/3.1; Linux 2.4.19-32mdkenterprise; X11; i686; ar, en_US)" => "konqueror linux", # Konqueror on KDE 3.1 on Linux Mandrake 9.0 under X windows
95
+ "Mozilla/5.0 (compatible; Konqueror/2.1.1; X11)" => "konqueror linux" # Konqueror 2.1.1 (KDE) on Linux Mandrake 8.0 under X windows
96
+ })
97
+ end
98
+
99
+ it "opera" do
100
+ assert_browser_strings({
101
+ "opera/9.50 (j2me/midp; opera mini/ 4.1.11320/546; u; pt)" => "opera opera9 mobile" , # Opera Mini 10.00 on MAC with OS X
102
+ "Opera/10.00 (Macintosh; Intel Mac OS X; u; en) presto/2.2.0" => "opera opera10 mac", # Opera 10.00 on MAC with OS X
103
+ "Opera/9.20 (Macintosh; Intel Mac OS X; U; en)" => "opera opera9 mac", # Opera 9.20 on MAC with OS X
104
+ "Opera/9.02 (Windows NT 5.0; U; en)" => "opera opera9 win", # Opera 9.02 on Win 2K
105
+ "Opera/9.00 (Windows NT 4.0; U; en)" => "opera opera9 win", # Opera 9.0 on Windows NT 4.0
106
+ "Opera/9.00 (X11; Linux i686; U; en)" => "opera opera9 linux", # Opera 9.0 on linux 2.6, static Qt installation
107
+ "Opera/9.00 (Windows NT 5.1; U; en)" => "opera opera9 win", # Opera 9 on XP (spot the diff with the next one)
108
+ "Opera/9.0 (Windows NT 5.1; U; en)" => "opera opera9 win", # Opera 9
109
+ "Opera/9.0 (Macintosh; PPC Mac OS X; U; en)" => "opera opera9 mac", # Opera 9
110
+ "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.0" => "opera opera9 win", # Opera 9
111
+ "Mozilla/4.0 (compatible; MSIE 6.0; Windows CE; PPC; 480x640) Opera 8.60 [en]" => "opera opera8 win", # Opera Mobile 8.60 on a Dell Axim X51v
112
+ "Opera/8.5 (Macintosh; PPC Mac OS X; U; en)" => "opera opera8 mac", # Opera 8.5 on the Mac
113
+ "Mozilla/5.0 (Macintosh; PPC Mac OS X; U; en) Opera 8.5" => "opera opera8 mac", # Opera 8.5 on the Mac
114
+ "Mozilla/4.0 (compatible; MSIE 6.0; Mac_PowerPC Mac OS X; en) Opera 8.5" => "opera opera8 mac", # Opera 8.5 on the Mac
115
+ "Opera/8.0 (Macintosh; PPC Mac OS X; U; en)" => "opera opera8 mac", # Opera 8.0 on the Mac
116
+ "Mozilla/5.0 (Macintosh; PPC Mac OS X; U; en) Opera 8.0" => "opera opera8 mac", # Opera 8.0 on the Mac
117
+ "Mozilla/4.0 (compatible; MSIE 6.0; Mac_PowerPC Mac OS X; en) Opera 8.0" => "opera opera8 mac", # Opera 8.0 on the Mac
118
+ "Opera/8.01 (Windows NT 5.1)" => "opera opera8 win", # Opera 8.01 preview
119
+ "Mozilla/5.0 (Windows NT 5.1; U; en) Opera 8.01" => "opera opera8 win", # Opera 8.01 preview
120
+ # "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" => "ie ie6 win", # Opera 8.01 preview
121
+ "Mozilla/5.0 (Windows NT 5.1; U; en) Opera 8.00" => "opera opera8 win", # Opera 8.00 (ex 7.60 preview) on XP Pro as Mozilla
122
+ "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 8.00" => "opera opera8 win", # Opera 8.00 (ex 7.60 preview) on XP Pro as MSIE
123
+ "Opera/8.00 (Windows NT 5.1; U; en)" => "opera opera8 win", # Opera 8.00 (ex 7.60 preview) on XP Pro
124
+ "Mozilla/5.0 (X11; Linux i386; U) Opera 7.60 [en-GB]" => "opera opera7 linux", # Opera 7.60 (pretending to be Mozilla running on NetBSD
125
+ "Opera/7.60 (Windows NT 5.2; U) [en] (IBM EVV/3.0/EAK01AG9/LE)" => "opera opera7 win", # Opera 7.60 running on XP. # win
126
+ "Opera/7.54 (Windows NT 5.1; U) [pl]" => "opera opera7 win", # Opera 7.54 in native mode
127
+ "Opera/7.50 (X11; Linux i686; U) [en]" => "opera opera7 linux", # Opera 7.50 running on Mandrake Linux
128
+ "Mozilla/5.0 (X11; Linux i686; U) Opera 7.50 [en]" => "opera opera7 linux", # Opera 7.50 running on Mandrake Linux
129
+ "Mozilla/4.0 (compatible; MSIE 6.0; X11; Linux i686) Opera 7.20 [en]" => "opera opera7 linux", # Opera 7.20 running on Linux and pretending to be MSIE 6.0
130
+ "Opera/7.11 (Windows NT 5.1; U) [en]" => "opera opera7 win", # On Windows XP.
131
+ "Mozilla/4.0 (compatible; MSIE 6.0; Windows ME) Opera 7.11 [en]" => "opera opera7 win", # Opera 7.11 running on WME
132
+ "Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 5.0) Opera 7.02 Bork-edition [en]" => "opera opera7 win", # The infamous MSN version of Opera 7.02 on W2K
133
+ "Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 4.0) Opera 7.0 [en]" => "opera opera7 win", # Opera 7.0 on NT 4.0.
134
+ "Mozilla/4.0 (compatible; MSIE 5.0; Windows 2000) Opera 6.0 [en]" => "opera opera6 win", # Opera 6.0 on Windows 2000.
135
+ "Mozilla/4.0 (compatible; MSIE 5.0; Windows 95) Opera 6.01 [en]" => "opera opera6 win", # Opera 6.01 on Windows 95.
136
+ "Mozilla/4.0 (compatible; MSIE 5.0; Mac_PowerPC) Opera 5.0 [en]" => "opera opera5 mac" # Opera 5.0 on the Mac (OS8.6)
137
+ })
138
+ end
139
+
140
+ it "msie" do
141
+ assert_browser_strings({
142
+ "mozilla/4.0 (compatible; msie 8.0; windows nt 5.1; trident/4.0; mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; sv1) ; .net clr 1.1.4322; .net clr 2.0.50727; .net clr 3.0.4506.2152; .net clr 3.5.30729; officeliveconnector.1.4; officelivepatch.1.3)" => "ie ie8 win",
143
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.2; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)" => "ie ie8 win",
144
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; Media Center PC 6.0; InfoPath.2; MS-RTC LM 8)" => "ie ie8 win",
145
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; InfoPath.2)" => "ie ie8 win",
146
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Zune 3.0)" => "ie ie8 win",
147
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; MS-RTC LM 8)" => "ie ie8 win",
148
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3)" => "ie ie8 win",
149
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; OfficeLiveConnector.1.3; OfficeLivePatch.0.0; Zune 3.0; MS-RTC LM 8)" => "ie ie8 win",
150
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; MS-RTC LM 8)" => "ie ie8 win",
151
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 4.0.20402; MS-RTC LM 8)" => "ie ie8 win",
152
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322; InfoPath.2)" => "ie ie8 win",
153
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; InfoPath.3; .NET CLR 4.0.20506)" => "ie ie8 win",
154
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729)" => "ie ie8 win",
155
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0)" => "ie ie8 win",
156
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 3.0.04506; Media Center PC 5.0; SLCC1)" => "ie ie8 win",
157
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)" => "ie ie8 win",
158
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0)" => "ie ie8 win",
159
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0; .NET CLR 3.0.04506; Media Center PC 5.0; SLCC1)" => "ie ie8 win",
160
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; FDM; Tablet PC 2.0; .NET CLR 4.0.20506; OfficeLiveConnector.1.4; OfficeLivePatch.1.3)" => "ie ie8 win",
161
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 3.0.04506; Media Center PC 5.0; SLCC1; Tablet PC 2.0)" => "ie ie8 win",
162
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322; InfoPath.2)" => "ie ie8 win",
163
+ "mozilla/4.0 (compatible; msie 8.0; windows nt 6.0; trident/4.0; slcc1; .net clr 2.0.50727; wwtclient2; infopath.2; officeliveconnector.1.3; officelivepatch.0.0; .net clr 3.0.30618; .net clr 3.5.30729)" => "ie ie8 win",
164
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)" => "ie ie8 win",
165
+ "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0" => "ie ie7 win",
166
+ "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; Media Center PC 5.0; .NET CLR 1.1.4322; Windows-Media-Player/10.00.00.3990; InfoPath.2" => "ie ie7 win", # MSIE 7 running on Windows Vista 64-bit with a ton of 'stuff'
167
+ "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; Dealio Deskball 3.0)" => "ie ie7 win", # MSIE 7 on XP and every version of .NET known to mankind
168
+ "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; NeosBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)" => "ie ie6 win", # Explanation: MSIE 6.x on XP with a skin from neos.tv who seem to specialize in the hospitality (read hotel) industry.
169
+ "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)" => "ie ie5 win", # MSIE 5.5 on Windows 98
170
+ "Mozilla/4.0 (compatible; MSIE 5.22; Mac_PowerPC)" => "ie ie5 mac", # Latest MAC OS X version of MSIE
171
+ "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)" => "ie ie5 win", # MSIE 5.0 on MS NT 4.0
172
+ "Mozilla/4.0 (compatible; MSIE 4.01; Windows NT 5.0)" => "ie ie4 win", # Explanation: MSIE 4.01 on Windows XP SP2
173
+ "Mozilla/2.0 (compatible; MSIE 3.02; Windows CE; 240x320)" => "ie ie3 win", # MSIE 3.02 on a Pocket PC 2002
174
+ "Mozilla/1.22 (compatible; MSIE 2.0; Windows 95)" => "ie ie2 win" # MSIE 2.0 in windows '95
175
+ })
176
+ end
177
+
178
+ it "safari" do
179
+ assert_browser_strings({
180
+ "mozilla/5.0 (windows; u; windows nt 6.0; pt-br) applewebkit/528.16 (khtml, like gecko) version/4.0 safari/528.16" => "webkit safari safari4 win",
181
+ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-gb) AppleWebKit/523.10.6 (KHTML, like Gecko) Version/3.0.4 Safari/523.10.6" => "webkit safari safari3 mac", # Safari 3.0.4 on Mac OS 10.5.1 Intel
182
+ "Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3A100a Safari/419.3" => "webkit safari safari3 ipod", # Safari 3.0 for the iPod touch
183
+ "Mozilla/5.0 (iPad; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3A100a Safari/419.3" => "webkit safari safari3 ipad", # Safari 3.0 for the iPad
184
+ "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1C28 Safari/419.3" => "webkit safari safari3 iphone", # Safari 3.0 for the iPhone
185
+ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/522.11.1 (KHTML, like Gecko) Version/3.0.3 Safari/522.12.1" => "webkit safari safari3 mac", # Safari 3.0.3 for Intel version of iMac
186
+ "Mozilla/5.0 (Windows; U; Windows NT 5.1; bg) AppleWebKit/522.13.1 (KHTML, like Gecko) Version/3.0.2 Safari/522.13.1" => "webkit safari safari3 win", # Safari 3.0.2 beta for Windows XP
187
+ "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru) AppleWebKit/522.11.3 (KHTML, like Gecko) Version/3.0 Safari/522.11.3" => "webkit safari safari3 win", # Safari browser V 3.0 Beta for Windows XP SP2
188
+ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/419.3 (KHTML, like Gecko) Safari/419.3" => "webkit safari safari2 mac", # Safari browser V 2.o.4 with Beta for OS X
189
+ "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/418.8 (KHTML, like Gecko) Safari/419.3" => "webkit safari safari2 mac", # Safari browser 2.0.4 for MAC OS X (10.4.7)
190
+ "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/417.9 (KHTML, like Gecko) Safari/417.8" => "webkit safari safari2 mac", # Safari browser 2.0.3 for MAC OS X (10.4.4)
191
+ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/417.3 (KHTML, like Gecko) Safari/417.2" => "webkit safari safari2 mac", # Safari browser 2.0 for MAC OS X (10.4.4 build)
192
+ "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412 (KHTML, like Gecko) Safari/412" => "webkit safari safari2 mac", # Safari browser 2.0 for MAC OS X (10.4.1 build 8B15)
193
+ "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; fr-fr) AppleWebKit/312.5.1 (KHTML, like Gecko) Safari/312.3.1" => "webkit safari safari1 mac", # Safari 1.3.1 on 1.3.9 after after Security update 2005-008
194
+ "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; fr-fr) AppleWebKit/312.5 (KHTML, like Gecko) Safari/312.3" => "webkit safari safari1 mac", # Safari 1.3.1 (v312.3) 10.3.9 = last update on last version of Panther
195
+ "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/124 (KHTML, like Gecko) Safari/125.1" => "webkit safari safari1 mac", # Safari browser 1.25.1 for MAC OS
196
+ "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/106.2 (KHTML, like Gecko) Safari/100.1" => "webkit safari safari1 mac", # Safari browser 1.0 for MAC OS X
197
+ "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; es) AppleWebKit/85 (KHTML, like Gecko) Safari/85" => "webkit safari safari1 mac", # Safari browser 1.0 for MAC OS X with spanish language variant
198
+ "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/74 (KHTML, like Gecko) Safari/74" => "webkit safari safari1 mac", # Safari browser build 74 for MAC OS X
199
+ "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/51 (like Gecko) Safari/51" => "webkit safari safari1 mac" # Safari browser for MAC OS X
200
+ })
201
+ end
202
+
203
+ it "webtv" do
204
+ assert_browser_strings({
205
+ "Mozilla/4.0 WebTV/2.8 (compatible; MSIE 4.0)" => "gecko" # WebTV 2.8
206
+ })
207
+ end
208
+
209
+ private
210
+
211
+ def assert_browser_strings(tests)
212
+ tests.each do |ua, expected|
213
+ UserAgent.css(ua).should == expected
214
+ end
215
+ end
216
+
217
+ end
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "user_agent/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = %q{useragent2css}
7
+ s.version = UserAgent::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Lawrence Pit"]
10
+ s.email = %q{lawrence.pit@gmail.com}
11
+ s.homepage = %q{http://github.com/lawrencepit/useragent2css}
12
+ s.summary = %q{User-Agent to CSS parser for ruby}
13
+ s.description = %q{User-Agent to CSS parser for ruby}
14
+ s.date = Time.now.utc.strftime("%Y-%m-%d")
15
+ s.files = Dir.glob("lib/**/*") + [
16
+ "MIT-LICENSE",
17
+ "README.md",
18
+ "Gemfile",
19
+ "useragent2css.gemspec"
20
+ ]
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib"]
24
+ s.rdoc_options = ["--charset=UTF-8"]
25
+ s.add_development_dependency "actionpack"
26
+ s.add_development_dependency "rake"
27
+ # s.add_development_dependency "rcov"
28
+ s.add_development_dependency "rspec"
29
+ end
30
+
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: useragent2css
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lawrence Pit
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: actionpack
16
+ requirement: &70139066061820 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70139066061820
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &70139066061400 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70139066061400
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70139066060980 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70139066060980
47
+ description: User-Agent to CSS parser for ruby
48
+ email: lawrence.pit@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - lib/user_agent/version.rb
54
+ - lib/user_agent.rb
55
+ - MIT-LICENSE
56
+ - README.md
57
+ - Gemfile
58
+ - useragent2css.gemspec
59
+ - spec/spec_helper.rb
60
+ - spec/user_agent_spec.rb
61
+ homepage: http://github.com/lawrencepit/useragent2css
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --charset=UTF-8
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.15
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: User-Agent to CSS parser for ruby
86
+ test_files:
87
+ - spec/spec_helper.rb
88
+ - spec/user_agent_spec.rb