Class: Perchfall::Notify::Channels::Slack

Inherits:
Perchfall::Notify::Channel show all
Defined in:
lib/perchfall/notify/channels/slack.rb

Overview

Delivers Perchfall::Report notifications to a Slack incoming webhook.

Requires a webhook URL. Accepts an optional formatter to control message shape, and an optional http_client for testability.

Example:

slack = Perchfall::Notify::Channels::Slack.new(
webhook_url: "https://hooks.slack.com/services/..."
)
slack.deliver(report)

Instance Method Summary collapse

Constructor Details

#initialize(webhook_url:, formatter: SlackFormatter.new, http_client: NetHttpClient.new) ⇒ Slack

Returns a new instance of Slack.

Parameters:

  • webhook_url (String) —

    Slack incoming webhook URL

  • formatter (#format) (defaults to: SlackFormatter.new) —

    converts a Report to a Slack payload Hash

  • http_client (#post) (defaults to: NetHttpClient.new) —

    sends the payload; defaults to NetHttpClient

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/perchfall/notify/channels/slack.rb', line 22

def initialize(webhook_url:, formatter: SlackFormatter.new, http_client: NetHttpClient.new)
  super()
  raise ConfigurationError, "webhook_url is required" if webhook_url.nil? || webhook_url.strip.empty?

  uri = begin
    URI.parse(webhook_url)
  rescue URI::InvalidURIError
    raise ConfigurationError, "webhook_url is not a valid URL"
  end

  raise ConfigurationError, "webhook_url must use https" unless uri.is_a?(URI::HTTPS)

  @webhook_url = webhook_url
  @formatter = formatter
  @http_client = http_client
end

Instance Method Details

#deliver(report) ⇒ Object

Parameters:

  • report (Perchfall::Report)

Raises:

  • (DeliveryError) —

    if the HTTP request fails or Slack rejects the payload



41
42
43
44
45
46
47
48
# File 'lib/perchfall/notify/channels/slack.rb', line 41

def deliver(report)
  payload = @formatter.format(report)
  @http_client.post(@webhook_url, payload)
rescue DeliveryError
  raise
rescue StandardError => e
  raise DeliveryError, "Slack delivery failed: #{e.message}"
end