Class: Perchfall::Notify::Channels::Slack
- Inherits:
-
Perchfall::Notify::Channel
- Object
- Perchfall::Notify::Channel
- Perchfall::Notify::Channels::Slack
- 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
- #deliver(report) ⇒ Object
-
#initialize(webhook_url:, formatter: SlackFormatter.new, http_client: NetHttpClient.new) ⇒ Slack
constructor
A new instance of Slack.
Constructor Details
#initialize(webhook_url:, formatter: SlackFormatter.new, http_client: NetHttpClient.new) ⇒ Slack
Returns a new instance of Slack.
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
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.}" end |