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

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_values, #configured?

Constructor Details

#initialize(*args, **kwargs) ⇒ Webhook

Returns a new instance of Webhook.



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

def initialize(*args, **kwargs)
  super(*args, **kwargs)

  url = kwargs[:url]
  headers = kwargs[:headers] || {}
  method = kwargs[:method] || "POST"
  template = kwargs[:template]

  @url = Addressable::URI.parse(url)
  @headers = headers
  @method = method
  @template = 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

#emit(artifacts:, rule:) ⇒ Object



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

def emit(artifacts:, rule:)
  return if artifacts.empty?

  res = nil

  payload_ = payload_as_string(artifacts: artifacts, rule: rule)
  payload = JSON.parse(payload_)

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

  case method
  when "GET"
    res = client.get
  when "POST"
    res = client.post
  end

  res
end

#valid?Boolean

Returns:

  • (Boolean)


92
93
94
95
96
# File 'lib/mihari/emitters/webhook.rb', line 92

def valid?
  return false if url.nil?

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