Class: ActivityNotification::OptionalTarget::Slack

Inherits:
Base
  • Object
show all
Defined in:
lib/activity_notification/optional_targets/slack.rb

Overview

Optional target implementation for Slack.

Instance Method Summary collapse

Methods inherited from Base

#initialize, #to_optional_target_name

Constructor Details

This class inherits a constructor from ActivityNotification::OptionalTarget::Base

Instance Method Details

#initialize_target(options = {}) ⇒ Object

Initialize method to prepare Slack::Notifier

Parameters:

  • options (Hash) (defaults to: {})

    Options for initializing

Options Hash (options):

  • :target_username (String, Proc, Symbol) — default: nil

    Target user name of Slack, it resolved by target instance like email_allowed?

  • :webhook_url (required, String) — default: nil

    Webhook URL of Slack Incoming WebHooks integration

  • others (Hash)

    Other options to be set Slack::Notifier.new, like :channel, :username, :icon_emoji etc



12
13
14
15
# File 'lib/activity_notification/optional_targets/slack.rb', line 12

def initialize_target(options = {})
  @target_username = options.delete(:target_username)
  @notifier = ::Slack::Notifier.new(options.delete(:webhook_url), options)
end

#notify(notification, options = {}) ⇒ Object

Publishes notification message to Slack

Parameters:

  • notification (Notification)

    Notification instance

  • options (Hash) (defaults to: {})

    Options for publishing

Options Hash (options):

  • :target_username (String, Proc, Symbol) — default: nil

    Target user name of Slack, it resolved by target instance like email_allowed?

  • :partial_root (String) — default: "activity_notification/optional_targets/#{target}/#{optional_target_name}", "activity_notification/optional_targets/#{target}/base", "activity_notification/optional_targets/default/#{optional_target_name}", "activity_notification/optional_targets/default/base"

    Partial template name

  • :partial (String) — default: self.key.tr('.', '/')

    Root path of partial template

  • :layout (String) — default: nil

    Layout template name

  • :layout_root (String) — default: 'layouts'

    Root path of layout template

  • :fallback (String, Symbol) — default: :default

    Fallback template to use when MissingTemplate is raised. Set :text to use i18n text as fallback.

  • others (Hash)

    Parameters to be set as locals



27
28
29
30
# File 'lib/activity_notification/optional_targets/slack.rb', line 27

def notify(notification, options = {})
  target_username = notification.target.resolve_value(options.delete(:target_username) || @target_username)
  @notifier.ping(render_notification_message(notification, options.merge(assignment: { target_username: target_username })))
end