Class: Whatsapp::Messages::Template::Component
- Inherits:
-
Object
- Object
- Whatsapp::Messages::Template::Component
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/ruby/whatsapp/messages/template/component.rb
Overview
Component for template messages (header, body, button)
Defined Under Namespace
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#initialize(type:, sub_type: nil, parameters: [], index: nil) ⇒ Component
constructor
A new instance of Component.
-
#serialize ⇒ Hash
Serializes the component to a hash format suitable for the WhatsApp API.
Constructor Details
#initialize(type:, sub_type: nil, parameters: [], index: nil) ⇒ Component
Returns a new instance of Component.
45 46 47 48 49 50 51 52 |
# File 'lib/ruby/whatsapp/messages/template/component.rb', line 45 def initialize(type:, sub_type: nil, parameters: [], index: nil) @type = type @sub_type = sub_type @parameters = parameters.map { |param| param.is_a?(Parameter) ? param : Parameter.new(**param) } @index = index validate! end |
Instance Attribute Details
#index ⇒ Integer?
37 38 39 |
# File 'lib/ruby/whatsapp/messages/template/component.rb', line 37 def index @index end |
#parameters ⇒ Array<Parameter>
33 34 35 |
# File 'lib/ruby/whatsapp/messages/template/component.rb', line 33 def parameters @parameters end |
#sub_type ⇒ String?
29 30 31 |
# File 'lib/ruby/whatsapp/messages/template/component.rb', line 29 def sub_type @sub_type end |
#type ⇒ String
25 26 27 |
# File 'lib/ruby/whatsapp/messages/template/component.rb', line 25 def type @type end |
Instance Method Details
#serialize ⇒ Hash
Serializes the component to a hash format suitable for the WhatsApp API.
56 57 58 59 60 61 62 63 |
# File 'lib/ruby/whatsapp/messages/template/component.rb', line 56 def serialize result = { type: } result[:sub_type] = sub_type if sub_type result[:index] = index if index result[:parameters] = parameters.map(&:serialize) if parameters.any? result.compact end |