Created
September 2, 2013 00:43
-
-
Save bemurphy/6408305 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
####### | |
# 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