slack_builder 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +5 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +204 -0
  5. data/lib/slack_builder/attachment.rb +60 -0
  6. data/lib/slack_builder/elements/actions.rb +19 -0
  7. data/lib/slack_builder/elements/base/concerns/has_elements.rb +62 -0
  8. data/lib/slack_builder/elements/base/element.rb +31 -0
  9. data/lib/slack_builder/elements/base/text.rb +20 -0
  10. data/lib/slack_builder/elements/context.rb +29 -0
  11. data/lib/slack_builder/elements/divider.rb +11 -0
  12. data/lib/slack_builder/elements/header.rb +21 -0
  13. data/lib/slack_builder/elements/image.rb +25 -0
  14. data/lib/slack_builder/elements/interactive_elements/button.rb +31 -0
  15. data/lib/slack_builder/elements/rich_text.rb +133 -0
  16. data/lib/slack_builder/elements/rich_text_elements/broadcast.rb +22 -0
  17. data/lib/slack_builder/elements/rich_text_elements/channel.rb +29 -0
  18. data/lib/slack_builder/elements/rich_text_elements/color.rb +22 -0
  19. data/lib/slack_builder/elements/rich_text_elements/concerns/has_highlight_style.rb +16 -0
  20. data/lib/slack_builder/elements/rich_text_elements/concerns/has_style.rb +30 -0
  21. data/lib/slack_builder/elements/rich_text_elements/concerns/has_text_style.rb +16 -0
  22. data/lib/slack_builder/elements/rich_text_elements/date.rb +28 -0
  23. data/lib/slack_builder/elements/rich_text_elements/emoji.rb +22 -0
  24. data/lib/slack_builder/elements/rich_text_elements/link.rb +23 -0
  25. data/lib/slack_builder/elements/rich_text_elements/text.rb +23 -0
  26. data/lib/slack_builder/elements/rich_text_elements/user.rb +29 -0
  27. data/lib/slack_builder/elements/rich_text_elements/usergroup.rb +29 -0
  28. data/lib/slack_builder/elements/section.rb +62 -0
  29. data/lib/slack_builder/elements/text_elements/markdown.rb +22 -0
  30. data/lib/slack_builder/elements/text_elements/plain_text.rb +22 -0
  31. data/lib/slack_builder/elements/video.rb +29 -0
  32. data/lib/slack_builder/message.rb +72 -0
  33. data/lib/slack_builder/version.rb +5 -0
  34. data/lib/slack_builder.rb +15 -0
  35. data/slack_builder.gemspec +40 -0
  36. metadata +85 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d261d55e8623155c0593ad7759d93fac8d721d62dbbbfeb9f4f15769707e5334
4
+ data.tar.gz: 294c96bef43e20972a6cea6475c527c78a92e41792a6183703350657a576aff4
5
+ SHA512:
6
+ metadata.gz: cad910618843bfa8f7b9e76275b6c9ec57216d9e5e67d3d04e038207f7f686e9ee8b4c93758033b4eb5c978b414f449810da62e81f20786e637c5c61183ccbca
7
+ data.tar.gz: 913cc697e863c83ecb5ffb4c5484509dcaea2dd275bb7105ec44d908b95c1076e44868ad5cadea86538754795d7b7021211d91e018792cb9a45e18c8a9930a15
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-08-03
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 cyb-
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,204 @@
1
+ # SlackBuilder
2
+
3
+ ![Version 0.1.0](https://img.shields.io/badge/Version-0.1.0-blue?logo=RubyGems&logoColor=%23E9573F)
4
+ [![CircleCI](https://circleci.com/gh/cyb-/beta-app/tree/main.svg?style=shield&circle-token=a8ff610f989095686162e979db5babf49f34539b)](https://circleci.com/gh/cyb-/slack-builder/?branch=main)
5
+
6
+ This gem aims to help generating [Slack](https://slack.com/) message payloads using [Block-kit API](https://api.slack.com/reference/block-kit/blocks) with kind of comfortable Ruby DSL focused on readability ¯\\_(ツ)_/¯
7
+
8
+ ### Available blocks
9
+
10
+ | | Block type | Available fields |
11
+ |-----|---------------------------------------------------------------------------|---------------------------------------------------|
12
+ | ✅ | [`actions`](https://api.slack.com/reference/block-kit/blocks#actions) | `elements` |
13
+ | ✅ | [`context`](https://api.slack.com/reference/block-kit/blocks#context) | `elements` |
14
+ | ✅ | [`divider`](https://api.slack.com/reference/block-kit/blocks#divider) | *N/A* |
15
+ | ❌ | [`file`](https://api.slack.com/reference/block-kit/blocks#file) | |
16
+ | ✅ | [`header`](https://api.slack.com/reference/block-kit/blocks#header) | `text` |
17
+ | 🏗️ | [`image`](https://api.slack.com/reference/block-kit/blocks#image) | `image_url`, `alt_text`, `title` |
18
+ | ❌ | [`input`](https://api.slack.com/reference/block-kit/blocks#input) | |
19
+ | ✅ | [`rich_text`](https://api.slack.com/reference/block-kit/blocks#rich_text) | `elements` |
20
+ | ✅ | [`section`](https://api.slack.com/reference/block-kit/blocks#section) | `text`, `fields`, `accessory` |
21
+ | 🏗 | [`video`](https://api.slack.com/reference/block-kit/blocks#video) | `video_url`, `thumbnail_url`, `alt_text`, `title` |
22
+
23
+ ### Rich Text blocks
24
+
25
+ | | Block type | Available fields |
26
+ |---|-----------------------------------------------------------------------------------------------------|-----------------------------------------|
27
+ | ✅ | [`rich_text_section`](https://api.slack.com/reference/block-kit/blocks#rich_text_section) | `elements` |
28
+ | ✅ | [`rich_text_preformatted`](https://api.slack.com/reference/block-kit/blocks#rich_text_preformatted) | `elements` |
29
+ | ✅ | [`rich_text_quote`](https://api.slack.com/reference/block-kit/blocks#rich_text_quote) | `elements` |
30
+ | ✅ | [`rich_text_list`](https://api.slack.com/reference/block-kit/blocks#rich_text_list) | `elements`, `style`, `indent`, `offset` |
31
+
32
+ ### Rich Text elements
33
+
34
+ | | Block type | Available fields |
35
+ |---|-----------------------------------------------------------------------------------------|------------------------------------------|
36
+ | ✅ | [`broadcast`](https://api.slack.com/reference/block-kit/blocks#broadcast-element-type) | `range` |
37
+ | ✅ | [`channel`](https://api.slack.com/reference/block-kit/blocks#channel-element-type) | `channel_id`, `style` |
38
+ | ✅ | [`color`](https://api.slack.com/reference/block-kit/blocks#color-element-type) | `value` |
39
+ | ✅ | [`date`](https://api.slack.com/reference/block-kit/blocks#date-element-type) | `timestamp`, `format`, `url`, `fallback` |
40
+ | ✅ | [`emoji`](https://api.slack.com/reference/block-kit/blocks#emoji-element-type) | `name` |
41
+ | ✅ | [`link`](https://api.slack.com/reference/block-kit/blocks#link-element-type) | `text`, `url`, `unsafe`, `style` |
42
+ | ✅ | [`text`](https://api.slack.com/reference/block-kit/blocks#text-element-type) | `text`, `style` |
43
+ | ✅ | [`user`](https://api.slack.com/reference/block-kit/blocks#user-element-type) | `user_id`, `style` |
44
+ | ✅ | [`usergroup`](https://api.slack.com/reference/block-kit/blocks#user-group-element-type) | `usergroup_id`, `style` |
45
+
46
+ ### Interactive elements
47
+
48
+ | | Block type | Available fields |
49
+ |----|-----------------------------------------------------------------------------|------------------------------------------------------------|
50
+ | 🏗 | [`button`](https://api.slack.com/reference/block-kit/block-elements#button) | `text`, `url`, `action_id`, `style`, `accessibility_label` |
51
+
52
+
53
+ ## Installation
54
+
55
+ > [!WARNING]
56
+ > Gem is going to be released on [RubyGems](https://rubygems.org/) soon.
57
+
58
+ Until the first release, you can install it from the Github repository using the `main` branch.
59
+
60
+ ```ruby
61
+ gem 'slack_builder', git: 'https://github.com/cyb-/slack-builder.git', branch: 'main', require: false
62
+ ```
63
+
64
+
65
+ ## Usage
66
+
67
+ Please refer to the **Slack API Block-kit** [documentation](https://api.slack.com/reference/block-kit/blocks) for available blocks and options.
68
+
69
+ ```ruby
70
+ require 'slack_builder'
71
+
72
+ SlackBuilder.message do
73
+ header 'Hello world :wave:'
74
+ divider
75
+ context do
76
+ mrkdwn '*`Hello`* <https://example.com|world> :wave:'
77
+ end
78
+ image 'https://media.giphy.com/media/AcfTF7tyikWyroP0x7/giphy.gif', alt_text: 'Foo', title: 'Blob :ok_hand:', emoji: false
79
+ divider
80
+ section '*`Hello`* <https://example.com|world> :wave:' do
81
+ mrkdwn '*Priority*'
82
+ mrkdwn '*Critical*'
83
+ plain_text 'High :collision:'
84
+ plain_text '¯\\_(ツ)_/¯', emoji: false
85
+ accessory do
86
+ image 'https://media.giphy.com/media/cYNjbM2MvPzM8raKvh/giphy.gif', alt_text: 'Bar'
87
+ end
88
+ end
89
+ video 'https://www.youtube.com/watch?v=PdaAHMztNVE', thumbnail_url: 'https://media.giphy.com/media/cYNjbM2MvPzM8raKvh/giphy.gif', alt_text: 'Foo', title: 'Bar :collision:'
90
+ divider
91
+ rich_text do
92
+ section do
93
+ text 'Hello', bold: true, code: true
94
+ link 'world', url: 'https://example.com'
95
+ emoji :wave
96
+ end
97
+ quote do
98
+ text 'Woopsie'
99
+ broadcast :channel
100
+ color '#666'
101
+ end
102
+ list :bullet do
103
+ section do
104
+ usergroup SLACK_USERGROUP_ID, client_highlight: true, highlight: true, italic: true
105
+ end
106
+ section do
107
+ channel SLACK_CHANNEL_ID, client_highlight: true, bold: true, strike: true
108
+ date 42, format: '{day_divider_pretty}', url: 'https://example.com'
109
+ end
110
+ end
111
+ list :ordered, indent: 1 do
112
+ section do
113
+ text 'Hello'
114
+ end
115
+ section do
116
+ text 'world'
117
+ end
118
+ end
119
+ list :bullet do
120
+ section do
121
+ emoji :wave
122
+ end
123
+ end
124
+ preformatted do
125
+ text 'Hello', italic: true
126
+ user SLACK_USER_ID, unlink: true, highlight: true, bold: true
127
+ end
128
+ end
129
+ divider
130
+ actions do
131
+ button 'Click me :bomb:', url: 'https://github.com/cyb-/slack-builder', style: :danger
132
+ end
133
+ end
134
+ ```
135
+
136
+ You can also optionally add [secondary attachments](https://api.slack.com/reference/messaging/attachments) to your message, with an optional color
137
+
138
+ ```ruby
139
+ require 'slack_builder'
140
+
141
+ SlackBuilder.message do
142
+ header 'Hello world :wave:'
143
+
144
+ attachment color: '#666' do
145
+ rich_text do
146
+ section do
147
+ text 'Woopsie'
148
+ emoji :grimacing
149
+ end
150
+ end
151
+ end
152
+ end
153
+ ```
154
+
155
+ Or with a text
156
+
157
+ ```ruby
158
+ require 'slack_builder'
159
+
160
+ SlackBuilder.message '*`Hello`* <https://example.com|world> :wave:' do
161
+ header 'Hello world :wave:'
162
+
163
+ attachment do
164
+ rich_text do
165
+ section do
166
+ emoji :bomb
167
+ end
168
+ end
169
+ end
170
+ end
171
+ ```
172
+
173
+ ### Sending message
174
+
175
+ > [!NOTE]
176
+ > Gem has no dependencies, it's up to you to send the message however you want ¯\\_(ツ)_/¯
177
+
178
+ #### Example using [Slack client](https://github.com/slack-ruby/slack-ruby-client)
179
+ ```ruby
180
+ require 'slack_builder'
181
+
182
+ payload = SlackBuilder.message do
183
+ # ...
184
+ end
185
+
186
+ client = Slack::Web::Client.new
187
+ client.chat_postMessage(channel: '#somewhere', as_user: true, **payload)
188
+ ```
189
+
190
+ ## Development
191
+
192
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
193
+
194
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
195
+
196
+
197
+ ## Contributing
198
+
199
+ Bug reports and pull requests are welcome on GitHub at https://github.com/cyb-/slack-builder/issues.
200
+
201
+
202
+ ## License
203
+
204
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'elements/base/concerns/has_elements'
4
+ require_relative 'elements/actions'
5
+ require_relative 'elements/divider'
6
+ require_relative 'elements/header'
7
+ require_relative 'elements/image'
8
+ require_relative 'elements/context'
9
+ require_relative 'elements/rich_text'
10
+ require_relative 'elements/section'
11
+ require_relative 'elements/video'
12
+
13
+ module SlackBuilder
14
+ class Attachment
15
+ include Elements::Base::Concerns::HasElements
16
+
17
+ def initialize(color: nil, &blk)
18
+ @color = color.to_s unless color.nil?
19
+ super(&blk)
20
+ end
21
+
22
+ def to_h
23
+ { blocks: to_a }.tap do |h|
24
+ h[:color] = @color if defined?(@color)
25
+ end
26
+ end
27
+
28
+ def actions(...)
29
+ add Elements::Actions.new(...) if block_given?
30
+ end
31
+
32
+ def context(...)
33
+ add Elements::Context.new(...) if block_given?
34
+ end
35
+
36
+ def divider(...)
37
+ add Elements::Divider.new(...)
38
+ end
39
+
40
+ def header(...)
41
+ add Elements::Header.new(...)
42
+ end
43
+
44
+ def image(...)
45
+ add Elements::Image.new(...)
46
+ end
47
+
48
+ def rich_text(...)
49
+ add Elements::RichText.new(...) if block_given?
50
+ end
51
+
52
+ def section(...)
53
+ add Elements::Section.new(...)
54
+ end
55
+
56
+ def video(...)
57
+ add Elements::Video.new(...)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base/element'
4
+ require_relative 'base/concerns/has_elements'
5
+ require_relative 'interactive_elements/button'
6
+
7
+ module SlackBuilder
8
+ module Elements
9
+ class Actions < Base::Element
10
+ include Base::Concerns::HasElements
11
+
12
+ TYPE = 'actions'
13
+
14
+ def button(...)
15
+ add InteractiveElements::Button.new(...)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../element'
4
+
5
+ module SlackBuilder
6
+ module Elements
7
+ module Base
8
+ module Concerns
9
+ module HasElements
10
+ ELEMENTS_KEY_NAME = :elements
11
+
12
+ def initialize(*args, **kwargs, &blk)
13
+ @elements = []
14
+ super(*args, **kwargs)
15
+ append(&blk)
16
+ end
17
+
18
+ def to_a
19
+ @elements.map(&:to_h)
20
+ end
21
+
22
+ def to_ary
23
+ to_a
24
+ end
25
+
26
+ def empty?
27
+ @elements.empty?
28
+ end
29
+
30
+ def any?
31
+ !empty?
32
+ end
33
+
34
+ def to_h
35
+ super.merge(self.class::ELEMENTS_KEY_NAME => to_a)
36
+ end
37
+
38
+ def to_hash
39
+ to_h
40
+ end
41
+
42
+ def as_json(*)
43
+ to_h
44
+ end
45
+
46
+ def append(&blk)
47
+ instance_exec(&blk) if block_given?
48
+ end
49
+
50
+ private
51
+
52
+ def add(element)
53
+ raise ArgumentError, 'Invalid element' unless element.is_a?(Element)
54
+
55
+ @elements << element
56
+ element
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SlackBuilder
4
+ module Elements
5
+ module Base
6
+ class Element
7
+ attr_reader :block_id
8
+
9
+ def initialize(block_id: nil)
10
+ @block_id = block_id unless block_id.nil?
11
+ end
12
+
13
+ def type
14
+ self.class::TYPE
15
+ end
16
+
17
+ def to_h
18
+ { type: type, block_id: block_id }.compact
19
+ end
20
+
21
+ def to_hash
22
+ to_h
23
+ end
24
+
25
+ def as_json(*)
26
+ to_h
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'element'
4
+
5
+ module SlackBuilder
6
+ module Elements
7
+ module Base
8
+ class Text < Element
9
+ def initialize(text, ...)
10
+ @text = text
11
+ super(...)
12
+ end
13
+
14
+ def to_h
15
+ super.merge(text: @text)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base/element'
4
+ require_relative 'base/concerns/has_elements'
5
+ require_relative 'text_elements/markdown'
6
+ require_relative 'text_elements/plain_text'
7
+ require_relative 'image'
8
+
9
+ module SlackBuilder
10
+ module Elements
11
+ class Context < Base::Element
12
+ include Base::Concerns::HasElements
13
+
14
+ TYPE = 'context'
15
+
16
+ def image(...)
17
+ add Image.new(...)
18
+ end
19
+
20
+ def mrkdwn(...)
21
+ add TextElements::Markdown.new(...)
22
+ end
23
+
24
+ def plain_text(...)
25
+ add TextElements::PlainText.new(...)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base/element'
4
+
5
+ module SlackBuilder
6
+ module Elements
7
+ class Divider < Base::Element
8
+ TYPE = 'divider'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base/element'
4
+ require_relative 'text_elements/plain_text'
5
+
6
+ module SlackBuilder
7
+ module Elements
8
+ class Header < Base::Element
9
+ TYPE = 'header'
10
+
11
+ def initialize(text, emoji: true, **kwargs)
12
+ @text = TextElements::PlainText.new(text, emoji: emoji)
13
+ super(**kwargs)
14
+ end
15
+
16
+ def to_h
17
+ super.merge(text: @text.to_h)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base/element'
4
+ require_relative 'text_elements/plain_text'
5
+
6
+ module SlackBuilder
7
+ module Elements
8
+ class Image < Base::Element
9
+ TYPE = 'image'
10
+
11
+ def initialize(image_url, alt_text:, title: nil, emoji: true, **kwargs)
12
+ @image_url = image_url
13
+ @alt_text = alt_text
14
+ @title = TextElements::PlainText.new(title, emoji: emoji) unless title.nil?
15
+ super(**kwargs)
16
+ end
17
+
18
+ def to_h
19
+ super.merge(image_url: @image_url, alt_text: @alt_text).tap do |h|
20
+ h[:title] = @title.to_h if defined?(@title)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../base/element'
4
+ require_relative '../text_elements/plain_text'
5
+
6
+ module SlackBuilder
7
+ module Elements
8
+ module InteractiveElements
9
+ class Button < Base::Element
10
+ TYPE = 'button'
11
+
12
+ def initialize(text, url:, style: nil, emoji: true, action_id: nil, accessibility_label: nil, **kwargs)
13
+ @text = TextElements::PlainText.new(text, emoji: emoji)
14
+ @url = url
15
+ @style = style.to_s unless style.nil?
16
+ @action_id = action_id unless action_id.nil?
17
+ @accessibility_label = accessibility_label unless accessibility_label.nil?
18
+ super(**kwargs)
19
+ end
20
+
21
+ def to_h
22
+ super.merge(text: @text.to_h, url: @url).tap do |h|
23
+ h[:style] = @style if defined?(@style)
24
+ h[:action_id] = @action_id if defined?(@action_id)
25
+ h[:accessibility_label] = @accessibility_label if defined?(@accessibility_label)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base/element'
4
+ require_relative 'base/concerns/has_elements'
5
+ require_relative 'rich_text_elements/broadcast'
6
+ require_relative 'rich_text_elements/channel'
7
+ require_relative 'rich_text_elements/color'
8
+ require_relative 'rich_text_elements/date'
9
+ require_relative 'rich_text_elements/emoji'
10
+ require_relative 'rich_text_elements/link'
11
+ require_relative 'rich_text_elements/text'
12
+ require_relative 'rich_text_elements/user'
13
+ require_relative 'rich_text_elements/usergroup'
14
+
15
+ module SlackBuilder
16
+ module Elements
17
+ class RichText < Base::Element
18
+ include Base::Concerns::HasElements
19
+
20
+ TYPE = 'rich_text'
21
+
22
+ module HasBorder
23
+ def initialize(border: nil, **kwargs, &blk)
24
+ @border = border unless border.nil?
25
+ super(**kwargs, &blk)
26
+ end
27
+
28
+ def to_h
29
+ super.tap do |h|
30
+ h[:border] = @border if defined?(@border)
31
+ end
32
+ end
33
+ end
34
+
35
+ class Section < Base::Element
36
+ include Base::Concerns::HasElements
37
+
38
+ TYPE = 'rich_text_section'
39
+
40
+ def broadcast(...)
41
+ add RichTextElements::Broadcast.new(...)
42
+ end
43
+
44
+ def channel(...)
45
+ add RichTextElements::Channel.new(...)
46
+ end
47
+
48
+ def color(...)
49
+ add RichTextElements::Color.new(...)
50
+ end
51
+
52
+ def date(...)
53
+ add RichTextElements::Date.new(...)
54
+ end
55
+
56
+ def emoji(...)
57
+ add RichTextElements::Emoji.new(...)
58
+ end
59
+
60
+ def link(...)
61
+ add RichTextElements::Link.new(...)
62
+ end
63
+
64
+ def text(...)
65
+ add RichTextElements::Text.new(...)
66
+ end
67
+
68
+ def user(...)
69
+ add RichTextElements::User.new(...)
70
+ end
71
+
72
+ def usergroup(...)
73
+ add RichTextElements::Usergroup.new(...)
74
+ end
75
+ end
76
+
77
+ class Preformatted < Section
78
+ include HasBorder
79
+
80
+ TYPE = 'rich_text_preformatted'
81
+ end
82
+
83
+ class Quote < Section
84
+ include HasBorder
85
+
86
+ TYPE = 'rich_text_quote'
87
+ end
88
+
89
+ class List < Base::Element
90
+ include Base::Concerns::HasElements
91
+ include HasBorder
92
+
93
+ TYPE = 'rich_text_list'
94
+
95
+ def initialize(style, indent: nil, offset: nil, **kwargs, &blk)
96
+ @style = style&.to_s
97
+ @indent = indent unless indent.nil?
98
+ @offset = offset unless offset.nil?
99
+ super(**kwargs, &blk)
100
+ end
101
+
102
+ def to_h
103
+ super.merge(style: @style).tap do |h|
104
+ h[:indent] = @indent if defined?(@indent)
105
+ h[:offset] = @offset if defined?(@offset)
106
+ end
107
+ end
108
+
109
+ def section(...)
110
+ add Section.new(...) if block_given?
111
+ end
112
+ end
113
+
114
+ protected
115
+
116
+ def section(...)
117
+ add Section.new(...) if block_given?
118
+ end
119
+
120
+ def preformatted(...)
121
+ add Preformatted.new(...) if block_given?
122
+ end
123
+
124
+ def quote(...)
125
+ add Quote.new(...) if block_given?
126
+ end
127
+
128
+ def list(...)
129
+ add List.new(...) if block_given?
130
+ end
131
+ end
132
+ end
133
+ end