Class: Whatsapp::MessageTemplates::Button
- Inherits:
-
Object
- Object
- Whatsapp::MessageTemplates::Button
- 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
- .build(type:, **attrs) ⇒ Button::Base
-
.normalize(type) ⇒ Symbol
The registry key for the given type.
-
.serialize(type:, **attrs) ⇒ Hash
Builds and serializes in one step.
Class Method Details
.build(type:, **attrs) ⇒ Button::Base
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.
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.
44 45 46 |
# File 'lib/ruby/whatsapp/message_templates/button.rb', line 44 def serialize(type:, **attrs) build(type:, **attrs).serialize end |