Class: Slackert::MessageBuilder

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

Overview

Builder class that allows to build a Slack message piece by piece. After the message has been constructed, it is built with builder.build method and can be passed to Alerter class for sending.

Supports blocks initialization that returns a ready alert/message

msg = Slackert::MessageBuilder.build do |b|
  b.add_header('header')
  ...
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMessageBuilder



27
28
29
# File 'lib/slackert/builder.rb', line 27

def initialize
  @content = []
end

Class Method Details

.build {|builder| ... } ⇒ Hash

Block initialization

Yields:

  • (builder)


21
22
23
24
25
# File 'lib/slackert/builder.rb', line 21

def self.build
  builder = new
  yield(builder)
  builder.build
end

Instance Method Details

#add_dividerObject

Adds a horizontal divider.



42
43
44
# File 'lib/slackert/builder.rb', line 42

def add_divider
  @content.push(Blocks::Divider.new.to_slack)
end

#add_header(text) ⇒ Object

Add a header to the message. It formats as a large title on top of the message.



34
35
36
37
38
# File 'lib/slackert/builder.rb', line 34

def add_header(text)
  return if text.empty?

  @content.push(Blocks::Header.new(text).to_slack)
end

#add_markdown_text(text) ⇒ Object

Adds markdown text to the message.



49
50
51
52
53
54
55
# File 'lib/slackert/builder.rb', line 49

def add_markdown_text(text)
  return if text.empty?

  mkd_section = Blocks::Section.new
  mkd_section.add_field_text(text)
  add_section(mkd_section)
end

#add_plain_text(text) ⇒ Object

Adds plain text to the message.



60
61
62
63
64
65
66
# File 'lib/slackert/builder.rb', line 60

def add_plain_text(text)
  return if text.empty?

  text_section = Blocks::Section.new
  text_section.add_section_text(text)
  add_section(text_section)
end

#add_section(section) ⇒ Object

Adds a Blocks::Section block element to the message.



86
87
88
# File 'lib/slackert/builder.rb', line 86

def add_section(section)
  @content.push(section.to_slack)
end

#buildHash

Builds a message. Creates a hash object from the added fields in the builder.



100
101
102
103
104
# File 'lib/slackert/builder.rb', line 100

def build
  {
    'blocks': @content
  }
end

#notify_users(user_ids, msg_prefix = 'Please look into this: ', delim = ' | ') ⇒ Object

Adds Slack user notifications.



73
74
75
76
77
78
79
80
81
# File 'lib/slackert/builder.rb', line 73

def notify_users(user_ids, msg_prefix = 'Please look into this: ', delim = ' | ')
  return if user_ids.empty?

  tag_section = Blocks::Section.new
  user_notifs = user_ids.map { |user_id| "<@#{user_id}>" }
  notifs_msg = user_notifs.join(delim)
  tag_section.add_section_text(msg_prefix + notifs_msg)
  add_section(tag_section)
end

#prepend_section(section) ⇒ Object

Inserts a Blocks::Section block element on top of the message.



93
94
95
# File 'lib/slackert/builder.rb', line 93

def prepend_section(section)
  @content.unshift(section.to_slack)
end