Class: SlackNotification

Inherits:
Object
  • Object
show all
Defined in:
lib/slack_notification.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SlackNotification

Returns a new instance of SlackNotification.



8
9
10
11
12
13
14
15
16
17
# File 'lib/slack_notification.rb', line 8

def initialize(options = {})
  @channel = validated_channel(options[:channel].to_s)
  @type = validated_type(options[:type])
  @title = options[:title]
  @fallback = options[:fallback]
  @dryrun = options[:dryrun] || (defined?(Rails) && Rails.env.test?)
  @fields = validated_fields(options[:fields])
  @blocks = options[:blocks]
  @footer = options[:footer]
end

Instance Attribute Details

#blocksObject

Returns the value of attribute blocks.



6
7
8
# File 'lib/slack_notification.rb', line 6

def blocks
  @blocks
end

#channelObject

Returns the value of attribute channel.



6
7
8
# File 'lib/slack_notification.rb', line 6

def channel
  @channel
end

#dryrunObject

Returns the value of attribute dryrun.



6
7
8
# File 'lib/slack_notification.rb', line 6

def dryrun
  @dryrun
end

#fallbackObject

Returns the value of attribute fallback.



6
7
8
# File 'lib/slack_notification.rb', line 6

def fallback
  @fallback
end

#fieldsObject

Returns the value of attribute fields.



6
7
8
# File 'lib/slack_notification.rb', line 6

def fields
  @fields
end

#titleObject

Returns the value of attribute title.



6
7
8
# File 'lib/slack_notification.rb', line 6

def title
  @title
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/slack_notification.rb', line 6

def type
  @type
end

Instance Method Details

#dataObject



19
20
21
22
23
24
25
26
27
# File 'lib/slack_notification.rb', line 19

def data
  @data = {}
  @data['title'] = @title || ENV['SLACK_DEFAULT_TITLE'] || 'Notification'
  @data['fallback'] = @fallback || @data['title']
  @data['fields'] = @fields if @fields
  @data['color'] = slack_color
  @data['footer'] = footer if footer
  @data
end

#notify!Object



29
30
31
# File 'lib/slack_notification.rb', line 29

def notify!
  @dryrun ? data : notifier.post(attachments: [data], blocks: @blocks)
end