Class: Whatsapp::MessageTemplates::Component::Carousel::Card

Inherits:
Object
  • Object
show all
Includes:
ValueObject
Defined in:
lib/ruby/whatsapp/message_templates/component/carousel/card.rb

Overview

One card in a carousel: a required media header, an optional body, and up to two buttons.

Composes Header and Buttons rather than restating their rules — the only extra constraint is that a card's header must be image or video, since a carousel card cannot be text or a location.

Note there is no card_index here. Cards are positional at creation time; the zero-based card_index only appears when sending a message. Source: https://developers.facebook.com/docs/whatsapp/business-management-api/message-templates/carousel-templates

Defined Under Namespace

Modules: Defaults

Constant Summary collapse

ALLOWED_HEADER_FORMATS =

A carousel card's header is restricted to these formats.

[Header::Formats::IMAGE, Header::Formats::VIDEO].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ValueObject

included

Constructor Details

#initialize(header:, body: nil, buttons: nil, parameter_format: ParameterFormats::POSITIONAL) ⇒ Card

@raise [ActiveModel::ValidationError] if validation fails.

Parameters:

  • header (Hash, Component::Header)

    The card's media header.

  • body (Hash, Component::Body, nil) (defaults to: nil)

    The card's body.

  • buttons (Array<Hash, Button::Base>, nil) (defaults to: nil)

    Up to 2 buttons.

  • parameter_format (String, Symbol) (defaults to: ParameterFormats::POSITIONAL)

    The owning template's placeholder style, threaded into the card's own components.



49
50
51
52
53
54
55
56
# File 'lib/ruby/whatsapp/message_templates/component/carousel/card.rb', line 49

def initialize(header:, body: nil, buttons: nil, parameter_format: ParameterFormats::POSITIONAL)
  @parameter_format = ParameterFormats.normalize(parameter_format)
  @header = build(Header, header)
  @body = build(Body, body)
  @buttons = blank_value?(buttons) ? nil : Buttons.new(buttons:, parameter_format: @parameter_format)

  validate!
end

Instance Attribute Details

#bodyComponent::Body?

Returns:



33
34
35
# File 'lib/ruby/whatsapp/message_templates/component/carousel/card.rb', line 33

def body
  @body
end

#buttonsComponent::Buttons?

Returns:



37
38
39
# File 'lib/ruby/whatsapp/message_templates/component/carousel/card.rb', line 37

def buttons
  @buttons
end

#headerComponent::Header

Returns:



29
30
31
# File 'lib/ruby/whatsapp/message_templates/component/carousel/card.rb', line 29

def header
  @header
end

Instance Method Details

#serializeHash

Returns The serialized card.

Returns:

  • (Hash)

    The serialized card.



67
68
69
# File 'lib/ruby/whatsapp/message_templates/component/carousel/card.rb', line 67

def serialize
  { components: [header, body, buttons].compact.map(&:serialize) }
end

#signatureArray

A comparable description of the card's shape, used by Whatsapp::MessageTemplates::Component::Carousel to enforce Meta's "all cards must have the same components" rule. Content is deliberately excluded — only structure matters.

Returns:

  • (Array)


62
63
64
# File 'lib/ruby/whatsapp/message_templates/component/carousel/card.rb', line 62

def signature
  [header&.format, !body.nil?, buttons&.api_types]
end