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
18
# 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?)
  @only_blocks = options[:only_blocks]
  @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

#only_blocksObject

Returns the value of attribute only_blocks.



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

def only_blocks
  @only_blocks
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



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

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



30
31
32
33
34
# File 'lib/slack_notification.rb', line 30

def notify!
  options = { blocks: @blocks }
  options.merge!(attachments: [data]) unless only_blocks
  @dryrun ? data : notifier.post(**options)
end