Class: SlackSender::Notifier

Inherits:
Object
  • Object
show all
Includes:
Axn
Defined in:
lib/slack_sender/notifier.rb,
lib/slack_sender/notifier/notification_dsl.rb,
lib/slack_sender/notifier/notification_definition.rb

Overview

Base class for Axns whose primary purpose is sending Slack notifications.

Usage:

class MyNotifier < SlackSender::Notifier
expects :user_id, type: Integer

notify do
  channel :notifications
  only_if { user.notifications_enabled? }
  text { "Hello #{user.name}!" }
end

private

def user = @user ||= User.find(user_id)
end

DSL inside notify do ... end:

channel :foo                    # single channel (symbol resolved as method or literal)
channels :foo, :bar             # multiple channels
text { "dynamic" }              # block evaluated in instance context
text :method_name               # symbol resolved as method call
text "static"                   # literal string
only_if { condition }           # conditional send (block)
only_if :method_name            # conditional send (symbol)

Defined Under Namespace

Classes: NotificationDSL, NotificationDefinition

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.notify { ... } ⇒ Object

Declare a notification to send.

Examples:

Simple notification

notify do
  channel :notifications
  text { "Hello!" }
end

With condition

notify do
  channels :ops, :alerts
  only_if { priority == :high }
  text { "Alert: #{message}" }
end

Yields:

  • Block evaluated in NotificationDSL context to configure the notification

Raises:

  • (ArgumentError)


58
59
60
61
62
63
64
65
66
# File 'lib/slack_sender/notifier.rb', line 58

def notify(&)
  raise ArgumentError, "notify requires a block" unless block_given?

  dsl = NotificationDSL.new
  dsl.instance_eval(&)
  definition = dsl.build

  self._notification_definitions = _notification_definitions + [definition]
end

Instance Method Details

#callObject



69
70
71
72
73
# File 'lib/slack_sender/notifier.rb', line 69

def call
  self.class._notification_definitions.each do |definition|
    definition.execute(self)
  end
end