Class: Slackert::Blocks::Section

Inherits:
BlockElement show all
Defined in:
lib/slackert/blocks.rb

Overview

Section block element is a very flexible layout block that can serve as a simple text block but it also allows for adding block elements such as field text, buttons, images and more. Currently only section text and field texts are supported.

To learn more, visit api.slack.com/reference/block-kit/blocks#section

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSection

Returns a new instance of Section.



62
63
64
65
66
# File 'lib/slackert/blocks.rb', line 62

def initialize
  @text = {}
  @fields = []
  super('section')
end

Class Method Details

.new_from_hash(values, line_break: true, bold_keys: true) ⇒ Section

Initialize field text objects from a hash.

Parameters:

  • key, (Hash)

    value pairs that each will become a field text object

  • line_break (Boolean) (defaults to: true)

    add a line break after each key so values render right under the key instead of next to it

  • bold_keys (Boolean) (defaults to: true)

    apply bold text formatting to the keys

Returns:



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/slackert/blocks.rb', line 74

def self.new_from_hash(values, line_break: true, bold_keys: true)
  s = new

  values.each do |key, value|
    title = bold_keys ? "*#{key}*" : key
    title = line_break ? "#{title}\n" : title
    s.add_field_text("#{title}#{value}")
  end

  s
end

Instance Method Details

#add_field_text(message, type = 'mrkdwn') ⇒ Object

Adds a field text object to the message. Field texts render in two columns on desktop and are added left to right. They show as one column on mobile. There can only be 10 fields added in total and each text item has a limit of 2000 characters.

Parameters:

  • message (String)

    field text message

  • type (String) (defaults to: 'mrkdwn')

    can be either mrkdwn or plain_text

Raises:

  • (RuntimeError)

    if maximum capacity of 10 field objects has been reached



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/slackert/blocks.rb', line 107

def add_field_text(message, type = 'mrkdwn')
  raise StandardError, 'Maximum field text objects has been reached.' if @fields.length == 10
  raise ArgumentError, 'Maximum number of characters in text exceeded' if message.length > 2000

  @fields.push(
    {
      'type': type,
      'text': message
    }
  )
end

#add_section_text(message, type = 'mrkdwn') ⇒ Object

Adds a text object on top of the section. There can only be one section text added. Adding more will replace the previously added section text. It is limited to 3000 characters.

Parameters:

  • message (String)

    section text message

  • type (String) (defaults to: 'mrkdwn')

    can be either mrkdwn or plain_text

Raises:

  • (ArgumentError)


91
92
93
94
95
96
97
98
# File 'lib/slackert/blocks.rb', line 91

def add_section_text(message, type = 'mrkdwn')
  raise ArgumentError, 'Maximum number of characters in text exceeded' if message.length > 3000

  @text = {
    'type': type,
    'text': message
  }
end

#to_slackObject



120
121
122
123
124
125
126
127
128
129
# File 'lib/slackert/blocks.rb', line 120

def to_slack
  if @text.empty? && @fields.empty?
    raise 'Either section text or field text needs to be filled in order to compose the section.'
  end

  section = { 'type': @type }
  section['text'] = @text unless @text.empty?
  section['fields'] = @fields unless @fields.empty?
  section
end