lib/mihari/analyzers/base.rb in mihari-5.2.4 vs lib/mihari/analyzers/base.rb in mihari-5.3.0
- old
+ new
@@ -1,15 +1,49 @@
# frozen_string_literal: true
module Mihari
module Analyzers
class Base
- extend Dry::Initializer
-
include Mixins::Configurable
include Mixins::Retriable
+ # @return [String]
+ attr_reader :query
+
+ # @return [Hash]
+ attr_reader :options
+
+ #
+ # @param [String] query
+ # @param [Hash, nil] options
+ #
+ def initialize(query, options: nil)
+ @query = query
+ @options = options || {}
+ end
+
+ #
+ # @return [Integer, nil]
+ #
+ def interval
+ @interval = options[:interval]
+ end
+
+ #
+ # @return [Integer]
+ #
+ def retry_interval
+ @retry_interval ||= options[:retry_interval] || DEFAULT_RETRY_INTERVAL
+ end
+
+ #
+ # @return [Integer]
+ #
+ def retry_times
+ @retry_times ||= options[:retry_times] || DEFAULT_RETRY_TIMES
+ end
+
# @return [Array<String>, Array<Mihari::Artifact>]
def artifacts
raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end
@@ -19,21 +53,18 @@
# - Reject an invalid artifact
#
# @return [Array<Mihari::Artifact>]
#
def normalized_artifacts
- retry_on_error do
+ retry_on_error(times: retry_times, interval: retry_interval) do
@normalized_artifacts ||= artifacts.compact.sort.map do |artifact|
# No need to set data_type manually
# It is set automatically in #initialize
artifact = artifact.is_a?(Artifact) ? artifact : Artifact.new(data: artifact)
- artifact
- end.select(&:valid?).uniq(&:data).map do |artifact|
- # set source
artifact.source = source
artifact
- end
+ end.select(&:valid?).uniq(&:data)
end
end
# @return [String]
def class_name
@@ -49,19 +80,22 @@
# @param [Hash] params
#
# @return [Mihari::Analyzers::Base]
#
def from_query(params)
- # get options and set default value as an empty hash
- options = params[:options] || {}
+ copied = params.deep_dup
- # set interval in the top level
- interval = options[:interval]
- params[:interval] = interval if interval
+ # convert params into arguments for initialization
+ query = copied[:query]
- query = params[:query]
+ # delete analyzer and query
+ %i[analyzer query].each do |key|
+ copied.delete key
+ end
- new(query, **params)
+ copied[:options] = copied[:options] || nil
+
+ new(query, **copied)
end
def inherited(child)
super
Mihari.analyzers << child