Skip to content

Commit 9fc6b4a

Browse files
committed
Merge pull request #13 from brain-geek/master
Fix issue with chrome detection. Thanks @brain-geek.
2 parents 7f47f3d + 4eb1095 commit 9fc6b4a

File tree

3 files changed

+88
-68
lines changed

3 files changed

+88
-68
lines changed

lib/agent_orange/browser.rb

+9
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ class Browser < Base
1010
:ie => 'MSIE|Internet Explorer|IE',
1111
:firefox => 'Firefox',
1212
:opera => 'Opera',
13+
:chrome => 'Chrome',
1314
:safari => 'Safari'
1415
}
1516

17+
BROWSER_TITLES = BROWSERS.merge :ie => 'MSIE'
18+
1619
def parse(user_agent)
1720
AgentOrange.debug "BROWSER PARSING", 2
1821

@@ -21,13 +24,19 @@ def parse(user_agent)
2124
if content[:name] =~ /(#{BROWSERS.collect{|cat,regex| regex}.join(')|(')})/i
2225
# Matched group against name
2326
self.populate(content)
27+
break
2428
elsif content[:comment] =~ /(#{BROWSERS.collect{|cat,regex| regex}.join(')|(')})/i
2529
# Matched group against comment
2630
chosen_content = { :name => nil, :version => nil }
2731
additional_groups = parse_comment(content[:comment])
2832
additional_groups.each do |additional_content|
2933
if additional_content[:name] =~ /(#{BROWSERS.collect{|cat,regex| regex}.join(')|(')})/i
3034
chosen_content = additional_content
35+
36+
#Additional checking for chromeframe, iemobile, etc.
37+
BROWSERS.each do |cat, regex|
38+
chosen_content[:name] = BROWSER_TITLES[cat] if additional_content[:name] =~ /(#{regex})/i
39+
end
3140
end
3241
end
3342

spec/long_user_agent_spec.rb

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
require File.dirname(__FILE__) + '/spec_helper'
2+
require 'net/http'
3+
require 'iconv'
4+
5+
describe AgentOrange::UserAgent do
6+
describe 'checking with real browsers list' do
7+
before do
8+
@lists = [Iconv.new('UTF-8//IGNORE', 'UTF-8').iconv( File.open("spec/fixtures/browser_ids.htm").read)]
9+
10+
begin
11+
@lists.push Net::HTTP.get('www.zytrax.com', '/tech/web/browser_ids.htm')
12+
rescue
13+
end
14+
end
15+
16+
it "should parse browsers from list" do
17+
[
18+
{
19+
:a_name => 'chrome',
20+
:browser_names => ['Chrome', 'Safari']
21+
},
22+
{
23+
:a_name => 'safari',
24+
:browser_names => ['Safari']
25+
},
26+
{
27+
:a_name => 'firefox',
28+
:browser_names => ['Firefox']
29+
},
30+
{
31+
:a_name => 'msie',
32+
#chromeframe!
33+
:browser_names => ['MSIE', 'Chrome']
34+
},
35+
{
36+
:a_name => 'opera',
37+
:browser_names => ['Opera']
38+
}
39+
].each do |params|
40+
part = ''
41+
42+
@lists.each do |list|
43+
part += list.scan(/<a name="#{params[:a_name]}">.+?<a name="/m).first.to_s
44+
end
45+
46+
part.scan(/<p class="g-c-s">([^<]+)<\/p>/).each do |q|
47+
ua = AgentOrange::UserAgent.new(q.first)
48+
br_name = ua.device.engine.browser.name
49+
50+
params[:browser_names].should include(br_name)
51+
end
52+
end
53+
end
54+
end
55+
end

spec/user_agent_spec.rb

+24-68
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,6 @@
2222
ua.device.engine.browser.name.should == "Safari"
2323
ua.device.engine.browser.version.to_s.should == "533.21.1"
2424
end
25-
26-
detect "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_1) AppleWebKit/535.5 (KHTML, like Gecko) Chrome/16.0.891.1 Safari/535.5" do |ua|
27-
ua.device.type.should == :computer
28-
ua.device.name.should == "Computer"
29-
ua.device.version.should == nil
30-
31-
ua.device.operating_system.name.should == "Mac OS X"
32-
ua.device.operating_system.version.to_s.should == "10.7.1"
33-
34-
ua.device.platform.name.should == "Macintosh"
35-
ua.device.platform.version.should == nil
36-
37-
ua.device.engine.name.should == "AppleWebKit"
38-
ua.device.engine.version.to_s.should == "535.5"
39-
40-
ua.device.engine.browser.name.should == "Safari"
41-
ua.device.engine.browser.version.to_s.should == "535.5"
42-
end
4325
end
4426

4527
describe "Firefox" do
@@ -78,61 +60,35 @@
7860
ua.device.engine.name.should == "AppleWebKit"
7961
ua.device.engine.version.to_s.should == "535.2"
8062

81-
# TODO should be Chrome.
82-
ua.device.engine.browser.name.should == "Safari"
83-
ua.device.engine.browser.version.to_s.should == "535.2"
63+
ua.device.engine.browser.name.should == "Chrome"
64+
ua.device.engine.browser.version.to_s.should == "15.0.872.0"
8465
end
85-
end
8666

87-
describe 'checking with real browsers list' do
88-
before do
89-
@lists = [Iconv.new('UTF-8//IGNORE', 'UTF-8').iconv( File.open("spec/fixtures/browser_ids.htm").read)]
67+
detect "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_1) AppleWebKit/535.5 (KHTML, like Gecko) Chrome/16.0.891.1 Safari/535.5" do |ua|
68+
ua.device.type.should == :computer
69+
ua.device.name.should == "Computer"
70+
ua.device.version.should == nil
9071

91-
begin
92-
@lists.push Net::HTTP.get('www.zytrax.com', '/tech/web/browser_ids.htm')
93-
rescue
94-
end
95-
end
72+
ua.device.operating_system.name.should == "Mac OS X"
73+
ua.device.operating_system.version.to_s.should == "10.7.1"
9674

97-
it "should parse browsers from list" do
98-
[
99-
{
100-
:a_name => 'chrome',
101-
:browser_names => ['Safari']
102-
},
103-
{
104-
:a_name => 'safari',
105-
:browser_names => ['Safari']
106-
},
107-
{
108-
:a_name => 'firefox',
109-
:browser_names => ['Firefox']
110-
},
111-
{
112-
:a_name => 'msie',
113-
:browser_names => ['MSIE','IEMobile']
114-
},
115-
{
116-
:a_name => 'opera',
117-
:browser_names => ['Opera']
118-
}
119-
].each do |params|
120-
part = ''
121-
122-
@lists.each do |list|
123-
part += list.scan(/<a name="#{params[:a_name]}">.+?<a name="/m).first.to_s
124-
end
125-
126-
part.scan(/<p class="g-c-s">([^<]+)<\/p>/).each do |q|
127-
ua = AgentOrange::UserAgent.new(q.first)
128-
br_name = ua.device.engine.browser.name
129-
130-
params[:browser_names].should include(br_name)
131-
end
132-
end
133-
end
75+
ua.device.platform.name.should == "Macintosh"
76+
ua.device.platform.version.should == nil
13477

135-
end
78+
ua.device.engine.name.should == "AppleWebKit"
79+
ua.device.engine.version.to_s.should == "535.5"
80+
81+
ua.device.engine.browser.name.should == "Chrome"
82+
ua.device.engine.browser.version.to_s.should == "16.0.891.1"
83+
end
13684

85+
detect "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; chromeframe/13.0.782.218; chromeframe; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)" do |ua|
86+
ua.device.type.should == :computer
87+
ua.device.name.should == "Computer"
88+
ua.device.version.should == nil
13789

90+
ua.device.engine.browser.name.should == "Chrome"
91+
ua.device.engine.browser.version.to_s.should == "13.0.782.218"
92+
end
93+
end
13894
end

0 commit comments

Comments
 (0)