class DeviseSlackNotifiable::Notifier

Module <-> Slack communication layer and payload builder

Attributes

client[R]

Public Class Methods

new() click to toggle source

DeviseSlackNotifiable::Notifier constructor.

Creates Slack::Notifier instance

# File lib/devise_slack_notifiable/notifier.rb, line 13
def initialize
  return unless enabled?

  raise DeviseSlackNotifiable::Errors::Configuration, 'Missing Slack Webhook URL' unless webhook

  @client = Slack::Notifier.new(webhook)
end

Public Instance Methods

send_message(entity, message_formatter) click to toggle source

Sends slack message

@param [Model] entity @param [Proc] message_formatter

# File lib/devise_slack_notifiable/notifier.rb, line 25
def send_message(entity, message_formatter)
  return true unless enabled?

  @client.ping(
    formatter(
      message_formatter.call(entity),
      entity
    )
  )
end

Protected Instance Methods

context_fields() click to toggle source

Context fields

@return [Array]

# File lib/devise_slack_notifiable/notifier.rb, line 57
def context_fields
  DeviseSlackNotifiable.configuration.context_fields
end
enabled?() click to toggle source

Integration enabled?

@return [Boolean]

# File lib/devise_slack_notifiable/notifier.rb, line 43
def enabled?
  DeviseSlackNotifiable.configuration.enabled
end
formatter(message, entity) click to toggle source

Message builder

@param [String] message @param [ActiveRecord::Base] entity

@return [Hash]

# File lib/devise_slack_notifiable/notifier.rb, line 67
def formatter(message, entity)
  {
    blocks: [
      {
        type: 'section',
        text: {
          type: 'plain_text',
          text: message,
          emoji: true
        }
      },
      {
        type: 'section',
        text: {
          type: 'plain_text',
          text: 'Context:',
          emoji: true
        }
      },
      {
        type: 'section',
        fields: context_fields.map do |field|
          {
            type: 'mrkdwn',
            text: "*#{field.to_s.humanize}:* #{entity.send(field)}"
          }
        end
      }
    ]
  }
end
webhook() click to toggle source

Webhook URL

@return [String, NilClass]

# File lib/devise_slack_notifiable/notifier.rb, line 50
def webhook
  DeviseSlackNotifiable.configuration.slack_webhook
end