class Browser::Bot
def self.bot_exceptions
def self.bot_exceptions @bot_exceptions ||= load_yaml("bot_exceptions.yml") end
def self.bots
def self.bots @bots ||= load_yaml("bots.yml") end
def self.default_matchers
def self.default_matchers [ EmptyUserAgentMatcher, KnownBotsMatcher, KeywordMatcher ] end
def self.load_yaml(path)
def self.load_yaml(path) YAML.load_file(Browser.root.join(path)) end
def self.matchers
def self.matchers @matchers ||= default_matchers end
def self.search_engines
def self.search_engines @search_engines ||= load_yaml("search_engines.yml") end
def self.why?(ua)
def self.why?(ua) ua = ua.downcase.strip browser = Browser.new(ua) matchers.find {|matcher| matcher.call(ua, browser) } end
def bot?
def bot? !bot_exception? && detect_bot? end
def bot_exception?
def bot_exception? lass.bot_exceptions.any? {|key| ua.include?(key) }
def detect_bot?
def detect_bot? lass.matchers.any? {|matcher| matcher.call(ua, browser) }
def initialize(ua)
def initialize(ua) @ua = ua.downcase.strip @browser = Browser.new(@ua) end
def name
def name return unless bot? self.class.bots.find {|key, _| ua.include?(key) }&.last || GENERIC_NAME end
def search_engine?
def search_engine? self.class.search_engines.any? {|key, _| ua.include?(key) } end
def why?
def why? self.class.matchers.find {|matcher| matcher.call(ua, self) } end