Class: BlockKit::Layout::Section

Inherits:
Base
  • Object
show all
Defined in:
lib/block_kit/layout/section.rb

Constant Summary collapse

MAX_TEXT_LENGTH =
3000
MAX_FIELDS =
10
MAX_FIELD_TEXT_LENGTH =
2000
SUPPORTED_ELEMENTS =
[
  Elements::Button,
  Elements::ChannelsSelect,
  Elements::Checkboxes,
  Elements::ConversationsSelect,
  Elements::DatePicker,
  Elements::ExternalSelect,
  Elements::Image,
  Elements::MultiChannelsSelect,
  Elements::MultiConversationsSelect,
  Elements::MultiExternalSelect,
  Elements::MultiStaticSelect,
  Elements::MultiUsersSelect,
  Elements::Overflow,
  Elements::RadioButtons,
  Elements::StaticSelect,
  Elements::TimePicker,
  Elements::UsersSelect,
  Elements::WorkflowButton
].freeze

Constants inherited from Base

Base::MAX_BLOCK_ID_LENGTH

Instance Method Summary collapse

Methods inherited from Base

inherited, #initialize

Methods inherited from Base

#==, fix, #fix_validation_errors, #fix_validation_errors!, fixes, inherited, #initialize, inspect, #inspect, #pretty_print, #to_json

Constructor Details

This class inherits a constructor from BlockKit::Layout::Base

Instance Method Details

#as_jsonObject



108
109
110
111
112
113
114
115
# File 'lib/block_kit/layout/section.rb', line 108

def as_json(*)
  super.merge(
    text: text&.as_json,
    fields: fields&.map(&:as_json),
    accessory: accessory&.as_json,
    expand: expand
  ).compact
end

#expand?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/block_kit/layout/section.rb', line 51

def expand?
  !!expand
end

#field(text:, type: :mrkdwn, verbatim: nil, emoji: nil) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/block_kit/layout/section.rb', line 87

def field(text:, type: :mrkdwn, verbatim: nil, emoji: nil)
  case type.to_sym
  when :mrkdwn
    mrkdwn_field(text: text, verbatim: verbatim)
  when :plain_text
    plain_text_field(text: text, emoji: emoji)
  else
    raise ArgumentError, "Invalid field type: #{type} (must be mrkdwn or plain_text)"
  end
end

#image(alt_text:, image_url: nil, slack_file: nil) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/block_kit/layout/section.rb', line 98

def image(alt_text:, image_url: nil, slack_file: nil)
  if (image_url.nil? && slack_file.nil?) || (image_url && slack_file)
    raise ArgumentError, "Must provide either image_url or slack_file, but not both."
  end

  self.accessory = Elements::Image.new(alt_text: alt_text, image_url: image_url, slack_file: slack_file)

  self
end