Module: RocketChatNotifier
- Defined in:
- lib/rocketchat/notifier.rb
Class Attribute Summary collapse
-
.verbose_mode ⇒ Object
Returns the value of attribute verbose_mode.
-
.webhook_url ⇒ Object
Returns the value of attribute webhook_url.
Class Method Summary collapse
- .configure {|_self| ... } ⇒ Object
- .notify(message, event: '', emoji: '', attachment: [{}]) ⇒ Object
Class Attribute Details
.verbose_mode ⇒ Object
Returns the value of attribute verbose_mode.
6 7 8 |
# File 'lib/rocketchat/notifier.rb', line 6 def verbose_mode @verbose_mode end |
.webhook_url ⇒ Object
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
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(, 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 Rails.logger.debug("\nparsing options: \n message: `#{message}`\n event: `#{event}`\n emoji: `#{emoji}`\n attachment: `#{attachment}`") if verbose_mode == true request_body = { message: } request_body[:event] = event if event.present? request_body[:emoji] = emoji if emoji.present? request_body[:attachment] = if .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 |