Class: Whatsapp::MessageTemplates::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/whatsapp/message_templates/component.rb,
lib/ruby/whatsapp/message_templates/component/base.rb,
lib/ruby/whatsapp/message_templates/component/body.rb,
lib/ruby/whatsapp/message_templates/component/footer.rb,
lib/ruby/whatsapp/message_templates/component/header.rb,
lib/ruby/whatsapp/message_templates/component/buttons.rb,
lib/ruby/whatsapp/message_templates/component/carousel.rb,
lib/ruby/whatsapp/message_templates/component/carousel/card.rb,
lib/ruby/whatsapp/message_templates/component/limited_time_offer.rb

Overview

Resolves a component kind to its implementing class.

Resolution goes through the frozen TYPES whitelist — never const_get on caller input — so an unknown or hostile type can only raise, never resolve an arbitrary constant. Mirrors Whatsapp::Messages::KINDS and Button::TYPES.

Only the component types with a published schema are registered. Meta's Graph API enum also lists GREETING, ALBUM, CALL_PERMISSION_REQUEST, TAP_TARGET_CONFIGURATION and ATTACHMENT, but publishes no field reference for them, so they are deliberately absent rather than guessed.

Note that a component validates only itself here. Rules that span components — a footer being forbidden alongside a limited-time offer, a carousel implying the MARKETING category — live on Template, which is the only object that can see all the siblings. Source: https://developers.facebook.com/docs/whatsapp/business-management-api/message-templates/components/

Defined Under Namespace

Classes: Base, Body, Buttons, Carousel, Footer, Header, LimitedTimeOffer

Constant Summary collapse

TYPES =
{
  header: Header,
  body: Body,
  footer: Footer,
  buttons: Buttons,
  carousel: Carousel,
  limited_time_offer: LimitedTimeOffer,
}.freeze

Class Method Summary collapse

Class Method Details

.build(type:, **attrs) ⇒ Component::Base

Parameters:

  • type (Symbol, String)

    The component kind, in either casing (:body, "body", or Meta's "BODY").

  • attrs (Hash)

    Forwarded to the resolved component class, including the owning template's parameter_format.

Returns:

Raises:

  • (TemplateError)

    if the type is unknown.

  • (ActiveModel::ValidationError)

    if the component is invalid.



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

def build(type:, **attrs)
  klass = TYPES.fetch(normalize(type)) do
    raise TemplateError,
      "Unknown component type: #{type.inspect}. Known types: #{TYPES.keys.join(', ')}"
  end

  klass.new(**attrs)
end

.normalize(type) ⇒ Symbol

Returns The registry key for the given type.

Parameters:

  • type (Symbol, String)

Returns:

  • (Symbol)

    The registry key for the given type.



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

def normalize(type)
  type.to_s.downcase.to_sym
end

.serialize(type:, **attrs) ⇒ Hash

Builds and serializes in one step.

Returns:

  • (Hash)


50
51
52
# File 'lib/ruby/whatsapp/message_templates/component.rb', line 50

def serialize(type:, **attrs)
  build(type:, **attrs).serialize
end