Sha256: ef4f41d0ef34ffcc50ddf6e3dc6ba738e4f2389865baa46631b8a33f0d010dd9

Contents?: true

Size: 1.31 KB

Versions: 10

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module CrawlerDetect
  # since 0.1.0
  class Detector
    # @param user_agent [String] User-agent string to detect
    # @return [CrawlerDetect::Detector] instance of detector class
    def initialize(user_agent)
      @user_agent = user_agent.to_s.dup
    end

    # @return [true, false] Is User-agent a crawler?
    def is_crawler?
      @is_crawler ||= begin
        !completely_exclusion? && matches_crawler_list?
      end
    end

    # @return [String] The detected crawler name from RAW data
    def crawler_name
      return unless is_crawler?
      @crawler_name
    end

    private

    # @private
    # @return [true, false] Is User-agent in white-list?
    def completely_exclusion?
      @user_agent.gsub!(exclusions_matcher, "")
      @user_agent.strip.length.zero?
    end

    # @private
    # @return [true, false] Is User-agent in black-list?
    def matches_crawler_list?
      @crawler_name = crawlers_matcher.match(@user_agent).to_s.strip
      !@crawler_name.empty?
    end

    # @private
    # @return [Regexp] White-list of User-agents
    def exclusions_matcher
      CrawlerDetect::Library.get_regexp("exclusions")
    end

    # @private
    # @return [Regexp] Black-list of User-agents
    def crawlers_matcher
      CrawlerDetect::Library.get_regexp("crawlers")
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
crawler_detect-1.2.3 lib/crawler_detect/detector.rb
crawler_detect-1.2.2 lib/crawler_detect/detector.rb
crawler_detect-1.2.1 lib/crawler_detect/detector.rb
crawler_detect-1.2.0 lib/crawler_detect/detector.rb
crawler_detect-1.1.2 lib/crawler_detect/detector.rb
crawler_detect-1.1.1 lib/crawler_detect/detector.rb
crawler_detect-1.1.0 lib/crawler_detect/detector.rb
crawler_detect-1.0.2 lib/crawler_detect/detector.rb
crawler_detect-1.0.1 lib/crawler_detect/detector.rb
crawler_detect-1.0.0 lib/crawler_detect/detector.rb