Module: RocketChatNotifier

Defined in:
lib/rocketchat/notifier.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.verbose_modeObject

Returns the value of attribute verbose_mode.



6
7
8
# File 'lib/rocketchat/notifier.rb', line 6

def verbose_mode
  @verbose_mode
end

.webhook_urlObject

Returns the value of attribute webhook_url.



6
7
8
# File 'lib/rocketchat/notifier.rb', line 6

def webhook_url
  @webhook_url
end

Class Method Details

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



8
9
10
11
12
# File 'lib/rocketchat/notifier.rb', line 8

def configure
  yield self

  raise "\nrocket chat notifier - configuration error: missing `webhook_url` in initializer" unless webhook_url
end

.notify(message, event: '', emoji: '', attachment: [{}]) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rocketchat/notifier.rb', line 14

def notify(message, event: '', emoji: '', attachment: [{}])
  Rails.logger.debug("\nintitializing rocket chat notifier ") if verbose_mode == true
  raise "\nrocket chat notifier - error: empty rocket chat message, message text is mandatory" unless message

  Rails.logger.debug("\nparsing options: \n  message: `#{message}`\n  event: `#{event}`\n  emoji: `#{emoji}`\n  attachment: `#{attachment}`") if verbose_mode == true

  request_body = {
    message: message
  }

  request_body[:event] = event if event.present?
  request_body[:emoji] = emoji if emoji.present?
  request_body[:attachment] = attachment if attachment.present?

  begin
    Rails.logger.debug("\nsending rocket chat notification request to webhook url:\n#{webhook_url}") if verbose_mode == true

    response = JSON.parse HTTParty.post(webhook_url, body: request_body.to_json, headers: { 'Content-Type' => 'application/json' }).body

    Rails.logger.debug("\nrocket chat response:\n#{response.inspect}\n") if verbose_mode == true
    warn("\nrocket chat notifier - warning: rocket chat could not be notified. rocket chat response: `#{response['error']}`") unless response['success'] || response['success'] == true
  rescue
    warn("\nrocket chat notifier - warning: could not connect to rocket chat webhook url or parse JSON result, check your webhook_url")
  end

  response
end