Class: SlackNotification

Inherits:
Notification show all
Defined in:
lib/app/models/slack_notification.rb

Overview

SlackNotification is a concrete class of Notification, sending notifications to the slack system. Initially this will be only used internally, however we could configure this to work with an account’s slack configuration.

This object allows you to create an EmailNotification object, set it’s property and tell it send_notification. It’ll handle everything from there including

  1. Running in the background thread

  2. Error recovery

  3. Retry on failure

Usage:

SlackNotification.say ‘Error getting data from resource’

or To change the channel it’s posted to…

SlackNotification.say ‘Error getting data from resource’, to: ‘another_channel’

or To who the message is from, the default is the environment name

SlackNotification.say ‘Error getting data from resource’, to: ‘another_channel’, from: ‘someone else’

You are done! Go about your business of creating awesome software!

Constant Summary

Constants inherited from Notification

Notification::ALL_STATES, Notification::DELIVERY_EMAIL, Notification::DELIVERY_SLACK, Notification::DELIVERY_SMS, Notification::STATE_INVALID, Notification::STATE_NEW, Notification::STATE_PROCESSED, Notification::STATE_PROCESSING, Notification::STATE_RESUBMITTED, Notification::STATE_RETRYING, Notification::STATE_SUBMITTED, Notification::STATE_VIEWED

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Notification

#account_message_template, #default_message_template, #deletable?, #deliver_message!, #finish_processing, #from_template, #message_from_haml_file, #message_from_haml_text, #message_from_liquid_text, #message_from_template, #name, #render_liquid_text, #retry_delivery, #search_fields, #send_notification, #sendable?, #start_processing, #stringify_all, #successful?, #viewed

Methods included from SearchAble

#after_search_text, #before_search_text, included, #method_missing, #respond_to?, #respond_to_missing?, #search_fields, #sort_fields, #update_search_and_sort_text, #update_text

Methods included from StandardModel

#audit_action, #auto_strip_attributes, #capture_user_info, #clear_cache, #created_by_display_name, #delete_and_log, #destroy_and_log, included, #last_modified_by_display_name, #log_change, #log_deletion, #remove_blank_secure_fields, #save_and_log, #save_and_log!, #secure_fields, #update, #update!, #update_and_log, #update_and_log!

Methods included from App47Logger

clean_params, #clean_params, delete_parameter_keys, #log_controller_error, log_debug, #log_debug, log_error, #log_error, log_exception, #log_message, log_message, #log_warn, log_warn, mask_parameter_keys, #update_flash_messages

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class SearchAble

Class Method Details

.say(message, to: nil, from: nil, template: nil) ⇒ Object

Convenience method to say something when we see something



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/app/models/slack_notification.rb', line 62

def self.say(message, to: nil, from: nil, template: nil)
  notification = SlackNotification.new(to: to, from: from)
  notification.message = if template.present?
                           notification.message_from_template template, message
                         elsif message.is_a?(Array)
                           message.join("\n")
                         else
                           message
                         end
  notification.send_notification
end

Instance Method Details

#deliver_messageObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/app/models/slack_notification.rb', line 34

def deliver_message
  if SystemConfiguration.slack_configured?
    start_processing
    payload = { text: message }
    payload[:channel] = to.presence || SystemConfiguration.slack_support_channel
    # Use the environment as the default, otherwise set it as the from
    payload[:username] = from.presence || Rails.env
    # Setup the delivery method for this message only.
    RestClient.post(SystemConfiguration.slack_api_url, payload.to_json)
    finish_processing
  else
    finish_processing 'Slack is not configured'
  end
rescue StandardError => error
  log_warn '!!! Error sending SLACK notification !!!', error
  finish_processing error.message
end

#delivery_channelObject

Default delivery channel is email, override for sms, SMTP or other channels



55
56
57
# File 'lib/app/models/slack_notification.rb', line 55

def delivery_channel
  DELIVERY_SLACK
end