Sha256: 4b4a50f7de148f4e40125428e621435affb47a3e8d3726b7ec53f8c162ad9bd5

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

module Slack
  module BlockKit
    module CompositionObjects
      class Text
        using Refinements::HashCompact
        attr_reader :type, :text, :emoji, :verbatim

        PLAINTEXT = :plain_text
        MRKDWN = :mrkdwn
        NEWLINE = "\n"

        def self.[](hash)
          new.tap do |object|
            object.type = hash.keys.find { |key| [PLAINTEXT, MRKDWN].include?(key) }
            raise ArgumentError, 'type must be `plain_text` or `mrkdwn`' unless object.type

            object.text = hash[object.type]
            object.emoji! if hash[:emoji]
            object.verbatim! if hash[:verbatim]
          end
        end

        def empty?
          text&.empty?
        end

        def emoji!
          @emoji = true
        end

        def verbatim!
          @verbatim = true
        end

        def type=(type)
          unless [PLAINTEXT, MRKDWN].include?(type.to_sym)
            raise ArgumentError, 'type must be `plain_text` or `mrkdwn`'
          end

          @type = type.to_sym
        end

        def text=(text)
          text = text.join(NEWLINE) if text.is_a?(Array)
          raise TypeError, 'text must be a string' unless text.respond_to?(:to_str)

          @text = text.to_s
        end

        def to_h
          { type: type,
            text: text,
            emoji: emoji,
            verbatim: verbatim }.compact
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slack-block-kit-0.1.0 lib/slack/block_kit/composition_objects/text.rb