Class: Slackert::Blocks::Section
- Inherits:
-
BlockElement
- Object
- BlockElement
- Slackert::Blocks::Section
- 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
-
.new_from_hash(values, line_break: true, bold_keys: true) ⇒ Section
Initialize field text objects from a hash.
Instance Method Summary collapse
-
#add_field_text(message, type = 'mrkdwn') ⇒ Object
Adds a field text object to the message.
-
#add_section_text(message, type = 'mrkdwn') ⇒ Object
Adds a text object on top of the section.
-
#initialize ⇒ Section
constructor
A new instance of Section.
- #to_slack ⇒ Object
Constructor Details
#initialize ⇒ Section
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.
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.
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/slackert/blocks.rb', line 107 def add_field_text(, 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 .length > 2000 @fields.push( { 'type': type, 'text': } ) 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.
91 92 93 94 95 96 97 98 |
# File 'lib/slackert/blocks.rb', line 91 def add_section_text(, type = 'mrkdwn') raise ArgumentError, 'Maximum number of characters in text exceeded' if .length > 3000 @text = { 'type': type, 'text': } end |
#to_slack ⇒ Object
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 |