-
-
Save adamkirkwood/324562 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserAgent | |
attr_reader :browser, :version, :os | |
def initialize(browser, version, os) | |
@browser = browser | |
@version = version | |
@os = os | |
end | |
def self.parse(uas) | |
browser, version = identify_browser_and_version(uas) | |
os = identify_operating_system(uas) | |
new browser, version, os | |
end | |
def self.identify_browser_and_version(uas) | |
if uas.match(/Chrome/) | |
["Chrome", uas.scan(/Chrome\/(.[^\s;()\[\]]+)/).flatten.compact.push("Unknown").first.strip] | |
elsif uas.match(/Safari\//) | |
["Safari", uas.scan(/Version\/(.[^\s;()\[\]]+)\s?|Safari\/(.[^\s;()\[\]]+)/).flatten.compact.push("Unknown").first.strip] | |
elsif uas.match(/Firefox/) | |
["Firefox", uas.scan(/Firefox\/(.[^\s;()\[\]]+)/).flatten.compact.push("Unknown").first.strip] | |
elsif uas.match(/Opera/) | |
["Opera", uas.scan(/Version\/(.[^\s;()\[\]]+)|Opera\/(.[^\s;()\[\]]+)\s|Opera\s(.[^\s;()\[\]]+)\s?/).flatten.compact.unshift("Unknown").last.strip] | |
elsif uas.match(/MSIE/) | |
["Internet Explorer", uas.scan(/MSIE\s(.[^\s;()\[\]]+)\;/).flatten.compact.push("Unknown").first.strip] | |
else | |
if uas.match(/AppleWebKit/) | |
["Unknown Browser using WebKit", uas.scan(/AppleWebKit\/(.[^\s;()\[\]]+)/).flatten.compact.push("Unknown").first.strip] | |
elsif uas.match(/KHTML/) | |
["Unknown Browser using KHTML", uas.scan(/KHTML\/(.[^\s;()\[\]]+)/).flatten.compact.push("Unknown").first.strip] | |
elsif uas.match(/Gecko/) | |
["Unknown Browser using Gecko", uas.scan(/Gecko\/(.[^\s;()\[\]]+)/).flatten.compact.push("Unknown").first.strip] | |
else | |
["", ""] | |
end | |
end | |
end | |
def self.identify_operating_system(uas) | |
if uas.match(/Mac\sOS\sX/) || uas.match(/Mac_PowerPC/) || uas.match(/Macintosh/) | |
macintosh_version(uas) | |
elsif uas.match(/SunOS/) | |
"SunOS" | |
elsif uas.match(/OpenBSD/) | |
"OpenBSD" | |
elsif uas.match(/Linux/) || uas.match(/X11/) | |
"Linux" | |
elsif uas.match(/Win/) | |
windows_version(uas) | |
else | |
"" | |
end | |
end | |
def self.macintosh_version(uas) | |
version = uas.scan(/Mac\sOS\sX\s(.[^\s;()\[\]]+)/).flatten.compact.push("").first.gsub("_", ".").strip | |
version = case version.to_s | |
when /10\.1/ | |
"Puma" | |
when /10\.2/ | |
"Jaguar" | |
when /10\.3/ | |
"Panther" | |
when /10\.4/ | |
"Tiger" | |
when /10\.5/ | |
"Leopard" | |
when /10\.6/ | |
"Snow Leopard" | |
else | |
version | |
end | |
return "MacOSX #{version}".strip | |
end | |
def self.windows_version(uas) | |
version = uas.scan(/Windows\sNT\s(.[^;()]+)|Windows\s(.[^;()]+)|WinNT(.[^;()]+)\;/).flatten.compact.push("").first.strip | |
version = case version | |
when "7.0" then " 7" | |
when "6.0" then " Vista" | |
when "5.2" then " Server 2003; Windows XP x64 Edition" | |
when "5.1" then " XP" | |
when "5.01" then " 2000, Service Pack 1 (SP1)" | |
when "5.0" then " 2000" | |
when "4.0" then " NT 4.0" | |
when "98" then " 98" | |
when "95" then " 95" | |
when "CE" then " CE" | |
else " #{version}" | |
end | |
return "Windows#{version}".strip | |
end | |
def browser_string | |
if @version == "Unknown" | |
@browser | |
else | |
"#{@browser} v#{@version}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment