Class: VolumeSweeper::Utils::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/volume_sweeper/utils/notification.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ Notification

Returns a new instance of Notification.



11
12
13
14
15
16
17
# File 'lib/volume_sweeper/utils/notification.rb', line 11

def initialize **kwargs
  @log = Utils::Log.instance

  setup_configuration **kwargs
  configure_mailer
  configure_ms_teams
end

Instance Attribute Details

#default_subjectObject (readonly)

Returns the value of attribute default_subject.



9
10
11
# File 'lib/volume_sweeper/utils/notification.rb', line 9

def default_subject
  @default_subject
end

Instance Method Details

#send_mail(text) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/volume_sweeper/utils/notification.rb', line 19

def send_mail text
  return unless smtp_configured?
  @log.msg "#{self_name}: sending mail notification."

  sender, receiver = @smtp_sender, @smtp_receiver
  subject = message_subject
  content = build_message_content text
  Mail.deliver do
    from     sender
    to       receiver
    subject  subject
    content_type 'text/html; charset=UTF-8'
    body     content
  end

  @log.msg "#{self_name}: email is sent successfully.", level: :info

rescue Exception => e
  @log.msg "#{self_name}: mail notification failed.", level: :error
  @log.msg "#{self_name}: #{e.message}.", level: :error
end

#send_ms_teams_notice(text) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/volume_sweeper/utils/notification.rb', line 41

def send_ms_teams_notice text
  return unless @webhook_url.present?

  @log.msg "#{self_name}: sending ms teams notification."

  request = Net::HTTP::Post.new @webhook_url.request_uri
  request['Content-Type'] = 'application/json'
  request.body = { title: message_subject, text: text }.to_json

  http = Net::HTTP.new @webhook_url.host, @webhook_url.port
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  body = http.request(request)&.body
  @log.msg "#{self_name}: ms teams notification is sent."
  body
rescue StandardError => e
  @log.msg "#{self_name}: ms teams notification failed.", level: :error
  @log.msg "#{self_name}: #{e.message}.", level: :error
end