Skip to content

Instantly share code, notes, and snippets.

@bemurphy bemurphy/uap_mp.rb
Created Sep 2, 2013

Embed
What would you like to do?
#######
# Monkeypatch the user_agent_parser
#######
# Memoizes the pattern load because otherwise
# parsing user agents will read the file
# and build regex every time at the cost
# of 35ms
require "user_agent_parser"
module UserAgentParser
class Parser
def load_patterns(path)
if defined?(@@_patterns)
return @@_patterns
end
yml = YAML.load_file(path)
# Parse all the regexs
yml.each_pair do |type, patterns|
patterns.each do |pattern|
pattern["regex"] = Regexp.new(pattern["regex"])
end
end
@@_patterns = [ yml["user_agent_parsers"], yml["os_parsers"], yml["device_parsers"] ]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.