Permalink
Browse files

Support for Chrome

  • Loading branch information...
1 parent abcae0b commit 84d8fae16dc0256dcc424c8d5b17537830d9e3c6 @jbritten committed Apr 5, 2012
Showing with 17 additions and 4 deletions.
  1. +14 −4 lib/user_agent.rb
  2. +3 −0 spec/user_agent_spec.rb
View
18 lib/user_agent.rb
@@ -1,5 +1,5 @@
class UserAgent
- VERSION = '0.0.1'
+ VERSION = '0.0.2'
attr_reader :browser_name, :browser_version
attr_reader :os_name, :os_version
@@ -41,6 +41,7 @@ def extract_products_from_agent_string
def identify_browser
identify_browser_opera or
+ identify_browser_chrome or
identify_browser_safari or
identify_browser_honest or
identify_browser_compatible or
@@ -105,6 +106,13 @@ def identify_browser_mozilla
end
end
+ def identify_browser_chrome
+ if browser = @products.detect{|product| product[0] == 'Chrome'}
+ @browser_version = browser[1]
+ @browser_name = 'Chrome'
+ end
+ end
+
def identify_browser_other
@browser_version = @products.first[1]
@browser_name = @products.first[0]
@@ -130,6 +138,7 @@ def identify_os_windows
when /NT 5.0/: '2000'
when /NT 5.1/: 'XP'
when /NT 6.0/: 'Vista'
+ when /NT 6.1/: '7'
end
end
@@ -142,9 +151,10 @@ def identify_os_mac
else
'Mac OS X'
end
-
- if element =~ /(10_._.)/
- @os_version = $1.gsub('_','.')
+
+ @os_version = case element
+ when /(10.+)/: $1.gsub('_','.')
+ when /(10.+)/: $1
end
end
View
3 spec/user_agent_spec.rb
@@ -24,6 +24,8 @@
"Safari" => "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; fr) AppleWebKit/412.7 (KHTML, like Gecko) Safari/412.5",
"Safari" => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092816 Mobile Safari 1.1.3",
"Safari" => "Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_1 like Mac OS X; fr-fr) AppleWebKit/525.18.1 (KHTML, like Gecko) Mobile/5F136",
+ "Chrome 0.2.149.27" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13",
+ "Chrome" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13",
"" => ""
}
examples.keys.sort.each do |expected|
@@ -40,6 +42,7 @@
"Linux" => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092816 Mobile Safari 1.1.3",
"Mac OS X 10.5.2" => "Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_5_2; en-gb) AppleWebKit/526+ (KHTML, like Gecko) Version/3.1 iPhone",
"Mac OS X 10.5.5" => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_5; sv-se) AppleWebKit/525.26.2 (KHTML, like Gecko) Version/3.2 Safari/525.26.12",
+ "Mac OS X 10.5.5" => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5.5; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5",
"Mac OS X" => "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; fr) AppleWebKit/412.7 (KHTML, like Gecko) Safari/412.5",
"OpenBSD" => "Mozilla/5.0 (X11; U; OpenBSD i386; en-US; rv:1.8.1.7) Gecko/20070930 Firefox/2.0.0.7",
"Windows 2000" => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; en) Opera 8.0",

0 comments on commit 84d8fae

Please sign in to comment.