Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Cleaned up user agent in same fashion as operating system.

  • Loading branch information...
commit 59e6310889b3ca594ea5b462cc82e41cae245075 1 parent f315a0c
John Nunemaker authored
16 lib/user_agent_parser/parser.rb
View
@@ -12,9 +12,8 @@ def initialize(patterns_path = UserAgentParser.patterns_path)
end
def parse user_agent
- ua = parse_ua(user_agent)
- ua.os = parse_os(user_agent)
- ua
+ os = parse_os(user_agent)
+ parse_ua(user_agent, os)
end
private
@@ -32,10 +31,10 @@ def patterns type
end
end
- def parse_ua user_agent
+ def parse_ua user_agent, os = nil
pattern, match = first_pattern_match(patterns("user_agent_parsers"), user_agent)
if match
- user_agent_from_pattern_match(pattern, match)
+ user_agent_from_pattern_match(pattern, match, os)
else
UserAgent.new
end
@@ -59,7 +58,7 @@ def first_pattern_match patterns, value
nil
end
- def user_agent_from_pattern_match pattern, match
+ def user_agent_from_pattern_match pattern, match, os
family, v1, v2, v3 = match[1], match[2], match[3], match[4]
if pattern["family_replacement"]
family = pattern["family_replacement"].sub('$1', family || '')
@@ -67,9 +66,8 @@ def user_agent_from_pattern_match pattern, match
v1 = pattern["v1_replacement"].sub('$1', v1 || '') if pattern["v1_replacement"]
v2 = pattern["v2_replacement"].sub('$1', v2 || '') if pattern["v2_replacement"]
v3 = pattern["v3_replacement"].sub('$1', v3 || '') if pattern["v3_replacement"]
- ua = UserAgent.new(family)
- ua.version = version_from_segments(v1, v2, v3)
- ua
+ version = version_from_segments(v1, v2, v3)
+ UserAgent.new(family, version, os)
end
def os_from_pattern_match pattern, match
28 lib/user_agent_parser/user_agent.rb
View
@@ -1,33 +1,31 @@
module UserAgentParser
-
class UserAgent
+ attr_reader :family, :version, :os
- attr_accessor :family, :version, :os
-
- def initialize(family="Other", version=nil, os=nil)
- self.family = family
- self.version = version
- self.os = os
+ def initialize(family = 'Other', version = nil, os = nil)
+ @family = family
+ @version = version
+ @os = os
end
def to_s
- s = family
- s += " #{version}" if version
- s
+ string = family
+ string += " #{version}" if version
+ string
end
def inspect
- s = to_s
- s += " (#{os})" if os
- "#<#{self.class} #{s}>"
+ string = to_s
+ string += " (#{os})" if os
+ "#<#{self.class} #{string}>"
end
-
+
def ==(other)
family == other.family &&
version == other.version &&
os == other.os
end
+ alias_method :eql?, :==
end
-
end
93 spec/user_agent_spec.rb
View
@@ -5,28 +5,97 @@
it "returns a string of just the family" do
UserAgentParser::UserAgent.new("Chrome").to_s.must_equal "Chrome"
end
+
it "returns a string of family and version" do
- v = UserAgentParser::Version.new("1.2.3pre")
- UserAgentParser::UserAgent.new("Chrome", v).to_s.must_equal "Chrome 1.2.3pre"
+ version = UserAgentParser::Version.new("1.2.3pre")
+ agent = UserAgentParser::UserAgent.new("Chrome", version)
+ agent.to_s.must_equal "Chrome 1.2.3pre"
end
end
+
describe "#==" do
- it "should return true for same user agents across different O/S's" do
- ua1 = UserAgentParser::UserAgent.new("Chrome", UserAgentParser::Version.new("1.0"))
- ua2 = UserAgentParser::UserAgent.new("Chrome", UserAgentParser::Version.new("1.0"))
- ua1.must_equal ua2
+ it "returns true for same agents with no OS" do
+ version = UserAgentParser::Version.new("1.0")
+ agent1 = UserAgentParser::UserAgent.new("Chrome", version)
+ agent2 = UserAgentParser::UserAgent.new("Chrome", version)
+ agent1.must_equal agent2
+ end
+
+ it "returns true for same agents on same OS" do
+ version = UserAgentParser::Version.new("1.0")
+ os = UserAgentParser::OperatingSystem.new('Windows')
+ agent1 = UserAgentParser::UserAgent.new("Chrome", version, os)
+ agent2 = UserAgentParser::UserAgent.new("Chrome", version, os)
+ agent1.must_equal agent2
+ end
+
+ it "returns false for same agent on different OS" do
+ version = UserAgentParser::Version.new("1.0")
+ windows = UserAgentParser::OperatingSystem.new('Windows')
+ mac = UserAgentParser::OperatingSystem.new('Mac')
+ agent1 = UserAgentParser::UserAgent.new("Chrome", version, windows)
+ agent2 = UserAgentParser::UserAgent.new("Chrome", version, mac)
+ agent1.wont_equal agent2
+ end
+
+ it "returns false for same os, but different family version" do
+ family_version1 = UserAgentParser::Version.new("1.0")
+ family_version2 = UserAgentParser::Version.new("2.0")
+ os = UserAgentParser::OperatingSystem.new('Windows')
+ agent1 = UserAgentParser::UserAgent.new("Chrome", family_version1, os)
+ agent2 = UserAgentParser::UserAgent.new("Chrome", family_version2, os)
+ agent1.wont_equal agent2
end
end
- describe "#inspect" do
- before do
- @ua = UserAgentParser::UserAgent.new("Chrome", UserAgentParser::Version.new("1.0"))
+
+ describe "#eql?" do
+ it "returns true for same agents with no OS" do
+ version = UserAgentParser::Version.new("1.0")
+ agent1 = UserAgentParser::UserAgent.new("Chrome", version)
+ agent2 = UserAgentParser::UserAgent.new("Chrome", version)
+ assert_equal true, agent1.eql?(agent2)
end
+
+ it "returns true for same agents on same OS" do
+ version = UserAgentParser::Version.new("1.0")
+ os = UserAgentParser::OperatingSystem.new('Windows')
+ agent1 = UserAgentParser::UserAgent.new("Chrome", version, os)
+ agent2 = UserAgentParser::UserAgent.new("Chrome", version, os)
+ assert_equal true, agent1.eql?(agent2)
+ end
+
+ it "returns false for same agent on different OS" do
+ version = UserAgentParser::Version.new("1.0")
+ windows = UserAgentParser::OperatingSystem.new('Windows')
+ mac = UserAgentParser::OperatingSystem.new('Mac')
+ agent1 = UserAgentParser::UserAgent.new("Chrome", version, windows)
+ agent2 = UserAgentParser::UserAgent.new("Chrome", version, mac)
+ assert_equal false, agent1.eql?(agent2)
+ end
+
+ it "returns false for same os, but different family version" do
+ family_version1 = UserAgentParser::Version.new("1.0")
+ family_version2 = UserAgentParser::Version.new("2.0")
+ os = UserAgentParser::OperatingSystem.new('Windows')
+ agent1 = UserAgentParser::UserAgent.new("Chrome", family_version1, os)
+ agent2 = UserAgentParser::UserAgent.new("Chrome", family_version2, os)
+ assert_equal false, agent1.eql?(agent2)
+ end
+ end
+
+ describe "#inspect" do
it "should return the family and version" do
- @ua.inspect.to_s.must_equal '#<UserAgentParser::UserAgent Chrome 1.0>'
+ family_version = UserAgentParser::Version.new("1.0")
+ agent = UserAgentParser::UserAgent.new("Chrome", family_version)
+ agent.inspect.to_s.must_equal '#<UserAgentParser::UserAgent Chrome 1.0>'
end
+
it "should return the OS if present" do
- @ua.os = UserAgentParser::OperatingSystem.new("OS X", UserAgentParser::Version.new("10.7.4"))
- @ua.inspect.must_equal "#<UserAgentParser::UserAgent Chrome 1.0 (OS X 10.7.4)>"
+ family_version = UserAgentParser::Version.new("1.0")
+ os_version = UserAgentParser::Version.new("10.7.4")
+ os = UserAgentParser::OperatingSystem.new("OS X", os_version)
+ agent = UserAgentParser::UserAgent.new("Chrome", family_version, os)
+ agent.inspect.must_equal "#<UserAgentParser::UserAgent Chrome 1.0 (OS X 10.7.4)>"
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.