Class: Mihari::Emitters::Webhook

Inherits:
Base
  • Object
show all
Defined in:
lib/mihari/emitters/webhook.rb

Constant Summary

Constants included from Mixins::Retriable

Mixins::Retriable::DEFAULT_ON

Instance Attribute Summary collapse

Attributes inherited from Base

#artifacts, #rule

Instance Method Summary collapse

Methods inherited from Base

inherited, #run

Methods included from Mixins::Retriable

#retry_on_error

Methods included from Mixins::Configurable

#configuration_keys, #configuration_keys?, #configuration_values, #configured?

Constructor Details

#initialize(artifacts:, rule:, **options) ⇒ Webhook

Returns a new instance of Webhook.

Parameters:



63
64
65
66
67
68
69
70
# File 'lib/mihari/emitters/webhook.rb', line 63

def initialize(artifacts:, rule:, **options)
  super(artifacts: artifacts, rule: rule, **options)

  @url = Addressable::URI.parse(options[:url])
  @headers = options[:headers] || {}
  @method = options[:method] || "POST"
  @template = options[:template]
end

Instance Attribute Details

#headersHash (readonly)

Returns:

  • (Hash)


50
51
52
# File 'lib/mihari/emitters/webhook.rb', line 50

def headers
  @headers
end

#methodString (readonly)

Returns:

  • (String)


53
54
55
# File 'lib/mihari/emitters/webhook.rb', line 53

def method
  @method
end

#templateString? (readonly)

Returns:

  • (String, nil)


56
57
58
# File 'lib/mihari/emitters/webhook.rb', line 56

def template
  @template
end

#urlAddressable::URI? (readonly)

Returns:

  • (Addressable::URI, nil)


47
48
49
# File 'lib/mihari/emitters/webhook.rb', line 47

def url
  @url
end

Instance Method Details

#emitObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mihari/emitters/webhook.rb', line 72

def emit
  return if artifacts.empty?

  client = Mihari::HTTP.new(url, headers: headers)

  res = nil
  case method
  when "GET"
    res = client.get
  when "POST"
    res = client.post(json: payload)
  end

  res
end

#valid?Boolean

Returns:

  • (Boolean)


88
89
90
91
92
# File 'lib/mihari/emitters/webhook.rb', line 88

def valid?
  return false if url.nil?

  %w[http https].include? url.scheme.downcase
end