Sha256: 06fb94bc97e3c40368a8d39bb8f84cbdc11baf04638ad623f9b76f3ce03df775

Contents?: true

Size: 940 Bytes

Versions: 1

Compression:

Stored size: 940 Bytes

Contents

# frozen_string_literal: true

require "date"
require "erb"
require "pathname"
require "yaml"

module Mihari
  module Services
    class RuleBuilder
      include Dry::Monads[:result, :try]

      # @return [String]
      attr_reader :path_or_id

      #
      # Initialize
      #
      # @param [String] path_or_id
      #
      def initialize(path_or_id)
        @path_or_id = path_or_id
      end

      #
      # @return [Hash]
      #
      def data
        if Mihari::Models::Rule.exists?(path_or_id)
          rule = Mihari::Models::Rule.find(path_or_id)
          return rule.data
        end

        raise ArgumentError, "#{path_or_id} does not exist" unless Pathname(path_or_id).exist?

        YAML.safe_load(
          ERB.new(File.read(path_or_id)).result,
          permitted_classes: [Date, Symbol]
        )
      end

      def result
        Try[StandardError] { Rule.new(**data) }.to_result
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mihari-5.6.2 lib/mihari/services/rule_builder.rb