Sha256: de0823ba0feaed354ee9e55dc6e603eb510ca420ff21157a9fb12b0215de84c0

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module Slack
  module BlockKit
    class Block
      class ImageBlock < Block
        using Refinements::HashCompact
        attr_accessor :image_url, :alt_text, :title

        def self.populate(hash, object)
          object.image_url = hash.fetch(:image_url)
          object.alt_text = hash.fetch(:alt_text)
          object.title = hash[:title] if hash.key?(:title)
        end

        def title=(obj)
          raise TypeError, 'title must be a Text Object' unless obj.is_a?(CompositionObjects::Text)

          @title = obj
        end

        def image_url=(url)
          raise RangeError, 'image_url is max 3000 characters' unless url.size <= 3000

          @image_url = url
        end

        def alt_text=(text)
          raise RangeError, 'alt_text is max 2000 characters' unless text.size <= 2000

          @alt_text = text
        end

        def to_h
          super.merge(
            image_url: image_url,
            alt_text: alt_text,
            title: title&.to_h
          ).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/block/image_block.rb