File: slack.rb

package info (click to toggle)
ruby-noticed 3.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 324 kB
  • sloc: ruby: 1,186; makefile: 4
file content (41 lines) | stat: -rw-r--r-- 1,131 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module Noticed
  module BulkDeliveryMethods
    class Slack < BulkDeliveryMethod
      DEFAULT_URL = "https://slack.com/api/chat.postMessage"

      required_options :json

      def deliver
        headers = evaluate_option(:headers)
        json = evaluate_option(:json)
        response = post_request url, headers: headers, json: json

        if raise_if_not_ok? && !success?(response)
          raise ResponseUnsuccessful.new(response, url, {headers: headers, json: json})
        end

        response
      end

      def url
        evaluate_option(:url) || DEFAULT_URL
      end

      def raise_if_not_ok?
        value = evaluate_option(:raise_if_not_ok)
        value.nil? || value
      end

      def success?(response)
        if response.content_type == "application/json"
          JSON.parse(response.body).dig("ok")
        else
          # https://api.slack.com/changelog/2016-05-17-changes-to-errors-for-incoming-webhooks
          response.is_a?(Net::HTTPSuccess)
        end
      end
    end
  end
end

ActiveSupport.run_load_hooks :noticed_bulk_delivery_methods_slack, Noticed::BulkDeliveryMethods::Slack