Skip to content

Commit

Permalink
Adding support for firefox.
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglas Meyer committed May 10, 2010
1 parent f61cecd commit 7d52a64
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 44 deletions.
6 changes: 5 additions & 1 deletion History.rdoc
@@ -1,8 +1,12 @@

=== 2010-05-07

* Support for Firefox

=== 1.0.0 / 2009-10-08

* Specs for Agent#==

=== 0.0.1 / 2009-08-28

* Initial release
* Initial release
71 changes: 36 additions & 35 deletions lib/user-agent/agent.rb
@@ -1,92 +1,92 @@

class Agent

##
# User agent string.

attr_reader :string

##
# Initialize with user agent _string_.

def initialize string
@string = string.strip
end

#--
# Instance methods
#++

##
# User agent name symbol.

def name
Agent.name_for_user_agent string
end

##
# User agent version.

def version
Agent.version_for_user_agent string
end

##
# User agent engine symbol.

def engine
Agent.engine_for_user_agent string
end

##
# User agent engine version string.

def engine_version
Agent.engine_version_for_user_agent string
end

##
# User agent os symbol.

def os
Agent.os_for_user_agent string
end

##
# User agent string.

def to_s
string
end

##
# Inspect.

def inspect
"#<Agent:#{name} version:#{version.inspect} engine:\"#{engine.to_s}:#{engine_version}\" os:#{os.to_s.inspect}>"
end

##
# Check if the agent is the same as _other_ agent.

def == other
string == other.string
end

#--
# Class methods
#++

##
# 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
end

##
# Return 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
Expand All @@ -96,10 +96,10 @@ def self.version_for_user_agent string
else $1 if string =~ /#{name}[\/ ]([\d\w\.\-]+)/i
end
end

##
# Return engine symbol for user agent _string_.

def self.engine_for_user_agent string
case string
when /webkit/i ; :webkit
Expand All @@ -112,10 +112,10 @@ def self.engine_for_user_agent string
else :unknown
end
end

##
# Return the os for user agent _string_.

def self.os_for_user_agent string
case string
when /windows nt 6\.0/i ; :'Windows Vista'
Expand All @@ -131,10 +131,10 @@ def self.os_for_user_agent string
else ; :Unknown
end
end

##
# Return name for user agent _string_.

def self.name_for_user_agent string
case string
when /konqueror/i ; :Konqueror
Expand All @@ -144,15 +144,16 @@ def self.name_for_user_agent string
when /opera/i ; :Opera
when /playstation 3/i ; :PS3
when /playstation portable/i ; :PSP
when /firefox/i ; :Firefox
else ; :Unknown
end
end

@agents = []

##
# Map agent _name_ to _options_.

def self.map name, options = {}
@agents << [name, options]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/user-agent/version.rb
@@ -1,4 +1,4 @@

class Agent
VERSION = '1.0.0'
end
end
20 changes: 13 additions & 7 deletions spec/agents_spec.rb
Expand Up @@ -13,19 +13,19 @@ def test name, version, os, engine, engine_version, string
end

describe Agent do

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'

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'
Expand All @@ -37,11 +37,17 @@ def test name, version, os, engine, engine_version, string

test :Konqueror, '4.2', :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, :konqueror, '3.1-rc6', 'Mozilla/5.0 (compatible; Konqueror/3.1-rc6; i686 Linux; 20021105)'

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

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

end

test :Firefox, '3.5', :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 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 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, :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, :gecko, '20061202', 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1) Gecko/20061202 Firefox/2.0'

end

0 comments on commit 7d52a64

Please sign in to comment.