Class: Slack::BlockKit::Layout::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/slack/block_kit/layout/context.rb

Overview

Displays message context, which can include both images and text.

api.slack.com/reference/messaging/blocks#context

Constant Summary collapse

TYPE =
'context'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block_id: nil) {|_self| ... } ⇒ Context

Returns a new instance of Context.

Yields:

  • (_self)

Yield Parameters:



14
15
16
17
18
19
# File 'lib/slack/block_kit/layout/context.rb', line 14

def initialize(block_id: nil)
  @block_id = block_id
  @elements = []

  yield(self) if block_given?
end

Instance Attribute Details

#elementsObject

Returns the value of attribute elements.



12
13
14
# File 'lib/slack/block_kit/layout/context.rb', line 12

def elements
  @elements
end

Instance Method Details

#append(element) ⇒ Object



33
34
35
36
37
# File 'lib/slack/block_kit/layout/context.rb', line 33

def append(element)
  @elements << element

  self
end

#as_jsonObject



39
40
41
42
43
44
45
# File 'lib/slack/block_kit/layout/context.rb', line 39

def as_json(*)
  {
    type: TYPE,
    elements: @elements.map(&:as_json),
    block_id: @block_id
  }.compact
end

#image(url:, alt_text:) ⇒ Object



21
22
23
# File 'lib/slack/block_kit/layout/context.rb', line 21

def image(url:, alt_text:)
  append(Element::Image.new(image_url: url, alt_text: alt_text))
end

#mrkdwn(text:, verbatim: nil) ⇒ Object



29
30
31
# File 'lib/slack/block_kit/layout/context.rb', line 29

def mrkdwn(text:, verbatim: nil)
  append(Composition::Mrkdwn.new(text: text, verbatim: verbatim))
end

#plain_text(text:, emoji: nil) ⇒ Object



25
26
27
# File 'lib/slack/block_kit/layout/context.rb', line 25

def plain_text(text:, emoji: nil)
  append(Composition::PlainText.new(text: text, emoji: emoji))
end