diff --git a/cv-user-agent.gemspec b/cv-user-agent.gemspec index 6bbfa83..df822ba 100644 --- a/cv-user-agent.gemspec +++ b/cv-user-agent.gemspec @@ -3,14 +3,14 @@ require 'user-agent/version' Gem::Specification.new do |s| - s.name = 'cv-user-agent' + s.name = 'podman-user-agent' s.version = UserAgent::VERSION - s.authors = ['Carlos Villela'] - s.email = 'cvillela@thoughtworks.com' - s.homepage = 'http://github.com/cv/user-agent' + s.authors = ['Adam Podolnick'] + s.email = 'podman@gmail.com' + s.homepage = 'http://github.com/podman/user-agent' s.summary = 'User agent parser' s.description = 'user-agent is a user agent parser support most of the commonly used browsers today.' - s.rubyforge_project = 'cv-user-agent' + s.rubyforge_project = 'podman-user-agent' s.require_paths = ['lib'] s.files = `git ls-files`.split("\n") diff --git a/lib/user-agent/agent.rb b/lib/user-agent/agent.rb index f3ca282..7686cdf 100644 --- a/lib/user-agent/agent.rb +++ b/lib/user-agent/agent.rb @@ -32,6 +32,10 @@ def platform ParsedUserAgent.platform_for_user_agent string end + def device + ParsedUserAgent.device_for_user_agent string + end + def to_s string end @@ -45,16 +49,21 @@ def == other end def self.engine_version_for_user_agent string - $1 if string =~ /#{engine_for_user_agent(string)}[\/ ]([\d\w\.\-]+)/i + e = engine_for_user_agent(string) + e = 'trident' if string =~ /trident/i + $1 if string =~ /#{e}[\/ ]([\d\w\.\-]+)/i end def self.version_for_user_agent string case name = name_for_user_agent(string) + when :ChromeFrame ; $1 if string =~ /chromeframe\/([\d\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 + when :"IE Mobile"; $1 if string =~ /iemobile\/([\d\.]+)/i + when :Edge ; $1 if string =~ /edge\/([\d]+)/i + else $1 if string =~ /#{name}[\/ ]([\d\w\.\-]+)/i || string =~ /rv\:([\d\.]+)/i end end @@ -65,6 +74,7 @@ def self.engine_for_user_agent string when /konqueror/i ; :konqueror when /chrome/i ; :chrome when /presto/i ; :presto + when /trident.*like gecko/i; :msie when /gecko/i ; :gecko when /msie/i ; :msie else :Unknown @@ -74,30 +84,51 @@ 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 6\.2/i ; :'Windows 8' + when /windows nt 6\.3/i ; :'Windows 8.1' + when /windows nt 10\.0/i ; :'Windows 10' + when /windows nt 6\.1/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 /windows phone os ([^;]+);/i ; :"Windows Phone OS #{$1}" when /os x (\d+)[._](\d+)/i ; :"OS X #{$1}.#{$2}" + when /android ([^;]+);/i ; :"Android #{$1}" 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}" + when /ipad.*os (\d+)[._](\d+)[._](\d+)/i; :"iOS #{$1}.#{$2}.#{$3}" + when /\(ipad.*os (\d+)[._](\d+)/i ; :"iOS #{$1}.#{$2}" + when /iphone.*os (\d+)[._](\d+)[._](\d+)/i; :"iOS #{$1}.#{$2}.#{$3}" + when /\iphone.*os (\d+)[._](\d+)/i ; :"iOS #{$1}.#{$2}" + when /webos\/([^;]+);/i ; :"webOS #{$1}" + when /os x/i ; :"OS X" + when /cros i\d{3} ([^\)]+)\)/i ; :"ChromeOS #{$1}" + when /rim tablet os ([^;]+);/i ; :"RIM Tablet OS #{$1}" + when /blackberry(\d+)\/([^\s]+)\s/i ; :"RIM OS #{$2}" + when /blackberry ([^;]+);/i ; :"RIM OS" else ; :Unknown end end def self.platform_for_user_agent string case string + when /xbox/i ; :Xbox + when /windows phone/i; :"Windows Phone" when /windows/i ; :Windows when /macintosh/i ; :Macintosh + when /android/i ; :Android when /linux/i ; :Linux when /wii/i ; :Wii when /playstation/i ; :Playstation + when /ipod/i ; :iPod when /ipad/i ; :iPad when /iphone/i ; :iPhone + when /blackberry/i ; :BlackBerry + when /playbook/i ; :PlayBook + when /webos/i ; :webOS + when /cros/i ; :ChromeOS else :Unknown end end @@ -105,17 +136,33 @@ def self.platform_for_user_agent string def self.name_for_user_agent string case string when /konqueror/i ; :Konqueror + when /chromeframe/i ; :ChromeFrame + when /edge/i ; :Edge when /chrome/i ; :Chrome + when /mobile safari/i ; :"Mobile Safari" when /safari/i ; :Safari + when /iemobile/i ; :"IE Mobile" when /msie/i ; :IE + when /trident.*rv\:11/i ; :IE # "Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko" when /opera/i ; :Opera when /playstation 3/i ; :PS3 when /playstation portable/i ; :PSP when /firefox/i ; :Firefox + when /ipad|iphone|ipod/i ; :"Mobile Safari" + when /blackberry/i ; :"BlackBerrry" else ; :Unknown end end + def self.device_for_user_agent string + case platform = platform_for_user_agent(string) + when :Windows, :Macintosh, :Linux, :ChromeOS ; :Desktop + when :iPod, :iPad, :iPhone, :BlackBerry, :PlayBook, :Android, :webOS, :"Windows Phone" ; :Mobile + when :Wii, :Playstation, :Xbox ; :"Game Console" + else ; :Unknown + end + end + @agents = [] def self.map name, options = {} diff --git a/lib/user-agent/version.rb b/lib/user-agent/version.rb index cb33770..a7b35b2 100644 --- a/lib/user-agent/version.rb +++ b/lib/user-agent/version.rb @@ -1,3 +1,3 @@ module UserAgent - VERSION = '1.1.0' + VERSION = '1.2.3' end diff --git a/spec/agents_spec.rb b/spec/agents_spec.rb index 1d14e84..9ee6f3f 100644 --- a/spec/agents_spec.rb +++ b/spec/agents_spec.rb @@ -21,18 +21,33 @@ def test name, version, platform, os, engine, engine_version, string 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.4', :iPad, :'iOS 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, :'iOS 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 :Safari, '4.0', :iPhone, :'iOS 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, '11.0', :Windows, :'Windows 8', :msie, '7.0', 'Mozilla/5.0 (Windows NT 6.2; Trident/7.0; rv:11.0) like Gecko' + test :IE, '11.0', :Windows, :'Windows 7', :msie, '7.0', 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko' + test :IE, '10.0', :Windows, :'Windows 8', :msie, '6.0', 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)' + test :IE, '10.0', :Windows, :'Windows 7', :msie, '6.0', 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)' + test :IE, '9.0', :Windows, :'Windows 7', :msie, '5.0', 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)' + test :IE, '8.0', :Windows, :'Windows 7', :msie, '4.0', '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)' 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 :IE, '11.0', :Windows, :'Windows 10', :msie, '7.0', ' +Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko' + + test :IE, '11.0', :Windows, :'Windows 8.1', :msie, '7.0', 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko' + + test :IE, '10.0', :Xbox, :'Windows 8', :msie, '6.0', 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; Xbox; Xbox One)' + + test :Edge, '14', :Windows, :'Windows 10', :webkit, '537.36', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/14.14300' + + 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'