Skip to content

Instantly share code, notes, and snippets.

@bemurphy
Created September 2, 2013 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bemurphy/6408305 to your computer and use it in GitHub Desktop.
Save bemurphy/6408305 to your computer and use it in GitHub Desktop.
#######
# 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