Class: BlockKit::Surfaces::Base

Inherits:
Base
  • Object
show all
Defined in:
lib/block_kit/surfaces/base.rb

Direct Known Subclasses

Home, Modal

Constant Summary collapse

MAX_BLOCKS =
100
SUPPORTED_BLOCKS =
[
  Layout::Actions,
  Layout::Context,
  Layout::Divider,
  Layout::Header,
  Layout::Image,
  Layout::Input,
  Layout::RichText,
  Layout::Section,
  Layout::Video
]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.

Raises:

  • (NotImplementedError)


52
53
54
55
56
# File 'lib/block_kit/surfaces/base.rb', line 52

def initialize(attributes = {})
  raise NotImplementedError, "#{self.class} is an abstract class and can't be instantiated." if instance_of?(Base)

  super
end

Class Method Details

.inherited(subclass) ⇒ Object



48
49
50
# File 'lib/block_kit/surfaces/base.rb', line 48

def self.inherited(subclass)
  subclass.attribute_fixers = attribute_fixers.deep_dup
end

Instance Method Details

#append(block) ⇒ Object

Overridden to return ‘self`, allowing chaining.



67
68
69
70
71
# File 'lib/block_kit/surfaces/base.rb', line 67

def append(block)
  blocks << block

  self
end

#as_jsonObject



73
74
75
76
77
78
79
80
# File 'lib/block_kit/surfaces/base.rb', line 73

def as_json(*)
  super.merge(
    blocks: blocks&.map(&:as_json),
    private_metadata: ,
    callback_id: callback_id,
    external_id: external_id
  ).compact
end

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



58
59
60
61
62
63
64
# File 'lib/block_kit/surfaces/base.rb', line 58

def image(alt_text:, image_url: nil, slack_file: nil, title: nil, emoji: nil, block_id: 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

  append(Layout::Image.new(image_url: image_url, slack_file: slack_file, alt_text: alt_text, title: title, block_id: block_id, emoji: emoji))
end