Class: Slack::BlockKit::Block::SectionBlock

Inherits:
Slack::BlockKit::Block show all
Defined in:
lib/slack/block_kit/block/section_block.rb

Instance Attribute Summary collapse

Attributes inherited from Slack::BlockKit::Block

#block_id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Slack::BlockKit::Block

[], #type

Constructor Details

#initializeSectionBlock

Returns a new instance of SectionBlock.



17
18
19
# File 'lib/slack/block_kit/block/section_block.rb', line 17

def initialize
  @fields = TypeRestrictedArray.new(CompositionObjects::Text)
end

Instance Attribute Details

#accessoryObject

Returns the value of attribute accessory.



8
9
10
# File 'lib/slack/block_kit/block/section_block.rb', line 8

def accessory
  @accessory
end

#fieldsObject (readonly)

Returns the value of attribute fields.



8
9
10
# File 'lib/slack/block_kit/block/section_block.rb', line 8

def fields
  @fields
end

#textObject

Returns the value of attribute text.



8
9
10
# File 'lib/slack/block_kit/block/section_block.rb', line 8

def text
  @text
end

Class Method Details

.populate(hash, object) ⇒ Object



10
11
12
13
14
15
# File 'lib/slack/block_kit/block/section_block.rb', line 10

def self.populate(hash, object)
  object.accessory = hash[:accessory] if hash.key?(:accessory)
  if hash.key?(:text) then object.text = hash[:text]
  elsif hash.key?(:fields) then hash[:fields].each(&object.fields.method(:<<))
  end
end

Instance Method Details

#to_hObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/slack/block_kit/block/section_block.rb', line 40

def to_h
  if text && text.text.size > 3000
    raise RangeError, 'text in SectionBlock has max 3000 characters'
  end

  super.merge(
    block_id: block_id,
    text: text&.to_h,
    fields: fields.map(&:to_h),
    accessory: accessory&.to_h
  ).compact
end

#valid?Boolean

Either text or fields must exist and be non-empty.

Returns:

  • (Boolean)


22
23
24
25
26
# File 'lib/slack/block_kit/block/section_block.rb', line 22

def valid?
  if @text.nil? || @text.empty? then !@fields.empty?
  else !@text&.empty?
  end
end