diff --git a/lib/user_agent_parser/parser.rb b/lib/user_agent_parser/parser.rb index 5b3dee8..eed5529 100644 --- a/lib/user_agent_parser/parser.rb +++ b/lib/user_agent_parser/parser.rb @@ -38,16 +38,20 @@ def parse(user_agent) private def load_patterns(path) - yml = YAML.load_file(path) + yml = YAML.load_file(path, freeze: true) + [ + parse_pattern(yml['user_agent_parsers']), + parse_pattern(yml['os_parsers']), + parse_pattern(yml['device_parsers']), + ] + end - # Parse all the regexs - yml.each_pair do |_type, patterns| - patterns.each do |pattern| - pattern[:regex] = Regexp.new(pattern['regex'], pattern['regex_flag'] == 'i') - end + def parse_pattern(patterns) + patterns.map do |pattern| + pattern = pattern.dup + pattern[:regex] = Regexp.new(pattern.delete('regex'), pattern.delete('regex_flag') == 'i') + pattern end - - [yml['user_agent_parsers'], yml['os_parsers'], yml['device_parsers']] end def parse_ua(user_agent, os = nil, device = nil)