Skip to content

Commit

Permalink
Added Wii support
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Aug 28, 2009
1 parent f9cdd16 commit 5ff62dd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 54 deletions.
8 changes: 8 additions & 0 deletions README.rdoc
Expand Up @@ -9,7 +9,15 @@ User agent parser.
agent.name # => :Safari
agent.version # => '4.0.3'
agent.engine # => :webkit
agent.os # => :'Windows Vista'
agent.engine_version # => '531.9'

== Supported Agents

* Safari
* Opera
* IE
* Wii

== License:

Expand Down
13 changes: 7 additions & 6 deletions lib/user-agent/agent.rb
Expand Up @@ -81,7 +81,7 @@ def == other
# Return engine version for user agent _string_.

def self.engine_version_for_user_agent string
$1 if string =~ /#{engine_for_user_agent(string)}\/([\d\w\.]+)/i
$1 if string =~ /#{engine_for_user_agent(string)}[\/ ]([\d\w\.]+)/i
end

##
Expand Down Expand Up @@ -115,13 +115,14 @@ def self.engine_for_user_agent string

def self.os_for_user_agent string
case string
when /windows nt 6.1/i ; :'Windows 7'
when /windows nt 6.0/i ; :'Windows Vista'
when /windows nt 5.2/i ; :'Windows 2003'
when /windows nt 5.1/i ; :'Windows XP'
when /windows nt 5.0/i ; :'Windows 2000'
when /windows nt 6\.0/i ; :'Windows Vista'
when /windows nt 6\.\d+/i ; :'Windows 7'
when /windows nt 5\.2/i ; :'Windows 2003'
when /windows nt 5\.1/i ; :'Windows XP'
when /windows nt 5\.0/i ; :'Windows 2000'
when /os x (\d+)[._](\d+)/i ; :"OS X #{$1}.#{$2}"
when /linux/i ; :Linux
when /wii/i ; :Wii
else ; :Unknown
end
end
Expand Down
74 changes: 26 additions & 48 deletions spec/agents_spec.rb
@@ -1,56 +1,34 @@

require File.dirname(__FILE__) + '/spec_helper'

describe Agent do
describe "Safari" do
[
['4.0dp1', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en) AppleWebKit/526.9 (KHTML, like Gecko) Version/4.0dp1 Safari/526.8'],
['4.0.3', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-us) AppleWebKit/531.9 (KHTML, like Gecko) Version/4.0.3 Safari/531.9'],
['4.0.2', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532+ (KHTML, like Gecko) Version/4.0.2 Safari/530.19.1'],
['4.0.1', 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/531.2+ (KHTML, like Gecko) Version/4.0.1 Safari/530.18'],
['4.0', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; ru-RU) AppleWebKit/528.16 (KHTML, like Gecko) Version/4.0 Safari/528.16'],
['3.2.3', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; cs-CZ) AppleWebKit/525.28.3 (KHTML, like Gecko) Version/3.2.3 Safari/525.29'],
].each do |version, string|
it "should parse version #{version}" do
Agent.new(string).name.should == :Safari
Agent.new(string).version.should == version
end
end
def test name, version, os, engine, engine_version, string
it "should parse #{name} #{version} on #{os} with engine #{engine} #{engine_version}" do
agent = Agent.new string
agent.name.should == name
agent.os.should == os
agent.engine.should == engine
agent.version.should == version
agent.engine_version.should == engine_version
end
end

describe Agent do

describe "IE" do
[
['8.0', '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)'],
['7.0b', 'Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)'],
['7.0', 'Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; c .NET CLR 3.0.04506; .NET CLR 3.5.30707; InfoPath.1; el-GR)'],
['6.1', 'Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)'],
['6.0b', 'Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1)'],
['6.0', 'Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)'],
].each do |version, string|
unless version.empty?
it "should parse version #{version}" do
Agent.new(string).name.should == :IE
Agent.new(string).version.should == version
end
end
end
end
test :Safari, '4.0dp1', :'Windows XP', :webkit, '526.9', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en) AppleWebKit/526.9 (KHTML, like Gecko) Version/4.0dp1 Safari/526.8'
test :Safari, '4.0.3', :'Windows Vista', :webkit, '531.9', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-us) AppleWebKit/531.9 (KHTML, like Gecko) Version/4.0.3 Safari/531.9'
test :Safari, '4.0.2', :'Windows 7', :webkit, '532', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532+ (KHTML, like Gecko) Version/4.0.2 Safari/530.19.1'
test :Safari, '4.0.1', :'OS X 10.5', :webkit, '531.2', 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/531.2+ (KHTML, like Gecko) Version/4.0.1 Safari/530.18'
test :Safari, '4.0', :'Windows Vista', :webkit, '528.16', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; ru-RU) AppleWebKit/528.16 (KHTML, like Gecko) Version/4.0 Safari/528.16'
test :Safari, '3.2.3', :'Windows XP', :webkit, '525.28.3', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; cs-CZ) AppleWebKit/525.28.3 (KHTML, like Gecko) Version/3.2.3 Safari/525.29'

describe "Opera" do
[
['9.99', 'Opera/9.99 (Windows NT 5.1; U; pl) Presto/9.9.9'],
['9.70', 'Mozilla/5.0 (Linux i686 ; U; en; rv:1.8.1) Gecko/20061208 Firefox/2.0.0 Opera 9.70'],
['9.64', 'Opera/9.64 (X11; Linux i686; U; Linux Mint; it) Presto/2.1.1'],
['9.51', 'Mozilla/5.0 (X11; Linux i686; U; en; rv:1.8.1) Gecko/20061208 Firefox/2.0.0 Opera 9.51'],
['9.00', 'Opera/9.00 (Nintindo Wii; U; ; 103858; Wii Shop Channel/1.0; en)'],
].each do |version, string|
unless version.empty?
it "should parse version #{version}" do
Agent.new(string).name.should == :Opera
Agent.new(string).version.should == version
end
end
end
end
test :IE, '8.0', :'Windows 7', :msie, '8.0', '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)'
test :IE, '7.0b', :'Windows 2003', :msie, '7.0b', 'Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)'
test :IE, '6.0b', :'Windows XP', :msie, '6.0b', 'Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1)'
test :IE, '6.0', :'Windows XP', :msie, '6.0', 'Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)'

test :Opera, '9.99', :'Windows XP', :presto, '9.9.9', 'Opera/9.99 (Windows NT 5.1; U; pl) Presto/9.9.9'
test :Opera, '9.70', :Linux, :gecko, '20061208', 'Mozilla/5.0 (Linux i686 ; U; en; rv:1.8.1) Gecko/20061208 Firefox/2.0.0 Opera 9.70'
test :Opera, '9.64', :Linux, :presto, '2.1.1', 'Opera/9.64 (X11; Linux i686; U; Linux Mint; it) Presto/2.1.1'
test :Opera, '9.00', :Wii, :unknown, nil, 'Opera/9.00 (Nintindo Wii; U; ; 103858; Wii Shop Channel/1.0; en)'

end

0 comments on commit 5ff62dd

Please sign in to comment.