Skip to content

Commit

Permalink
Lowercase name and platform. Stringify OS.
Browse files Browse the repository at this point in the history
* Lowercase is less to remember (ie: where is capitalization). 
* OS looks so awkward as a symbol. Probably a taste issue but whatevs...
  • Loading branch information
jnunemaker committed Jun 21, 2011
1 parent a776e13 commit fb2d4bd
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 77 deletions.
68 changes: 34 additions & 34 deletions lib/user_agent.rb
Expand Up @@ -49,10 +49,10 @@ def self.engine_version_for_user_agent(string)

def self.version_for_user_agent(string)
case name = name_for_user_agent(string)
when :Chrome ; $1 if string =~ /chrome\/([\d\w\.\-]+)/i
when :Safari ; $1 if string =~ /version\/([\d\w\.\-]+)/i
when :PS3 ; $1 if string =~ /([\d\w\.\-]+)\)\s*$/i
when :PSP ; $1 if string =~ /([\d\w\.\-]+)\)?\s*$/i
when :chrome ; $1 if string =~ /chrome\/([\d\w\.\-]+)/i
when :safari ; $1 if string =~ /version\/([\d\w\.\-]+)/i
when :ps3 ; $1 if string =~ /([\d\w\.\-]+)\)\s*$/i
when :psp ; $1 if string =~ /([\d\w\.\-]+)\)?\s*$/i
else $1 if string =~ /#{name}[\/ ]([\d\w\.\-]+)/i
end
end
Expand All @@ -72,46 +72,46 @@ def self.engine_for_user_agent(string)

def self.os_for_user_agent(string)
case string
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
when /playstation 3/i ; :Playstation
when /playstation portable/i ; :Playstation
when /\(iPad.*os (\d+)[._](\d+)/i ; :"iPad OS #{$1}.#{$2}"
when /\(iPhone.*os (\d+)[._](\d+)/i ; :"iPhone OS #{$1}.#{$2}"
else ; :Unknown
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'
when /playstation 3/i ; 'Playstation'
when /playstation portable/i ; 'Playstation'
when /\(iPad.*os (\d+)[._](\d+)/i ; "iPad OS #{$1}.#{$2}"
when /\(iPhone.*os (\d+)[._](\d+)/i ; "iPhone OS #{$1}.#{$2}"
else ; 'Unknown'
end
end

def self.name_for_user_agent(string)
case string
when /konqueror/i ; :Konqueror
when /chrome/i ; :Chrome
when /safari/i ; :Safari
when /msie/i ; :IE
when /opera/i ; :Opera
when /playstation 3/i ; :PS3
when /playstation portable/i ; :PSP
when /firefox/i ; :Firefox
else ; :Unknown
when /konqueror/i ; :konqueror
when /chrome/i ; :chrome
when /safari/i ; :safari
when /msie/i ; :ie
when /opera/i ; :opera
when /playstation 3/i ; :ps3
when /playstation portable/i ; :psp
when /firefox/i ; :firefox
else ; :unknown
end
end

def self.platform_for_user_agent(string)
case string
when /windows/i ; :Windows
when /macintosh/i ; :Macintosh
when /linux/i ; :Linux
when /wii/i ; :Wii
when /playstation/i ; :Playstation
when /ipad/i ; :iPad
when /iphone/i ; :iPhone
else :Unknown
when /windows/i ; :windows
when /macintosh/i ; :macintosh
when /linux/i ; :linux
when /wii/i ; :wii
when /playstation/i ; :playstation
when /ipad/i ; :ipad
when /iphone/i ; :iphone
else :unknown
end
end
end
6 changes: 3 additions & 3 deletions spec/user_agent_spec.rb
Expand Up @@ -13,7 +13,7 @@

describe "#os" do
it "should return operating system symbol" do
@agent.os.should == :'OS X 10.5'
@agent.os.should == 'OS X 10.5'
end
end

Expand All @@ -37,13 +37,13 @@

describe "#inspect" do
it "should return string presenting the engine, os, version, etc" do
@agent.inspect.should == '#<UserAgent:Safari version:"4.0dp1" engine:"webkit:528.4" os:"OS X 10.5">'
@agent.inspect.should == '#<UserAgent:safari version:"4.0dp1" engine:"webkit:528.4" os:"OS X 10.5">'
end
end

describe "#name" do
it "should return the agent name symbol" do
@agent.name.should == :'Safari'
@agent.name.should == :safari
end
end

Expand Down
87 changes: 47 additions & 40 deletions spec/user_agents_spec.rb
Expand Up @@ -14,45 +14,52 @@ def test(name, version, platform, os, engine, engine_version, string)

describe UserAgent do

test :Safari, '4.0dp1', :Windows, :'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, :'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, :'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', :Macintosh, :'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, :'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, :'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'

test :Safari, '4.0.4', :iPad, :'iPad OS 3.2', :webkit, '531.21.10', 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10'
test :Safari, '4.0.4', :iPad, :'iPad OS 3.2', :webkit, '531.21.10', 'Mozilla/5.0 (iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10'

test :Safari, '4.0', :iPhone, :'iPhone OS 3.0', :webkit, '528.18', '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'

test :IE, '8.0', :Windows, :'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, :'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, :'Windows XP', :msie, '6.0b', 'Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1)'
test :IE, '6.0', :Windows, :'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, :'Windows XP', :presto, '9.9.9', 'Opera/9.99 (Windows NT 5.1; U; pl) Presto/9.9.9'
test :Opera, '9.70', :Linux, :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, :Linux, :presto, '2.1.1', 'Opera/9.64 (X11; Linux i686; U; Linux Mint; it) Presto/2.1.1'
test :Opera, '9.00', :Wii, :Wii, :unknown, nil, 'Opera/9.00 (Nintindo Wii; U; ; 103858; Wii Shop Channel/1.0; en)'

test :Chrome, '4.0.202.2', :Linux, :Linux, :webkit, '532.0', 'Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/4.0.202.2 Safari/532.0'
test :Chrome, '0.2.149.27', :Windows, :'Windows 2003', :webkit, '525.13', 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13'
test :Chrome, '0.2.149.30', :Windows, :'Windows Vista', :webkit, '525.13', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.30 Safari/525.13'

test :Konqueror, '4.2', :Linux, :Linux, :khtml, '4.2.4', 'Mozilla/5.0 (compatible; Konqueror/4.2; Linux; X11; x86_64) KHTML/4.2.4 (like Gecko) Fedora/4.2.4-2.fc11'
test :Konqueror, '3.1-rc6', :Linux, :Linux, :konqueror, '3.1-rc6', 'Mozilla/5.0 (compatible; Konqueror/3.1-rc6; i686 Linux; 20021105)'

test :PS3, '2.00', :Playstation, :Playstation, :unknown, nil, 'Mozilla/5.0 (PLAYSTATION 3; 2.00)'
test :PS3, '1.10', :Playstation, :Playstation, :unknown, nil, 'Mozilla/5.0 (PLAYSTATION 3; 1.10)'

test :PSP, '2.00', :Playstation, :Playstation, :unknown, nil, 'PSP (PlayStation Portable); 2.00'
test :PSP, '2.00', :Playstation, :Playstation, :unknown, nil, 'Mozilla/4.0 (PSP (PlayStation Portable); 2.00)'

test :Firefox, '3.5', :Linux, :Linux, :gecko, '20090624', 'Mozilla/5.0 (X11;U; Linux i686; en-GB; rv:1.9.1) Gecko/20090624 Ubuntu/9.04 (jaunty) Firefox/3.5'
test :Firefox, '3.5', :Windows, :'Windows 7', :gecko, '20090612', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1) Gecko/20090612 Firefox/3.5'
test :Firefox, '3.1', :Windows, :'Windows XP', :gecko, '2009011606', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.6pre) Gecko/2009011606 Firefox/3.1'
test :Firefox, '3.0', :Linux, :Linux, :gecko, '2008062315', 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9) Gecko/2008062315 (Gentoo) Firefox/3.0'
test :Firefox, '2.0', :Linux, :Linux, :gecko, '20061202', 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1) Gecko/20061202 Firefox/2.0'
test :safari, '4.0dp1', :windows, '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, '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, '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', :macintosh, '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, '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, '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'
test :safari, '4.0.4', :ipad, 'iPad OS 3.2', :webkit, '531.21.10', 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10'
test :safari, '4.0.4', :ipad, 'iPad OS 3.2', :webkit, '531.21.10', 'Mozilla/5.0 (iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10'
test :safari, '4.0', :iphone, 'iPhone OS 3.0', :webkit, '528.18', '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'

test :ie, '8.0', :windows, '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, '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, '7.0', :windows, 'Windows XP', :msie, '7.0', "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2)"
test :ie, '7.0', :windows, 'Windows XP', :msie, '7.0', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; MSOffice 12)'
test :ie, '6.0b', :windows, 'Windows XP', :msie, '6.0b', 'Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1)'
test :ie, '6.0', :windows, '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, 'Windows XP', :presto, '9.9.9', 'Opera/9.99 (Windows NT 5.1; U; pl) Presto/9.9.9'
test :opera, '9.70', :linux, '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, 'Linux', :presto, '2.1.1', 'Opera/9.64 (X11; Linux i686; U; Linux Mint; it) Presto/2.1.1'
test :opera, '9.00', :wii, 'Wii', :unknown, nil, 'Opera/9.00 (Nintindo Wii; U; ; 103858; Wii Shop Channel/1.0; en)'

test :chrome, '6.0.472.62', :macintosh, 'OS X 10.6', :webkit, '534.3', 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.62 Safari/534.3'
test :chrome, '6.0.472.63', :macintosh, 'OS X 10.6', :webkit, '534.3', 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3'
test :chrome, '6.0.472.55', :linux, 'Linux', :webkit, '534.3', 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.55 Safari/534.3'
test :chrome, '5.0.375.127', :windows, 'Windows XP', :webkit, '533.4', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.127 Safari/533.4'
test :chrome, '6.0.472.59', :windows, 'Windows XP', :webkit, '534.3', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.59 Safari/534.3'
test :chrome, '6.0.472.53', :linux, 'Linux', :webkit, '534.3', 'Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.53 Safari/534.3'
test :chrome, '4.0.202.2', :linux, 'Linux', :webkit, '532.0', 'Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/4.0.202.2 Safari/532.0'
test :chrome, '0.2.149.27', :windows, 'Windows 2003', :webkit, '525.13', 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13'
test :chrome, '0.2.149.30', :windows, 'Windows Vista', :webkit, '525.13', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.30 Safari/525.13'

test :konqueror, '4.2', :linux, 'Linux', :khtml, '4.2.4', 'Mozilla/5.0 (compatible; Konqueror/4.2; Linux; X11; x86_64) KHTML/4.2.4 (like Gecko) Fedora/4.2.4-2.fc11'
test :konqueror, '3.1-rc6', :linux, 'Linux', :konqueror, '3.1-rc6', 'Mozilla/5.0 (compatible; Konqueror/3.1-rc6; i686 Linux; 20021105)'

test :ps3, '2.00', :playstation, 'Playstation', :unknown, nil, 'Mozilla/5.0 (PLAYSTATION 3; 2.00)'
test :ps3, '1.10', :playstation, 'Playstation', :unknown, nil, 'Mozilla/5.0 (PLAYSTATION 3; 1.10)'

test :psp, '2.00', :playstation, 'Playstation', :unknown, nil, 'PSP (PlayStation Portable); 2.00'
test :psp, '2.00', :playstation, 'Playstation', :unknown, nil, 'Mozilla/4.0 (PSP (PlayStation Portable); 2.00)'

test :firefox, '3.5.13', :windows, 'Windows XP', :gecko, '20100914', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.13) Gecko/20100914 Firefox/3.5.13 (.NET CLR 3.5.30729)'
test :firefox, '3.6.10', :windows, 'Windows XP', :gecko, '20100914', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pt-BR; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 GTB7.1'
test :firefox, '3.6.10', :windows, 'Windows Vista', :gecko, '20100914', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; pt-BR; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 GTB7.1 ( .NET CLR 3.5.30729)'
test :firefox, '3.6.8', :linux, 'Linux', :gecko, '20100723', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.8) Gecko/20100723 Ubuntu/9.10 (karmic) Firefox/3.6.8'
test :firefox, '3.6.9', :linux, 'Linux', :gecko, '20100824', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.9) Gecko/20100824 Firefox/3.6.9'
test :firefox, '3.6.9', :linux, 'Linux', :gecko, '20100825', 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.9) Gecko/20100825 Ubuntu/10.04 (lucid) Firefox/3.6.9'

end

0 comments on commit fb2d4bd

Please sign in to comment.