Class: Whatsapp::MessageTemplates::Component::Buttons

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby/whatsapp/message_templates/component/buttons.rb

Overview

The container for a template's tappable buttons. At most one per template, holding up to 10 buttons.

Note the asymmetry with the send side: here every button lives inside this one component, whereas sending a template emits one button component per button, addressed by a zero-based index that follows the order declared here. That is why #serialize preserves the caller's order exactly. Source: https://developers.facebook.com/docs/whatsapp/business-management-api/message-templates/components/

Defined Under Namespace

Modules: Defaults

Constant Summary collapse

MAX_PER_TYPE =

Documented per-type caps. Only the limits Meta actually publishes are enforced; OTP is deliberately uncapped here (the overall MAX_BUTTONS still applies) because no per-type limit is stated for it.

{
  Button::QuickReply::Defaults::TYPE => 10,
  Button::Url::Defaults::TYPE => 2,
  Button::PhoneNumber::Defaults::TYPE => 1,
  Button::CopyCode::Defaults::TYPE => 1,
}.freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#example_error, #parameter_format

Instance Method Summary collapse

Methods inherited from Base

#api_type

Methods included from ValueObject

included

Constructor Details

#initialize(buttons: []) ⇒ Buttons

Returns a new instance of Buttons.

Parameters:



41
42
43
44
45
46
47
# File 'lib/ruby/whatsapp/message_templates/component/buttons.rb', line 41

def initialize(buttons: [], **)
  super(**)

  @buttons = build_buttons(buttons)

  validate!
end

Instance Attribute Details

#buttonsArray<Button::Base>

Returns:



32
33
34
# File 'lib/ruby/whatsapp/message_templates/component/buttons.rb', line 32

def buttons
  @buttons
end

Instance Method Details

#api_typesArray<String>

The wire types in declared order. Exposed so Carousel can compare card structures and Template can inspect buttons across components.

Returns:

  • (Array<String>)


52
53
54
# File 'lib/ruby/whatsapp/message_templates/component/buttons.rb', line 52

def api_types
  buttons.map(&:api_type)
end

#serializeHash

Returns The serialized component.

Returns:

  • (Hash)

    The serialized component.



57
58
59
# File 'lib/ruby/whatsapp/message_templates/component/buttons.rb', line 57

def serialize
  { type: Defaults::TYPE, buttons: buttons.map(&:serialize) }
end