Class: OnyxCord::Webhooks::View

Inherits:
Object
  • Object
show all
Defined in:
lib/onyxcord/webhooks/view.rb

Overview

A reusable view representing a component collection, with builder methods.

Defined Under Namespace

Classes: ContainerBuilder, FileBuilder, MediaGalleryBuilder, RowBuilder, SectionBuilder, SelectMenuBuilder, SeparatorBuilder, TextDisplayBuilder

Constant Summary collapse

BUTTON_STYLES =

Possible button style names and values.

{
  primary: 1,
  secondary: 2,
  success: 3,
  danger: 4,
  link: 5,
  premium: 6
}.freeze
SEPARATOR_SIZES =

Possible separator size names and values.

{
  small: 1,
  large: 2
}.freeze
COMPONENT_TYPES =

Component types.

{
  action_row: 1,
  button: 2,
  string_select: 3,
  # text_input: 4, # (defined in modal.rb)
  user_select: 5,
  role_select: 6,
  mentionable_select: 7,
  channel_select: 8,
  section: 9,
  text_display: 10,
  thumbnail: 11,
  media_gallery: 12,
  file: 13,
  separator: 14,
  container: 17
  # label: 18, # (defined in modal.rb)
  # file_upload: 19, (defined in modal.rb)
  # radio_group: 21, (defined in modal.rb)
  # checkbox_group: 22, (defined in modal.rb)
  # checkbox: 23 (defined in modal.rb)
}.freeze
IS_COMPONENTS_V2 =
OnyxCord::MessageComponents::IS_COMPONENTS_V2
V2_COMPONENT_TYPES =
OnyxCord::MessageComponents::V2_COMPONENT_TYPES

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.apply_v2_flag(flags, components, force: false) ⇒ Object



491
492
493
# File 'lib/onyxcord/webhooks/view.rb', line 491

def self.apply_v2_flag(flags, components, force: false)
  OnyxCord::MessageComponents.apply_v2_flag(flags, components, force: force)
end

.component_payload(components) ⇒ Object



487
488
489
# File 'lib/onyxcord/webhooks/view.rb', line 487

def self.component_payload(components)
  OnyxCord::MessageComponents.payload(components)
end

.components_v2?(components) ⇒ Boolean

Returns:

  • (Boolean)


483
484
485
# File 'lib/onyxcord/webhooks/view.rb', line 483

def self.components_v2?(components)
  OnyxCord::MessageComponents.components_v2?(components)
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


469
470
471
# File 'lib/onyxcord/webhooks/view.rb', line 469

def any?
  @components.any?
end

#components_v2?Boolean

Returns:

  • (Boolean)


473
474
475
# File 'lib/onyxcord/webhooks/view.rb', line 473

def components_v2?
  self.class.components_v2?(to_a)
end

#container(id: nil, color: nil, colour: nil, spoiler: false) {|builder| ... } ⇒ Object

Add a container component to the view.



533
534
535
536
537
538
# File 'lib/onyxcord/webhooks/view.rb', line 533

def container(id: nil, color: nil, colour: nil, spoiler: false, &block)
  builder = ContainerBuilder.new(id: id, color: color, colour: colour, spoiler: spoiler)
  @components << builder
  yield builder if block_given?
  builder
end

#empty?Boolean

Returns:

  • (Boolean)


465
466
467
# File 'lib/onyxcord/webhooks/view.rb', line 465

def empty?
  @components.empty?
end

#fileObject Also known as: file_display

Add a file component to the view.



506
507
508
509
510
# File 'lib/onyxcord/webhooks/view.rb', line 506

def file(...)
  builder = FileBuilder.new(...)
  @components << builder
  builder
end

#flags(flags = 0) ⇒ Object



479
480
481
# File 'lib/onyxcord/webhooks/view.rb', line 479

def flags(flags = 0)
  self.class.apply_v2_flag(flags, to_a)
end

Add a media gallery component to the view.



550
551
552
553
554
555
# File 'lib/onyxcord/webhooks/view.rb', line 550

def media_gallery(id: nil, &block)
  builder = MediaGalleryBuilder.new(id: id)
  @components << builder
  yield builder if block_given?
  builder
end

#row(id: nil) {|builder| ... } ⇒ Object

Add a row component to the view.

Yields:

  • (builder)

See Also:

  • OnyxCord::Webhooks::View::RowBuilder#initialize


497
498
499
500
501
502
# File 'lib/onyxcord/webhooks/view.rb', line 497

def row(id: nil, &block)
  builder = RowBuilder.new(id: id)
  @components << builder
  yield builder if block_given?
  builder
end

#section(id: nil) {|builder| ... } ⇒ Object

Add a section component to the view.



516
517
518
519
520
521
# File 'lib/onyxcord/webhooks/view.rb', line 516

def section(id: nil, &block)
  builder = SectionBuilder.new(id: id)
  @components << builder
  yield builder if block_given?
  builder
end

#separatorObject

Add a separator component to the view.



525
526
527
528
529
# File 'lib/onyxcord/webhooks/view.rb', line 525

def separator(...)
  builder = SeparatorBuilder.new(...)
  @components << builder
  builder
end

#text_displayObject

Add a text display component to the view.



542
543
544
545
546
# File 'lib/onyxcord/webhooks/view.rb', line 542

def text_display(...)
  builder = TextDisplayBuilder.new(...)
  @components << builder
  builder
end

#v2?Boolean

Returns:

  • (Boolean)


477
478
479
# File 'lib/onyxcord/webhooks/view.rb', line 477

def components_v2?
  self.class.components_v2?(to_a)
end