Sha256: d696ee3d5272da82d3c6a363121783528d760627c1af93aa4496472f3231ce3e

Contents?: true

Size: 1.96 KB

Versions: 4

Compression:

Stored size: 1.96 KB

Contents

# typed: strict
# frozen_string_literal: true

module WhatsappSdk
  module Resource
    class InteractiveActionSection
      extend T::Sig

      # Returns the ActionSection title you want to send.
      #
      # @returns title [String]. The character limit is 24 characters.
      sig { returns(String) }
      attr_accessor :title

      # Returns the ActionSection rows you want to send.
      #
      # @returns id [T::Array[InteractiveActionSectionRow]]. There must be at least one rows object.
      sig { returns(T::Array[InteractiveActionSectionRow]) }
      attr_accessor :rows

      sig { params(row: InteractiveActionSectionRow).void }
      def add_row(row)
        @rows << row
      end

      ACTION_SECTION_TITLE_MAXIMUM = 24
      ACTION_SECTION_ROWS_MAXIMUM = 10

      sig { params(title: String, rows: T::Array[InteractiveActionSectionRow]).void }
      def initialize(title:, rows: [])
        @title = title
        @rows = rows
        validate(skip_rows: true)
      end

      def to_json
        {
          title: title,
          rows: rows.map(&:to_json)
        }
      end

      sig { params(skip_rows: T.nilable(T::Boolean)).void }
      def validate(skip_rows: false)
        validate_title
        validate_rows unless skip_rows
      end

      private

      sig { void }
      def validate_title
        title_length = title.length
        return if title_length <= ACTION_SECTION_TITLE_MAXIMUM

        raise Errors::InvalidInteractiveActionSection,
              "Invalid length #{title_length} for title in section. Maximum length: " \
              "#{ACTION_SECTION_TITLE_MAXIMUM} characters."
      end

      sig { void }
      def validate_rows
        rows_length = rows.length
        return if rows_length <= ACTION_SECTION_ROWS_MAXIMUM

        raise Errors::InvalidInteractiveActionSection,
              "Invalid number of rows #{rows_length} in section. Maximum count: " \
              "#{ACTION_SECTION_ROWS_MAXIMUM}."
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
whatsapp_sdk-0.12.1 lib/whatsapp_sdk/resource/interactive_action_section.rb
whatsapp_sdk-0.12.0 lib/whatsapp_sdk/resource/interactive_action_section.rb
whatsapp_sdk-0.11.0 lib/whatsapp_sdk/resource/interactive_action_section.rb
whatsapp_sdk-0.10.0 lib/whatsapp_sdk/resource/interactive_action_section.rb