Class: Patches::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/patches/notifier.rb

Class Method Summary collapse

Class Method Details

.environment_prefixObject



30
31
32
# File 'lib/patches/notifier.rb', line 30

def environment_prefix
  Rails.env.upcase if defined?(Rails)
end

.failure_message(patch_path, error) ⇒ Object



17
18
19
# File 'lib/patches/notifier.rb', line 17

def failure_message(patch_path, error)
  message("Error applying patch:", Pathname.new(patch_path).basename, "failed with error:", error)
end

.message(*args) ⇒ Object



21
22
23
# File 'lib/patches/notifier.rb', line 21

def message(*args)
  [notification_prefix, *args, notification_suffix].compact.join(" ")
end

.notification_prefixObject



25
26
27
28
# File 'lib/patches/notifier.rb', line 25

def notification_prefix
  prefix = config.notification_prefix.presence || environment_prefix
  "[#{prefix}]" if prefix.present?
end

.notification_suffixObject



34
35
36
# File 'lib/patches/notifier.rb', line 34

def notification_suffix
  config.notification_suffix.presence || tenant_suffix
end

.notify_failure(patch_path, error) ⇒ Object



9
10
11
# File 'lib/patches/notifier.rb', line 9

def notify_failure(patch_path, error)
  send_slack_message(failure_message(patch_path, error), 'danger')
end

.notify_success(patches) ⇒ Object



5
6
7
# File 'lib/patches/notifier.rb', line 5

def notify_success(patches)
  send_slack_message(success_message(patches), 'good')
end

.send_slack_message(message, color) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/patches/notifier.rb', line 42

def send_slack_message(message, color)
  return unless defined?(Slack) && config.use_slack

  notifier = Slack::Notifier.new(
    config.slack_webhook_url,
    channel: config.slack_channel,
    username: config.slack_username)

  payload = { icon_emoji: ":dog:", attachments: [{ color: color, text: message }] }

  notifier.post payload
end

.success_message(patches) ⇒ Object



13
14
15
# File 'lib/patches/notifier.rb', line 13

def success_message(patches)
  message(patches.count, "patches succeeded")
end

.tenant_suffixObject



38
39
40
# File 'lib/patches/notifier.rb', line 38

def tenant_suffix
  "for tenant: #{Apartment::Tenant.current}" if defined?(Apartment)
end