lib/mihari/emitters/webhook.rb in mihari-7.0.5 vs lib/mihari/emitters/webhook.rb in mihari-7.1.0
- old
+ new
@@ -1,67 +1,37 @@
# frozen_string_literal: true
-require "erb"
-
module Mihari
module Emitters
- class ERBTemplate < ERB
- class << self
- def template
- %{
- {
- "rule": {
- "id": "<%= @rule.id %>",
- "title": "<%= @rule.title %>",
- "description": "<%= @rule.description %>"
- },
- "artifacts": [
- <% @artifacts.each_with_index do |artifact, idx| %>
- "<%= artifact.data %>"
- <%= ',' if idx < (@artifacts.length - 1) %>
- <% end %>
- ],
- "tags": [
- <% @rule.tags.each_with_index do |tag, idx| %>
- "<%= tag.name %>"
- <%= ',' if idx < (@rule.tags.length - 1) %>
- <% end %>
- ]
- }
- }
- end
- end
-
- def initialize(artifacts:, rule:, options: {})
- @artifacts = artifacts
- @rule = rule
-
- @template = options.fetch(:template, self.class.template)
- super(@template)
- end
-
- def result
- super(binding)
- end
- end
-
class Webhook < Base
# @return [Addressable::URI, nil]
attr_reader :url
# @return [Hash]
attr_reader :headers
# @return [String]
attr_reader :method
- # @return [String, nil]
+ # @return [String]
attr_reader :template
# @return [Array<Mihari::Models::Artifact>]
attr_accessor :artifacts
+ DEFAULT_TEMPLATE = %{
+ json.rule do
+ json.id rule.id
+ json.title rule.title
+ json.description rule.description
+ end
+
+ json.artifacts artifacts.map(&:data)
+
+ json.tags rule.tags.map(&:name)
+ }
+
#
# @param [Mihari::Rule] rule
# @param [Hash, nil] options
# @param [Hash, nil] params
#
@@ -69,11 +39,11 @@
super(rule: rule, options: options)
@url = Addressable::URI.parse(params[:url])
@headers = params[:headers] || {}
@method = params[:method] || "POST"
- @template = params[:template]
+ @template = params[:template] || DEFAULT_TEMPLATE
@artifacts = []
end
#
@@ -112,18 +82,10 @@
# Render template
#
# @return [String]
#
def render
- options = {}
- options[:template] = File.read(template) unless template.nil?
-
- erb_template = ERBTemplate.new(
- artifacts: artifacts,
- rule: rule,
- options: options
- )
- erb_template.result
+ Services::JbuilderRenderer.call(template, { rule: rule, artifacts: artifacts })
end
#
# @return [Hash]
#