Class: Whatsapp::MessageTemplates::Button

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/whatsapp/message_templates/button.rb,
lib/ruby/whatsapp/message_templates/button/otp.rb,
lib/ruby/whatsapp/message_templates/button/url.rb,
lib/ruby/whatsapp/message_templates/button/base.rb,
lib/ruby/whatsapp/message_templates/button/copy_code.rb,
lib/ruby/whatsapp/message_templates/button/quick_reply.rb,
lib/ruby/whatsapp/message_templates/button/phone_number.rb,
lib/ruby/whatsapp/message_templates/button/otp/supported_app.rb

Overview

Resolves a button 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 Whatsapp::Messages::Interactive::ACTION_TYPES.

Only the button types with a published schema are registered. Meta's Graph API enum also lists FLOW, MPM, CATALOG, VOICE_CALL, VIDEO_CALL, POSTBACK, BOOKING_STATUS, PAYMENT_REQUEST and REQUEST_CONTACT_INFO, but publishes no field reference for them, so they are deliberately absent rather than guessed. Source: https://developers.facebook.com/docs/whatsapp/business-management-api/message-templates/components/

Defined Under Namespace

Classes: Base, CopyCode, Otp, PhoneNumber, QuickReply, Url

Constant Summary collapse

TYPES =
{
  quick_reply: QuickReply,
  url: Url,
  phone_number: PhoneNumber,
  copy_code: CopyCode,
  otp: Otp,
}.freeze

Class Method Summary collapse

Class Method Details

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

Parameters:

  • type (Symbol, String)

    The button kind, in either casing (:quick_reply, "quick_reply", or Meta's "QUICK_REPLY").

  • attrs (Hash)

    Forwarded to the resolved button class.

Returns:

Raises:

  • (TemplateError)

    if the type is unknown.

  • (ActiveModel::ValidationError)

    if the button is invalid.



33
34
35
36
37
38
39
40
# File 'lib/ruby/whatsapp/message_templates/button.rb', line 33

def build(type:, **attrs)
  klass = TYPES.fetch(normalize(type)) do
    raise TemplateError,
      "Unknown button 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.



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

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

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

Builds and serializes in one step.

Returns:

  • (Hash)


44
45
46
# File 'lib/ruby/whatsapp/message_templates/button.rb', line 44

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