Class: Whatsapp::MessageTemplates::Component::Header

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby/whatsapp/message_templates/component/header.rb

Overview

The optional component above the body: a line of text, a media attachment, or a location pin. At most one per template.

Three shapes share one class because they all serialize to the same {type:, format:, ...} envelope and differ only in which fields are legal. The format decides:

TEXT     — `text` (60 chars, at most ONE placeholder) plus an `example`
media    — `header_handle` from the Resumable Upload API; no text
LOCATION — nothing else; coordinates are supplied when the message is sent

This mirrors the conditional-validation approach already used by Whatsapp::Messages::Video for its id-or-link choice. Source: https://developers.facebook.com/docs/whatsapp/business-management-api/message-templates/components/

Defined Under Namespace

Modules: Defaults, Formats

Constant Summary collapse

MARKDOWN_CHARACTERS =

Characters Meta rejects in a header, which does not support Markdown.

["*", "_", "~", "`"].freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#example_error, #parameter_format

Instance Method Summary collapse

Methods inherited from Base

#api_type

Methods included from ValueObject

included

Constructor Details

#initialize(format:, text: nil, example: nil, header_handle: nil) ⇒ Header

Returns a new instance of Header.

Parameters:

  • format (String, Symbol)
  • text (String, nil) (defaults to: nil)

    The header text, for the TEXT format.

  • example (Array, Hash, String, nil) (defaults to: nil)

    A sample value for the text placeholder, or a pre-built example payload.

  • header_handle (String, Array<String>, nil) (defaults to: nil)

    The uploaded asset handle, for media formats. A convenience alternative to example: { header_handle: [...] }.

  • kwargs (Hash)

    Forwarded to Base (parameter_format). @raise [ActiveModel::ValidationError] if validation fails.



73
74
75
76
77
78
79
80
81
82
# File 'lib/ruby/whatsapp/message_templates/component/header.rb', line 73

def initialize(format:, text: nil, example: nil, header_handle: nil, **)
  super(**)

  @format = normalize_format(format)
  @text = text
  @header_handle = header_handle
  @example = build_example(example)

  validate!
end

Instance Attribute Details

#exampleHash?

Returns The serialized example payload.

Returns:

  • (Hash, nil)

    The serialized example payload.



53
54
55
# File 'lib/ruby/whatsapp/message_templates/component/header.rb', line 53

def example
  @example
end

#formatString



45
46
47
# File 'lib/ruby/whatsapp/message_templates/component/header.rb', line 45

def format
  @format
end

#header_handleString, ...

Returns Media headers only.

Returns:

  • (String, Array<String>, nil)

    Media headers only.



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

def header_handle
  @header_handle
end

#textString?

Returns TEXT headers only.

Returns:

  • (String, nil)

    TEXT headers only.



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

def text
  @text
end

Instance Method Details

#media?Boolean

Returns Whether this header carries an uploaded asset.

Returns:

  • (Boolean)

    Whether this header carries an uploaded asset.



85
86
87
# File 'lib/ruby/whatsapp/message_templates/component/header.rb', line 85

def media?
  Formats::MEDIA.include?(format)
end

#serializeHash

Returns The serialized component.

Returns:

  • (Hash)

    The serialized component.



90
91
92
93
94
95
96
97
# File 'lib/ruby/whatsapp/message_templates/component/header.rb', line 90

def serialize
  {
    type: Defaults::TYPE,
    format:,
    text:,
    example:,
  }.compact
end