Permalink
Browse files

All green

  • Loading branch information...
1 parent d8cd074 commit 64b4a069e864ea3fad6c02ea629056fa7122968e @tj committed Aug 28, 2009
Showing with 45 additions and 12 deletions.
  1. +25 −10 lib/user-agent/agent.rb
  2. +20 −2 spec/agent_spec.rb
View
@@ -25,17 +25,24 @@ def name
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 version string.
+ # User agent engine version string.
- def version
- Agent.version_for_user_agent string
+ def engine_version
+ Agent.engine_version_for_user_agent string
end
##
@@ -56,7 +63,7 @@ def to_s
# Inspect.
def inspect
- "#<Agent name:#{name.to_s.inspect} engine:#{engine.to_s.inspect} os:#{os.to_s.inspect} version:#{version.inspect}>"
+ "#<Agent:#{name} version:#{version.inspect} engine:\"#{engine.to_s}:#{engine_version}\" os:#{os.to_s.inspect}>"
end
##
@@ -71,10 +78,19 @@ def == other
#++
##
+ # 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
- $1 if string =~ /#{engine_for_user_agent(string)}\/([\d\w\.]+)/i
+ case string
+ when /safari/i ; $1 if string =~ /Version\/([\d\w\.]+)/i
+ end
end
##
@@ -112,12 +128,11 @@ def self.os_for_user_agent string
# Return name for user agent _string_.
def self.name_for_user_agent string
- version = version_for_user_agent string
- engine = engine_for_user_agent string
- info = @agents.find do |name, info|
- info[:engine] == engine && info[:version].match(version)
+ case string
+ when /chrome/i ; :Chrome
+ when /safari/i ; :Safari
+ else ; :Unknown
end
- info.first if info
end
@agents = []
View
@@ -24,9 +24,27 @@
end
end
- describe "#version" do
+ describe "#engine_version" do
it "should return engine version" do
- @agent.version.should == '528.4'
+ @agent.engine_version.should == '528.4'
+ end
+ end
+
+ describe "#to_s" do
+ it "should return the user agent string" do
+ @agent.to_s.should == @agent.string
+ end
+ end
+
+ describe "#inspect" do
+ it "should return string presenting the engine, os, version, etc" do
+ @agent.inspect.should == '#<Agent: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'
end
end
end

0 comments on commit 64b4a06

Please sign in to comment.