Sha256: a01d0c8c115f620e96e02bbfbc90aba2583946e8d67fa79abda6ced69cf3927b

Contents?: true

Size: 1.66 KB

Versions: 9

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

require "json"

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

      # @return [Array, nil]
      attr_reader :errors

      #
      # Initialize
      #
      # @param [Hash] data
      #
      def initialize(data)
        @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::Artifact>]
      #
      def artifacts
        @artifacts ||= data[:artifacts].map do |data|
          artifact = Artifact.new(data: data)
          artifact.rule_id = rule_id
          artifact
        end.uniq(&:data).select(&:valid?)
      end

      #
      # @return [Mihari::Services::RuleProxy]
      #
      def rule
        @rule ||= Services::RuleProxy.new(Mihari::Rule.find(rule_id).data)
      end

      class << self
        #
        # Load rule from YAML string
        #
        # @param [String] yaml
        #
        # @return [Mihari::Services::Alert]
        #
        def from_yaml(yaml)
          new YAML.safe_load(yaml, permitted_classes: [Date, Symbol])
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mihari-5.6.1 lib/mihari/services/alert_proxy.rb
mihari-5.6.0 lib/mihari/services/alert_proxy.rb
mihari-5.5.0 lib/mihari/services/alert_proxy.rb
mihari-5.4.9 lib/mihari/services/alert_proxy.rb
mihari-5.4.8 lib/mihari/services/alert_proxy.rb
mihari-5.4.7 lib/mihari/services/alert_proxy.rb
mihari-5.4.6 lib/mihari/services/alert_proxy.rb
mihari-5.4.5 lib/mihari/services/alert_proxy.rb
mihari-5.4.4 lib/mihari/services/alert_proxy.rb