Sha256: c83697cce88a218e8c923667dcef74c7be040e2cba54ae908832f0d2ce7184ed

Contents?: true

Size: 1.61 KB

Versions: 10

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module Mihari
  module Services
    #
    # Alert proxy
    #
    class AlertProxy
      # @return [Hash]
      attr_reader :data

      # @return [Array, nil]
      attr_reader :errors

      #
      # Initialize
      #
      # @param [Hash] data
      #
      def initialize(**data)
        super()

        @data = data.deep_symbolize_keys
        @errors = nil

        validate!
      end

      #
      # @return [Boolean]
      #
      def errors?
        return false if errors.nil?

        !errors.empty?
      end

      def validate!
        contract = Schemas::AlertContract.new
        result = contract.call(data)

        @data = result.to_h
        @errors = result.errors

        raise ValidationError.new("Validation failed", errors) if errors?
      end

      def [](key)
        data key.to_sym
      end

      #
      # @return [String]
      #
      def rule_id
        @rule_id ||= data[:rule_id]
      end

      #
      # @return [Array<Mihari::Models::Artifact>]
      #
      def artifacts
        @artifacts ||= data[:artifacts].map do |data|
          artifact = Models::Artifact.new(data: data)
          artifact.rule_id = rule_id
          artifact.source = source
          artifact
        end.uniq(&:data).select(&:valid?)
      end

      #
      # @return [String, nil]
      #
      def source
        @source ||= data[:source]
      end

      #
      # @return [Mihari::Rule]
      #
      def rule
        @rule ||= [].tap do |out|
          data = Mihari::Models::Rule.find(rule_id).data
          out << Rule.new(**data)
        end.first
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
mihari-7.1.3 lib/mihari/services/proxies.rb
mihari-7.1.2 lib/mihari/services/proxies.rb
mihari-7.1.1 lib/mihari/services/proxies.rb
mihari-7.1.0 lib/mihari/services/proxies.rb
mihari-7.0.5 lib/mihari/services/proxies.rb
mihari-7.0.4 lib/mihari/services/proxies.rb
mihari-7.0.3 lib/mihari/services/proxies.rb
mihari-7.0.2 lib/mihari/services/proxies.rb
mihari-7.0.1 lib/mihari/services/proxies.rb
mihari-7.0.0 lib/mihari/services/proxies.rb