Sha256: a5fc8a5a8d8a3872ba5b64b80a44c2ee6c323d17198f78b2f5c844d0e6793ab6

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

module CMSScanner
  module Finders
    # Unique Finders container
    #
    # This class is designed to return a unique result such as a version
    # Note: Finders contained can return multiple results but the #run will only
    # returned the best finding
    class UniqueFinders < IndependentFinders
      # @param [ Hash ] opts
      # @option opts [ Symbol ] :mode :mixed, :passive or :aggressive
      # @option opts [ Int ] :confidence_threshold  If a finding's confidence reaches this value,
      #                                             it will be returned as the best finding.
      #                                             Default is 100.
      #                                             If <= 0, all finders will be ran.
      #
      # @return [ Object ] The best finding
      def run(opts = {})
        opts[:confidence_threshold] ||= 100

        symbols_from_mode(opts[:mode]).each do |symbol|
          each do |finder|
            [*finder.send(symbol, opts.merge(found: findings))].compact.each do |found|
              findings << found
            end

            next if opts[:confidence_threshold] <= 0

            findings.each { |f| return f if f.confidence >= opts[:confidence_threshold] }
          end
        end

        best_finding(findings)
      end

      # @param [ Array<Object> ] findings
      #
      # @return [ Object ] The best finding
      def best_finding(findings)
        # results are sorted by confidence ASC
        findings.sort_by!(&:confidence)

        # If all findings have the same confidence, nil is returned
        return if findings.size > 1 && findings.first.confidence == findings.last.confidence

        findings.last
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cms_scanner-0.0.16 lib/cms_scanner/finders/unique_finders.rb