Class: Whatsapp::MessageTemplates::ComponentSet

Inherits:
Object
  • Object
show all
Includes:
ValueObject
Defined in:
lib/ruby/whatsapp/message_templates/component_set.rb

Overview

A template's components, plus every rule that spans more than one of them.

This is the other half of the split described in Component: a component class validates itself, and everything about how components relate lives here. That keeps rules like "a footer is forbidden alongside a limited-time offer" in one place instead of duplicated across the sibling classes that would each only see half the picture.

It is a separate object from Template because editing a template replaces its components wholesale without re-supplying a name or language, so update needs exactly these checks and none of the template-identity ones.

category is optional for that reason: when it is absent — as when editing — the category-dependent rules are skipped rather than guessed at.

Defined Under Namespace

Modules: Defaults

Constant Summary collapse

SINGLETON_TYPES =

Wire types that may appear at most once in a template.

[
  Component::Header::Defaults::TYPE,
  Component::Footer::Defaults::TYPE,
  Component::Buttons::Defaults::TYPE,
  Component::Carousel::Defaults::TYPE,
  Component::LimitedTimeOffer::Defaults::TYPE,
].freeze
CATEGORY_RESTRICTED_TYPES =

Components restricted to a subset of categories, and which ones.

{
  Component::Carousel::Defaults::TYPE => [Categories::MARKETING].freeze,
  Component::LimitedTimeOffer::Defaults::TYPE => [Categories::MARKETING].freeze,
}.freeze
LIMITED_TIME_OFFER_HEADER_FORMATS =

Header formats a limited-time-offer template may use.

[
  Component::Header::Formats::IMAGE,
  Component::Header::Formats::VIDEO,
].freeze
LOCATION_HEADER_CATEGORIES =

Categories that may use a location header.

[Categories::UTILITY, Categories::MARKETING].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ValueObject

included

Constructor Details

#initialize(components:, category: nil, parameter_format: ParameterFormats::POSITIONAL) ⇒ ComponentSet

@raise [ActiveModel::ValidationError] if validation fails. @raise [TemplateError] if a component type is unknown.

Parameters:

  • components (Array<Hash, Component::Base>)

    The template's components.

  • category (String, Symbol, nil) (defaults to: nil)

    The owning template's category. Omit to skip the category-dependent rules.

  • parameter_format (String, Symbol) (defaults to: ParameterFormats::POSITIONAL)

    The placeholder style, threaded into every component.



79
80
81
82
83
84
85
# File 'lib/ruby/whatsapp/message_templates/component_set.rb', line 79

def initialize(components:, category: nil, parameter_format: ParameterFormats::POSITIONAL)
  @category = Categories.normalize(category)
  @parameter_format = ParameterFormats.normalize(parameter_format)
  @components = build_components(components)

  validate!
end

Instance Attribute Details

#categoryString?

Returns:

  • (String, nil)


59
60
61
# File 'lib/ruby/whatsapp/message_templates/component_set.rb', line 59

def category
  @category
end

#componentsArray<Component::Base>

Returns:



55
56
57
# File 'lib/ruby/whatsapp/message_templates/component_set.rb', line 55

def components
  @components
end

#parameter_formatString

Returns:

  • (String)


63
64
65
# File 'lib/ruby/whatsapp/message_templates/component_set.rb', line 63

def parameter_format
  @parameter_format
end

Instance Method Details

#api_typesArray<String>

Returns The wire types present, in declared order.

Returns:

  • (Array<String>)

    The wire types present, in declared order.



94
95
96
# File 'lib/ruby/whatsapp/message_templates/component_set.rb', line 94

def api_types
  components.map(&:api_type)
end

#find(api_type) ⇒ Component::Base?

Parameters:

  • api_type (String)

    A component wire type, e.g. "BODY".

Returns:



89
90
91
# File 'lib/ruby/whatsapp/message_templates/component_set.rb', line 89

def find(api_type)
  components.find { |component| component.api_type == api_type }
end

#serializeArray<Hash>

Returns The serialized components.

Returns:

  • (Array<Hash>)

    The serialized components.



99
100
101
# File 'lib/ruby/whatsapp/message_templates/component_set.rb', line 99

def serialize
  components.map(&:serialize)
end