Permalink
Browse files

Allow initializing parser with patterns path.

No reason to lock all parsers down to a single patterns path file. This defaults to the global file, but allows each parser instance to have its own as well.
  • Loading branch information...
1 parent 6223bcb commit 90bd1659d15a11cc968d07acbd0ec9764e05c575 @jnunemaker committed Jan 4, 2013
Showing with 33 additions and 14 deletions.
  1. +15 −8 lib/user_agent_parser/parser.rb
  2. +18 −6 spec/parser_spec.rb
@@ -4,16 +4,23 @@ module UserAgentParser
class Parser
+ # Private: The path to the patterns yaml file that should be used.
+ attr_reader :patterns_path
+
+ def initialize(patterns_path = UserAgentParser.patterns_path)
+ @patterns_path = patterns_path
+ end
+
def parse user_agent
ua = parse_ua(user_agent)
ua.os = parse_os(user_agent)
ua
end
-
+
private
def all_patterns
- @all_patterns ||= YAML.load_file(UserAgentParser.patterns_path)
+ @all_patterns ||= YAML.load_file(@patterns_path)
end
def patterns type
@@ -24,7 +31,7 @@ def patterns type
end
end
end
-
+
def parse_ua user_agent
pattern, match = first_pattern_match(patterns("user_agent_parsers"), user_agent)
if match
@@ -33,7 +40,7 @@ def parse_ua user_agent
UserAgent.new
end
end
-
+
def parse_os user_agent
pattern, match = first_pattern_match(patterns("os_parsers"), user_agent)
if match
@@ -59,12 +66,12 @@ def user_agent_from_pattern_match pattern, match
end
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"]
+ v3 = pattern["v3_replacement"].sub('$1', v3 || '') if pattern["v3_replacement"]
ua = UserAgent.new(family)
ua.version = version_from_segments(v1, v2, v3)
ua
end
-
+
def os_from_pattern_match pattern, match
os, v1, v2, v3, v4 = match[1], match[2], match[3], match[4], match[5]
os = pattern["os_replacement"].sub('$1', os || '') if pattern["os_replacement"]
@@ -76,12 +83,12 @@ def os_from_pattern_match pattern, match
os.version = version_from_segments(v1, v2, v3, v4)
os
end
-
+
def version_from_segments(*segments)
version_string = segments.compact.join(".")
version_string.empty? ? nil : Version.new(version_string)
end
-
+
end
end
View
@@ -5,18 +5,30 @@
before do
@parser ||= UserAgentParser::Parser.new
end
-
+
def self.ua_string_to_test_name(ua)
- # Some Ruby versions (JRuby) need sanitised test names, as some chars screw
+ # Some Ruby versions (JRuby) need sanitised test names, as some chars screw
# up the test method definitions
ua.gsub(/[^a-z0-9_.-]/i,'_')
end
def self.test_case_test_name(tc)
ua_string_to_test_name(tc['user_agent_string'])
end
+ describe "#initialize" do
+ it "defaults patterns path file to global" do
+ parser = UserAgentParser::Parser.new
+ parser.patterns_path.must_equal(UserAgentParser.patterns_path)
+ end
+
+ it "allows overriding the global with a specific file" do
+ parser = UserAgentParser::Parser.new('some/path')
+ parser.patterns_path.must_equal('some/path')
+ end
+ end
+
describe "#parse" do
-
+
ua_parser_test_cases.each do |tc|
it "should parse UA for #{test_case_test_name(tc)}" do
ua = @parser.parse(tc['user_agent_string'])
@@ -26,7 +38,7 @@ def self.test_case_test_name(tc)
ua.version.patch.must_equal_test_case_property(tc, 'patch') if tc['patch']
end
end
-
+
os_parser_test_cases.each do |tc|
it "should parse OS for #{test_case_test_name(tc)}" do
ua = @parser.parse(tc['user_agent_string'])
@@ -37,7 +49,7 @@ def self.test_case_test_name(tc)
ua.os.version.patch_minor.must_equal_test_case_property(tc, 'patch_minor') if tc['patch_minor']
end
end
-
+
end
-
+
end

0 comments on commit 90bd165

Please sign in to comment.