Class: Whatsapp::Messages::Template::Component

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/ruby/whatsapp/messages/template/component.rb

Overview

Component for template messages (header, body, button)

Defined Under Namespace

Modules: SubTypes, Types

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, sub_type: nil, parameters: [], index: nil) ⇒ Component

Returns a new instance of Component.

Parameters:

  • type (String)

    The type of component (header, body, button).

  • sub_type (String, nil) (defaults to: nil)

    The sub-type for button components (quick_reply, url).

  • parameters (Array<Hash, Parameter>) (defaults to: [])

    Array of parameter hashes.

  • index (Integer, nil) (defaults to: nil)

    Button index for multiple buttons.



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

#indexInteger?

Returns:

  • (Integer, nil)


37
38
39
# File 'lib/ruby/whatsapp/messages/template/component.rb', line 37

def index
  @index
end

#parametersArray<Parameter>

Returns:



33
34
35
# File 'lib/ruby/whatsapp/messages/template/component.rb', line 33

def parameters
  @parameters
end

#sub_typeString?

Returns:

  • (String, nil)


29
30
31
# File 'lib/ruby/whatsapp/messages/template/component.rb', line 29

def sub_type
  @sub_type
end

#typeString

Returns:

  • (String)


25
26
27
# File 'lib/ruby/whatsapp/messages/template/component.rb', line 25

def type
  @type
end

Instance Method Details

#serializeHash

Serializes the component to a hash format suitable for the WhatsApp API.

Returns:

  • (Hash)

    The serialized component.



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