Sha256: 239ff238aa25abc31d28e41a71906996751a63af973ac20eb57456dea579d9ef

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module Browser
  class Bot
    def self.detect_empty_ua!
      @detect_empty_ua = true
    end

    def self.detect_empty_ua?
      @detect_empty_ua
    end

    def self.bots
      @bots ||= YAML.load_file(Browser.root.join("bots.yml"))
    end

    def self.bot_exceptions
      @bot_exceptions ||= YAML
                          .load_file(Browser.root.join("bot_exceptions.yml"))
    end

    def self.search_engines
      @search_engines ||= YAML
                          .load_file(Browser.root.join("search_engines.yml"))
    end

    def self.why?(ua)
      downcased_ua = ua.downcase
      bots.find {|key, _| downcased_ua.include?(key) }
    end

    attr_reader :ua

    def initialize(ua)
      @ua = ua
    end

    def bot?
      bot_with_empty_ua? || (!bot_exception? && detect_bot?)
    end

    def search_engine?
      self.class.search_engines.any? {|key, _| downcased_ua.include?(key) }
    end

    def name
      return unless bot?
      return "Generic Bot" if bot_with_empty_ua?

      self.class.bots.find {|key, _| downcased_ua.include?(key) }.last
    end

    private def bot_with_empty_ua?
      self.class.detect_empty_ua? && ua.strip == ""
    end

    private def bot_exception?
      self.class.bot_exceptions.any? {|key| downcased_ua.include?(key) }
    end

    private def detect_bot?
      self.class.bots.any? {|key, _| downcased_ua.include?(key) }
    end

    private def downcased_ua
      @downcased_ua ||= ua.downcase
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
browser-2.7.1 lib/browser/bot.rb