Skip to content

Commit

Permalink
Expose parse_os, parse_device and parse_ua (#108)
Browse files Browse the repository at this point in the history
* Expose parse_os, parse_device and parse_ua

* Add specs for new public Parser methods
  • Loading branch information
daniel-statsig authored Apr 13, 2023
1 parent c62bbe9 commit 654a3a9
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 37 deletions.
60 changes: 30 additions & 30 deletions lib/user_agent_parser/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,36 @@ def parse(user_agent)
parse_ua(user_agent, os, device)
end

def parse_os(user_agent)
pattern, match = first_pattern_match(@os_patterns, user_agent)

if match
os_from_pattern_match(pattern, match)
else
OperatingSystem.new
end
end

def parse_device(user_agent)
pattern, match = first_pattern_match(@device_patterns, user_agent)

if match
device_from_pattern_match(pattern, match)
else
Device.new
end
end

def parse_ua(user_agent, os = nil, device = nil)
pattern, match = first_pattern_match(@ua_patterns, user_agent)

if match
user_agent_from_pattern_match(pattern, match, os, device)
else
UserAgent.new(nil, nil, os, device)
end
end

def patterns_path
patterns_paths.first
end
Expand Down Expand Up @@ -76,36 +106,6 @@ def parse_pattern(patterns)
end
end

def parse_ua(user_agent, os = nil, device = nil)
pattern, match = first_pattern_match(@ua_patterns, user_agent)

if match
user_agent_from_pattern_match(pattern, match, os, device)
else
UserAgent.new(nil, nil, os, device)
end
end

def parse_os(user_agent)
pattern, match = first_pattern_match(@os_patterns, user_agent)

if match
os_from_pattern_match(pattern, match)
else
OperatingSystem.new
end
end

def parse_device(user_agent)
pattern, match = first_pattern_match(@device_patterns, user_agent)

if match
device_from_pattern_match(pattern, match)
else
Device.new
end
end

def first_pattern_match(patterns, value)
patterns.each do |pattern|
return [pattern, pattern[:regex].match(value)] if pattern[:regex].match?(value)
Expand Down
90 changes: 83 additions & 7 deletions spec/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ def self.file_to_test_cases(file)
file_to_yaml(file)['test_cases'].map do |test_case|
{
'user_agent_string' => test_case['user_agent_string'],
'family' => test_case['family'],
'major' => test_case['major'],
'minor' => test_case['minor'],
'patch' => test_case['patch'],
'patch_minor' => test_case['patch_minor'],
'brand' => test_case['brand'],
'model' => test_case['model']
'family' => test_case['family'],
'major' => test_case['major'],
'minor' => test_case['minor'],
'patch' => test_case['patch'],
'patch_minor' => test_case['patch_minor'],
'brand' => test_case['brand'],
'model' => test_case['model']
}
end.reject do |test_case|
# We don't do the hacky javascript user agent overrides
Expand Down Expand Up @@ -177,4 +177,80 @@ def other_patterns_path
end
end
end

describe '#parse_os' do
operating_system_test_cases.each do |test_case|
it "parses OS for #{test_case_to_test_name(test_case)}" do
operating_system = PARSER.parse_os(test_case['user_agent_string'])

if test_case['family']
_(operating_system.family).must_equal_test_case_property(test_case, 'family')
end

if test_case['major']
_(operating_system.version.major).must_equal_test_case_property(test_case, 'major')
end

if test_case['minor']
_(operating_system.version.minor).must_equal_test_case_property(test_case, 'minor')
end

if test_case['patch']
_(operating_system.version.patch).must_equal_test_case_property(test_case, 'patch')
end

if test_case['patch_minor']
_(operating_system.version.patch_minor).must_equal_test_case_property(test_case, 'patch_minor')
end
end
end
end

describe '#parse_device' do
device_test_cases.each do |test_case|
it "parses device for #{test_case_to_test_name(test_case)}" do
device = PARSER.parse_device(test_case['user_agent_string'])

if test_case['family']
_(device.family).must_equal_test_case_property(test_case, 'family')
end

if test_case['model']
_(device.model).must_equal_test_case_property(test_case, 'model')
end

if test_case['brand']
_(device.brand).must_equal_test_case_property(test_case, 'brand')
end
end
end
end

describe '#parse_ua' do
user_agent_test_cases.each do |test_case|
it "parses UA for #{test_case_to_test_name(test_case)}" do
user_agent = PARSER.parse_ua(test_case['user_agent_string'])

assert_nil user_agent.os
assert_nil user_agent.device

if test_case['family']
_(user_agent.family).must_equal_test_case_property(test_case, 'family')
end

if test_case['major']
_(user_agent.version.major).must_equal_test_case_property(test_case, 'major')
end

if test_case['minor']
_(user_agent.version.minor).must_equal_test_case_property(test_case, 'minor')
end

if test_case['patch']
_(user_agent.version.patch).must_equal_test_case_property(test_case, 'patch')
end
end
end
end

end

0 comments on commit 654a3a9

Please sign in to comment.