BigW Consortium Gitlab

notifier.rb 1.07 KB
Newer Older
1 2 3 4
module MicrosoftTeams
  class Notifier
    def initialize(webhook)
      @webhook = webhook
Tiago Botelho committed
5
      @header = { 'Content-type' => 'application/json' }
6 7 8
    end

    def ping(options = {})
Tiago Botelho committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
      result = false

      begin
        response = HTTParty.post(
          @webhook.to_str,
          headers: @header,
          body: body(options)
        )

        result = true if response
      rescue HTTParty::Error, StandardError => error
        Rails.logger.info("#{self.class.name}: Error while connecting to #{@webhook}: #{error.message}")
      end

      result
24 25 26 27 28 29 30
    end

    private

    def body(options = {})
      result = { 'sections' => [] }

31 32
      result['title'] = options[:title]
      result['summary'] = options[:pretext]
Tiago Botelho committed
33
      result['sections'] << MicrosoftTeams::Activity.new(options[:activity]).prepare
34

Tiago Botelho committed
35 36 37 38 39 40 41
      attachments = options[:attachments]
      unless attachments.blank?
        result['sections'] << {
          'title' => 'Details',
          'facts' => [{ 'name' => 'Attachments', 'value' => attachments }]
        }
      end
42 43 44 45 46

      result.to_json
    end
  end
end