diff options
author | Tiago Botelho <tiagonbotelho@hotmail.com> | 2017-04-04 16:10:21 +0100 |
---|---|---|
committer | Tiago Botelho <tiagonbotelho@hotmail.com> | 2017-04-06 19:47:07 +0100 |
commit | 1f404065ca602593dc18aa051515fd88132ddfdf (patch) | |
tree | e1fb01a2584c2137ad603500fc22652ab099bb46 /lib/microsoft_teams | |
parent | 05f69ef21246f1a2c0b83540d3e205c9e4d77819 (diff) | |
download | gitlab-ce-1f404065ca602593dc18aa051515fd88132ddfdf.tar.gz |
adds relevant tests
Diffstat (limited to 'lib/microsoft_teams')
-rw-r--r-- | lib/microsoft_teams/activity.rb | 6 | ||||
-rw-r--r-- | lib/microsoft_teams/notifier.rb | 35 |
2 files changed, 27 insertions, 14 deletions
diff --git a/lib/microsoft_teams/activity.rb b/lib/microsoft_teams/activity.rb index e25ce262cbf..d2c420efdaf 100644 --- a/lib/microsoft_teams/activity.rb +++ b/lib/microsoft_teams/activity.rb @@ -1,19 +1,19 @@ module MicrosoftTeams class Activity - def initialize(title, subtitle, text, image) + def initialize(title:, subtitle:, text:, image:) @title = title @subtitle = subtitle @text = text @image = image end - def to_json + def prepare { 'activityTitle' => @title, 'activitySubtitle' => @subtitle, 'activityText' => @text, 'activityImage' => @image - }.to_json + } end end end diff --git a/lib/microsoft_teams/notifier.rb b/lib/microsoft_teams/notifier.rb index a5d2fe15ab6..3bef68a1bcb 100644 --- a/lib/microsoft_teams/notifier.rb +++ b/lib/microsoft_teams/notifier.rb @@ -2,30 +2,43 @@ module MicrosoftTeams class Notifier def initialize(webhook) @webhook = webhook + @header = { 'Content-type' => 'application/json' } end def ping(options = {}) - HTTParty.post( - @webhook.to_str, - headers: { 'Content-type' => 'application/json' }, - body: body(options) - ) + 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 end private def body(options = {}) - attachments = options[:attachments] result = { 'sections' => [] } result['title'] = options[:title] result['summary'] = options[:pretext] - result['sections'] << options[:activity] + result['sections'] << MicrosoftTeams::Activity.new(options[:activity]).prepare - result['sections'] << { - 'title' => 'Details', - 'facts' => [{ 'name' => 'Attachments', 'value' => attachments }] - } if attachments.present? && attachments.empty? + attachments = options[:attachments] + unless attachments.blank? + result['sections'] << { + 'title' => 'Details', + 'facts' => [{ 'name' => 'Attachments', 'value' => attachments }] + } + end result.to_json end |