Sha256: c0d7dc372b6906ba2d3e39e1e75837d9233dd1591d813df899e91ead9f5608ac

Contents?: true

Size: 1.49 KB

Versions: 5

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module WhatsappSdk
  module Resource
    class ButtonParameter
      class InvalidType < StandardError
        attr_accessor :message

        def initialize(type)
          @message = "invalid type #{type}. type should be text or payload"
          super
        end
      end

      # Returns the button parameter type.
      #
      # @returns type [String] Valid options are payload and text.
      attr_accessor :type

      module Type
        TEXT = "text"
        PAYLOAD = "payload"

        VALID_TYPES = [PAYLOAD, TEXT].freeze
      end

      # Required for quick_reply buttons.
      # Returns the button payload. Developer-defined payload that is returned when the button is clicked
      # in addition to the display text on the button.
      #
      # @returns payload [String]
      attr_accessor :payload

      # Required for URL buttons.
      # Developer-provided suffix that is appended to the predefined prefix URL in the template.
      #
      # @returns text [String]
      attr_accessor :text

      def initialize(type:, payload: nil, text: nil)
        @type = type
        @payload = payload
        @text = text
        validate
      end

      def to_json(*_args)
        json = {
          type: type
        }
        json[:payload] = payload if payload
        json[:text] = text if text
        json
      end

      private

      def validate
        return if Type::VALID_TYPES.include?(type)

        raise InvalidType, type
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
whatsapp_sdk-0.3.2 lib/whatsapp_sdk/resource/button_parameter.rb
whatsapp_sdk-0.3.1 lib/whatsapp_sdk/resource/button_parameter.rb
whatsapp_sdk-0.3.0 lib/whatsapp_sdk/resource/button_parameter.rb
whatsapp_sdk-0.2.0 lib/whatsapp_sdk/resource/button_parameter.rb
whatsapp_sdk-0.1.0 lib/whatsapp_sdk/resource/button_parameter.rb