block-kit 1.0.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.
- checksums.yaml +7 -0
- data/.rspec +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +183 -0
- data/Rakefile +8 -0
- data/lib/block_kit/base.rb +190 -0
- data/lib/block_kit/blocks.rb +50 -0
- data/lib/block_kit/composition/confirmation_dialog.rb +48 -0
- data/lib/block_kit/composition/conversation_filter.rb +42 -0
- data/lib/block_kit/composition/dispatch_action_config.rb +33 -0
- data/lib/block_kit/composition/input_parameter.rb +19 -0
- data/lib/block_kit/composition/mrkdwn.rb +15 -0
- data/lib/block_kit/composition/option.rb +41 -0
- data/lib/block_kit/composition/option_group.rb +27 -0
- data/lib/block_kit/composition/overflow_option.rb +21 -0
- data/lib/block_kit/composition/plain_text.rb +15 -0
- data/lib/block_kit/composition/slack_file.rb +36 -0
- data/lib/block_kit/composition/text.rb +29 -0
- data/lib/block_kit/composition/trigger.rb +33 -0
- data/lib/block_kit/composition/workflow.rb +19 -0
- data/lib/block_kit/composition.rb +19 -0
- data/lib/block_kit/concerns/confirmable.rb +21 -0
- data/lib/block_kit/concerns/conversation_selection.rb +26 -0
- data/lib/block_kit/concerns/dispatchable.rb +24 -0
- data/lib/block_kit/concerns/dsl_generation.rb +76 -0
- data/lib/block_kit/concerns/external.rb +19 -0
- data/lib/block_kit/concerns/focusable_on_load.rb +17 -0
- data/lib/block_kit/concerns/has_initial_option.rb +23 -0
- data/lib/block_kit/concerns/has_initial_options.rb +23 -0
- data/lib/block_kit/concerns/has_option_groups.rb +37 -0
- data/lib/block_kit/concerns/has_options.rb +28 -0
- data/lib/block_kit/concerns/has_placeholder.rb +21 -0
- data/lib/block_kit/concerns/has_rich_text_elements.rb +83 -0
- data/lib/block_kit/concerns/plain_text_emoji_assignment.rb +39 -0
- data/lib/block_kit/concerns.rb +18 -0
- data/lib/block_kit/elements/base.rb +23 -0
- data/lib/block_kit/elements/base_button.rb +45 -0
- data/lib/block_kit/elements/button.rb +27 -0
- data/lib/block_kit/elements/channels_select.rb +22 -0
- data/lib/block_kit/elements/checkboxes.rb +14 -0
- data/lib/block_kit/elements/conversations_select.rb +24 -0
- data/lib/block_kit/elements/date_picker.rb +21 -0
- data/lib/block_kit/elements/datetime_picker.rb +21 -0
- data/lib/block_kit/elements/email_text_input.rb +24 -0
- data/lib/block_kit/elements/external_select.rb +21 -0
- data/lib/block_kit/elements/file_input.rb +33 -0
- data/lib/block_kit/elements/image.rb +53 -0
- data/lib/block_kit/elements/multi_channels_select.rb +31 -0
- data/lib/block_kit/elements/multi_conversations_select.rb +33 -0
- data/lib/block_kit/elements/multi_external_select.rb +21 -0
- data/lib/block_kit/elements/multi_select.rb +21 -0
- data/lib/block_kit/elements/multi_static_select.rb +12 -0
- data/lib/block_kit/elements/multi_users_select.rb +31 -0
- data/lib/block_kit/elements/number_input.rb +52 -0
- data/lib/block_kit/elements/overflow.rb +23 -0
- data/lib/block_kit/elements/plain_text_input.rb +66 -0
- data/lib/block_kit/elements/radio_buttons.rb +14 -0
- data/lib/block_kit/elements/rich_text_input.rb +26 -0
- data/lib/block_kit/elements/select.rb +18 -0
- data/lib/block_kit/elements/static_select.rb +12 -0
- data/lib/block_kit/elements/time_picker.rb +43 -0
- data/lib/block_kit/elements/url_text_input.rb +24 -0
- data/lib/block_kit/elements/users_select.rb +17 -0
- data/lib/block_kit/elements/workflow_button.rb +18 -0
- data/lib/block_kit/elements.rb +36 -0
- data/lib/block_kit/fixers/associated.rb +27 -0
- data/lib/block_kit/fixers/base.rb +30 -0
- data/lib/block_kit/fixers/null_value.rb +42 -0
- data/lib/block_kit/fixers/truncate.rb +35 -0
- data/lib/block_kit/fixers.rb +11 -0
- data/lib/block_kit/layout/actions.rb +84 -0
- data/lib/block_kit/layout/base.rb +23 -0
- data/lib/block_kit/layout/context.rb +48 -0
- data/lib/block_kit/layout/divider.rb +9 -0
- data/lib/block_kit/layout/file.rb +19 -0
- data/lib/block_kit/layout/header.rb +22 -0
- data/lib/block_kit/layout/image.rb +61 -0
- data/lib/block_kit/layout/input.rb +119 -0
- data/lib/block_kit/layout/markdown.rb +19 -0
- data/lib/block_kit/layout/rich_text/elements/broadcast.rb +22 -0
- data/lib/block_kit/layout/rich_text/elements/channel.rb +21 -0
- data/lib/block_kit/layout/rich_text/elements/color.rb +20 -0
- data/lib/block_kit/layout/rich_text/elements/date.rb +34 -0
- data/lib/block_kit/layout/rich_text/elements/emoji.rb +27 -0
- data/lib/block_kit/layout/rich_text/elements/link.rb +28 -0
- data/lib/block_kit/layout/rich_text/elements/mention_style.rb +27 -0
- data/lib/block_kit/layout/rich_text/elements/text.rb +23 -0
- data/lib/block_kit/layout/rich_text/elements/text_style.rb +23 -0
- data/lib/block_kit/layout/rich_text/elements/user.rb +21 -0
- data/lib/block_kit/layout/rich_text/elements/usergroup.rb +21 -0
- data/lib/block_kit/layout/rich_text/elements.rb +35 -0
- data/lib/block_kit/layout/rich_text/list.rb +41 -0
- data/lib/block_kit/layout/rich_text/preformatted.rb +19 -0
- data/lib/block_kit/layout/rich_text/quote.rb +19 -0
- data/lib/block_kit/layout/rich_text/section.rb +11 -0
- data/lib/block_kit/layout/rich_text.rb +53 -0
- data/lib/block_kit/layout/section.rb +152 -0
- data/lib/block_kit/layout/video.rb +71 -0
- data/lib/block_kit/layout.rb +35 -0
- data/lib/block_kit/surfaces/base.rb +173 -0
- data/lib/block_kit/surfaces/home.rb +40 -0
- data/lib/block_kit/surfaces/message.rb +140 -0
- data/lib/block_kit/surfaces/modal.rb +74 -0
- data/lib/block_kit/surfaces.rb +11 -0
- data/lib/block_kit/typed_array.rb +114 -0
- data/lib/block_kit/typed_set.rb +78 -0
- data/lib/block_kit/types/array.rb +45 -0
- data/lib/block_kit/types/blocks.rb +37 -0
- data/lib/block_kit/types/generic.rb +45 -0
- data/lib/block_kit/types/option.rb +48 -0
- data/lib/block_kit/types/set.rb +35 -0
- data/lib/block_kit/types/text.rb +80 -0
- data/lib/block_kit/types.rb +17 -0
- data/lib/block_kit/validators/array_inclusion_validator.rb +55 -0
- data/lib/block_kit/validators/associated_validator.rb +61 -0
- data/lib/block_kit/validators.rb +8 -0
- data/lib/block_kit/version.rb +5 -0
- data/lib/block_kit.rb +38 -0
- metadata +190 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e870bbe818fc81700e8112f99de5ddad4c27340d6e0cebc89d536f63e2a90123
|
4
|
+
data.tar.gz: 786f5df43117cf99fdcdf5a31886a5618bd5f19a6c360978906c6aa24033d32d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 551382789b7591dd7041236cc4504d8defe7e94b087ed7107eae2f3c77520bb7e92360256e2f9d597bf133c864d47b361b479d28cf153e6b415034b5275df08c
|
7
|
+
data.tar.gz: 854d019c85780c5242915319bf86a583b74c8f6ea0fc3aaa2dae2315255c36fe0ec0c86edcd85cbbcd564b5540a9e5504215ce4ea7126ea33d202cc7a1f0933d
|
data/.rspec
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
+
identity and orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
+
and learning from the experience
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
+
community
|
27
|
+
|
28
|
+
Examples of unacceptable behavior include:
|
29
|
+
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
+
any kind
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
+
* Public or private harassment
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
35
|
+
without their explicit permission
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
+
professional setting
|
38
|
+
|
39
|
+
## Enforcement Responsibilities
|
40
|
+
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
+
or harmful.
|
45
|
+
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
+
decisions when appropriate.
|
50
|
+
|
51
|
+
## Scope
|
52
|
+
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
+
an individual is officially representing the community in public spaces.
|
55
|
+
Examples of representing our community include using an official email address,
|
56
|
+
posting via an official social media account, or acting as an appointed
|
57
|
+
representative at an online or offline event.
|
58
|
+
|
59
|
+
## Enforcement
|
60
|
+
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
+
reported to the community leaders responsible for enforcement at
|
63
|
+
[INSERT CONTACT METHOD].
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
65
|
+
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
67
|
+
reporter of any incident.
|
68
|
+
|
69
|
+
## Enforcement Guidelines
|
70
|
+
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
73
|
+
|
74
|
+
### 1. Correction
|
75
|
+
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
77
|
+
unprofessional or unwelcome in the community.
|
78
|
+
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
82
|
+
|
83
|
+
### 2. Warning
|
84
|
+
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
86
|
+
actions.
|
87
|
+
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
93
|
+
ban.
|
94
|
+
|
95
|
+
### 3. Temporary Ban
|
96
|
+
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
98
|
+
sustained inappropriate behavior.
|
99
|
+
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
101
|
+
communication with the community for a specified period of time. No public or
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
104
|
+
Violating these terms may lead to a permanent ban.
|
105
|
+
|
106
|
+
### 4. Permanent Ban
|
107
|
+
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
111
|
+
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
113
|
+
community.
|
114
|
+
|
115
|
+
## Attribution
|
116
|
+
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
118
|
+
version 2.1, available at
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
120
|
+
|
121
|
+
Community Impact Guidelines were inspired by
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
123
|
+
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
127
|
+
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 David Celis
|
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,183 @@
|
|
1
|
+
# Block Kit
|
2
|
+
|
3
|
+
`block-kit` is a Ruby library for Slack's [Block Kit](https://api.slack.com/block-kit) framework built on ActiveModel, providing a powerful DSL for building surfaces like modals, home tabs, messages, or even just blocks themselves.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
```bash
|
10
|
+
bundle add block-kit
|
11
|
+
```
|
12
|
+
|
13
|
+
Or just install it globally:
|
14
|
+
|
15
|
+
```bash
|
16
|
+
gem install block-kit
|
17
|
+
```
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Blocks can be built individually or as a surface or collection of blocks, which each block able to initialize with a simple Hash or attributes or via a block-based DSL. For example, each of the following declarations would result in the same `section` block:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require "block-kit"
|
25
|
+
|
26
|
+
section = BlockKit::Layout::Section.new(text: BlockKit::Composition::Mrkdwn.new(text: "Hello, world!"))
|
27
|
+
section = BlockKit::Layout::Section.new(text: "Hello, world!")
|
28
|
+
section = BlockKit::Layout::Section.new do |s|
|
29
|
+
# With one of any of the following:
|
30
|
+
s.mrkdwn(text: "Hello, world!")
|
31
|
+
s.text("Hello, world!")
|
32
|
+
s.text = BlockKit::Composition::Mrkdwn.new(text: "Hello, world!")
|
33
|
+
s.text = {type: "mrkdwn", text: "Hello, world!"}
|
34
|
+
s.text = "Hello, world!" # Defaults to `mrkdwn` for section blocks
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
Because each block is built using ActiveModel, attributes are cast to their appropriate types from a variety of inputs. If a block contains another block, you can even assign that nested block as a Hash, which means you should be able to completely reconstruct views or messages from the JSON they serialize to. Additionally, like other ActiveModel classes, values that cannot be cast simply result in `nil`, including when you attempt to do something like add an element to a block that doesn't support it.
|
39
|
+
|
40
|
+
ActiveModel and the extensive DSL provides a powerful and flexible way to build UI in Slack. Here's an end-to-end example of building a message and a modal and sending them to Slack:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
require "slack-ruby-client"
|
44
|
+
require "block-kit"
|
45
|
+
|
46
|
+
footer = BlockKit::Layout::Context.new do |c|
|
47
|
+
c.mrkdwn(text: "_Made with :heart: in Portland, OR_")
|
48
|
+
end
|
49
|
+
|
50
|
+
# Or `BlockKit::Blocks.new do |b|`
|
51
|
+
blocks = BlockKit.blocks do |b|
|
52
|
+
b.header(text: "Hello, :earth_americas:!", emoji: true)
|
53
|
+
|
54
|
+
b.divider
|
55
|
+
|
56
|
+
b.section(block_id: "content") do |s|
|
57
|
+
s.mrkdwn(text: "Welcome to Block Kit!")
|
58
|
+
s.button(text: "Learn more", style: "primary", url: "https://api.slack.com/block-kit")
|
59
|
+
end
|
60
|
+
|
61
|
+
b.append(footer)
|
62
|
+
end
|
63
|
+
|
64
|
+
client = Slack::Web::Client.new
|
65
|
+
client.chat_postMessage(
|
66
|
+
channel: "#general",
|
67
|
+
blocks: blocks.to_json,
|
68
|
+
text: "Hello, world!"
|
69
|
+
)
|
70
|
+
|
71
|
+
modal = BlockKit.modal(blocks: blocks) do |m|
|
72
|
+
m.title(text: "Hello, :earth_americas:!", emoji: true)
|
73
|
+
m.close(text: "Close", emoji: true)
|
74
|
+
end
|
75
|
+
|
76
|
+
client.views_open(
|
77
|
+
trigger_id: "trigger_id",
|
78
|
+
view: modal.to_json
|
79
|
+
)
|
80
|
+
```
|
81
|
+
|
82
|
+
Most DSL methods you call while building blocks will accept optional keyword arguments for the block's attributes and will yield the block itself, allowing you to choose how you want to build your blocks.
|
83
|
+
|
84
|
+
Another benefit of being built on ActiveModel is that all documented limitations of BlockKit are enforced as model validations, with surfaces and collections validating all blocks within them. For example:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
modal = BlockKit.modal do |m|
|
88
|
+
m.title(text: "Hello, world! Welcome to Block Kit!")
|
89
|
+
|
90
|
+
m.section(block_id: "content") do |s|
|
91
|
+
s.mrkdwn(text: "Welcome to Block Kit!")
|
92
|
+
s.button(text: "Learn more", style: "primary", url: "invalid.url")
|
93
|
+
end
|
94
|
+
|
95
|
+
m.append(footer)
|
96
|
+
end
|
97
|
+
|
98
|
+
modal.valid?
|
99
|
+
# => false
|
100
|
+
|
101
|
+
modal.errors.full_messages
|
102
|
+
["Blocks is invalid", "Blocks[0] is invalid: accessory.url is not a valid URI", "Title is too long (maximum is 24 characters)"]
|
103
|
+
```
|
104
|
+
|
105
|
+
This allows you to catch most issues that would result in an `invalid_blocks` error from Slack before you even send the request. Better yet, `block-kit` provides a way to fix any validation error that wouldn't result in changing the behavior of your view. For instance:
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
modal = BlockKit.modal do |m|
|
109
|
+
m.title(text: "Hello, world! Welcome to Block Kit!")
|
110
|
+
|
111
|
+
m.section(block_id: "content") do |s|
|
112
|
+
s.mrkdwn(text: "Welcome to Block Kit!")
|
113
|
+
s.button(text: "Learn more", style: "", url: "https://api.slack.com/block-kit")
|
114
|
+
end
|
115
|
+
|
116
|
+
m.append(footer)
|
117
|
+
end
|
118
|
+
|
119
|
+
modal.valid?
|
120
|
+
# => false
|
121
|
+
|
122
|
+
modal.errors.full_messages
|
123
|
+
# => ["Blocks is invalid", "Blocks[0] is invalid: accessory.style can't be blank", "Title is too long (maximum is 24 characters)"]
|
124
|
+
|
125
|
+
modal.fix_validation_errors
|
126
|
+
# => true # or false if any errors couldn't be fixed
|
127
|
+
|
128
|
+
modal.title
|
129
|
+
# => #<BlockKit::Composition::PlainText text: "Hello, world! Welcome...", emoji: nil>
|
130
|
+
|
131
|
+
modal.blocks.first.accessory
|
132
|
+
# => #<BlockKit::Elements::Button text: #<BlockKit::Composition::PlainText text: "Learn more", emoji: nil>, style: nil, ...>
|
133
|
+
```
|
134
|
+
|
135
|
+
The gem can also be configured to fix validation errors automatically on validation or when rendering as JSON if you don't want to have to remember to call `fix_validation_errors` yourself:
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
BlockKit.configure do |config|
|
139
|
+
# You can set both of these, but `autofix_on_render` is likely enough if you
|
140
|
+
# prefer not to have to call `valid?` at all. Note that `autofix_on_render`
|
141
|
+
# does _not_ mean that only the resulting JSON is fixed; it still fixes the
|
142
|
+
# underlying model's attributes, meaning the model will be mutated.
|
143
|
+
config.autofix_on_validation = true
|
144
|
+
config.autofix_on_render = true
|
145
|
+
|
146
|
+
# Set this to perform autofixes that may change the behavior of your view. This
|
147
|
+
# is useful if you would rather post messages or publish views at the cost of
|
148
|
+
# behavioral quirks or changes rather than suffer from `invalid_blocks` errors.
|
149
|
+
#
|
150
|
+
# If you'd prefer to run dangerous autofixers on demand, you can do this by
|
151
|
+
# calling `fix_validation_errors(dangerous: true)` on your blocks or surfaces.
|
152
|
+
config.autofix_dangerously = true
|
153
|
+
end
|
154
|
+
```
|
155
|
+
|
156
|
+
Autofixers currently include:
|
157
|
+
|
158
|
+
* Truncating long text fields to their maximum length
|
159
|
+
* Setting optional attributes to `nil` if they are blank
|
160
|
+
|
161
|
+
Dangerous autofixers (which can be run by calling `fix_validation_errors(dangerous: true)`) that may change the behavior of your view include:
|
162
|
+
|
163
|
+
* Truncating long _lists_ of blocks or elements to their maximum length (such as surface blocks, options, option groups, actions elements, etc.)
|
164
|
+
* Truncating URLs to their maximum length
|
165
|
+
* Removing section accessories or actions/input elements from surfaces that don't support them
|
166
|
+
|
167
|
+
## Development
|
168
|
+
|
169
|
+
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.
|
170
|
+
|
171
|
+
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).
|
172
|
+
|
173
|
+
## Contributing
|
174
|
+
|
175
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/davidcelis/block-kit. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/davidcelis/block-kit/blob/main/CODE_OF_CONDUCT.md).
|
176
|
+
|
177
|
+
## License
|
178
|
+
|
179
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
180
|
+
|
181
|
+
## Code of Conduct
|
182
|
+
|
183
|
+
Everyone interacting in the Block::Kit project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/davidcelis/block-kit/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,190 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_model"
|
4
|
+
require "json"
|
5
|
+
|
6
|
+
module BlockKit
|
7
|
+
class Base
|
8
|
+
include ActiveModel::Model
|
9
|
+
include ActiveModel::Attributes
|
10
|
+
include ActiveModel::Validations
|
11
|
+
include ActiveModel::Validations::Callbacks
|
12
|
+
|
13
|
+
class_attribute :type, default: nil
|
14
|
+
class_attribute :attribute_fixers, default: Hash.new { |h, k| h[k] = [] }
|
15
|
+
|
16
|
+
before_validation :fix_validation_errors, if: -> { BlockKit.config.autofix_on_validation && !fixing? }
|
17
|
+
|
18
|
+
def self.fixes(attribute, fixers)
|
19
|
+
fixers.each do |name, options|
|
20
|
+
options = {} if !options.is_a?(Hash)
|
21
|
+
fixer_class = BlockKit::Fixers.const_get(name.to_s.camelize)
|
22
|
+
fixer = fixer_class.new(attribute: attribute, **options)
|
23
|
+
|
24
|
+
attribute_fixers[attribute] << fixer
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.fix(method_name, dangerous: false)
|
29
|
+
attribute_fixers[:base] << {name: method_name, dangerous: dangerous}
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.inherited(base)
|
33
|
+
base.attribute_fixers = attribute_fixers.dup
|
34
|
+
end
|
35
|
+
|
36
|
+
def initialize(attributes = {})
|
37
|
+
raise NotImplementedError, "#{self.class} is an abstract class and can't be instantiated." if instance_of?(Base)
|
38
|
+
|
39
|
+
super
|
40
|
+
|
41
|
+
yield(self) if block_given?
|
42
|
+
end
|
43
|
+
|
44
|
+
def fix_validation_errors(dangerous: BlockKit.config.autofix_dangerously)
|
45
|
+
fixing do
|
46
|
+
return true if valid?
|
47
|
+
|
48
|
+
Array(attribute_fixers[:base]).each do |method|
|
49
|
+
method(method[:name]).call unless method[:dangerous] && !dangerous
|
50
|
+
end
|
51
|
+
|
52
|
+
attribute_fixers.except(:base).each do |attribute, fixers|
|
53
|
+
fixers.each { |fixer| fixer.fix(self, fixing_dangerously: dangerous) }
|
54
|
+
end
|
55
|
+
|
56
|
+
validate
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def fix_validation_errors!(dangerous: BlockKit.config.autofix_dangerously)
|
61
|
+
fixing do
|
62
|
+
fix_validation_errors(dangerous: dangerous)
|
63
|
+
|
64
|
+
validate!
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def as_json(*)
|
69
|
+
fix_validation_errors if BlockKit.config.autofix_on_render
|
70
|
+
|
71
|
+
{type: self.class.type.to_s}
|
72
|
+
end
|
73
|
+
|
74
|
+
def to_json(*)
|
75
|
+
as_json.to_json
|
76
|
+
end
|
77
|
+
|
78
|
+
def ==(other)
|
79
|
+
other.instance_of?(self.class) && attributes == other.attributes
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.inspect
|
83
|
+
"#{name}(#{attribute_types.map { |k, v| "#{k}: #{v.type}" }.join(", ")})"
|
84
|
+
end
|
85
|
+
|
86
|
+
def inspect
|
87
|
+
"#<#{self.class} #{attributes.map { |k, v| "#{k}: #{v.inspect}" }.join(", ")}>"
|
88
|
+
end
|
89
|
+
|
90
|
+
def pretty_print(pp)
|
91
|
+
pp.object_group(self) do
|
92
|
+
pp.breakable
|
93
|
+
pp.seplist(attributes) do |k, v|
|
94
|
+
pp.text "#{k}: "
|
95
|
+
pp.pp v
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
private
|
101
|
+
|
102
|
+
# The `fixing` instance variable is set to true when the object is being
|
103
|
+
# autofixed. This is used to prevent infinite loops when the fixers modify
|
104
|
+
# the object and trigger validation again.
|
105
|
+
def fixing
|
106
|
+
previous_value = @fixing
|
107
|
+
|
108
|
+
@fixing = true
|
109
|
+
|
110
|
+
yield
|
111
|
+
ensure
|
112
|
+
@fixing = previous_value
|
113
|
+
end
|
114
|
+
|
115
|
+
def fixing?
|
116
|
+
!!@fixing
|
117
|
+
end
|
118
|
+
|
119
|
+
def self.plain_text_attribute(name)
|
120
|
+
attribute name, Types::Generic.of_type(Composition::PlainText)
|
121
|
+
|
122
|
+
dsl_method(name, required_fields: [:text], yields: false)
|
123
|
+
end
|
124
|
+
private_class_method :plain_text_attribute
|
125
|
+
|
126
|
+
def self.dsl_method(attribute, as: nil, type: nil, required_fields: [], mutually_exclusive_fields: [], yields: true)
|
127
|
+
type ||= attribute_types[attribute.to_s]
|
128
|
+
type = Types::Generic.of_type(type) if type.is_a?(Class) && type < BlockKit::Base
|
129
|
+
raise ArgumentError, "attribute #{attribute} does not exist" if type.instance_of?(ActiveModel::Type::Value)
|
130
|
+
|
131
|
+
is_array_attribute = false
|
132
|
+
|
133
|
+
if type.instance_of?(Types::Array)
|
134
|
+
is_array_attribute = true
|
135
|
+
type = type.item_type
|
136
|
+
end
|
137
|
+
|
138
|
+
is_array_attribute ||= attribute_types[attribute.to_s].type == :array
|
139
|
+
|
140
|
+
fields = type.block_class.attribute_names.map(&:to_sym)
|
141
|
+
plain_text_fields = type.block_class.attribute_types.select { |_, v| v.respond_to?(:block_class) && v.block_class == Composition::PlainText }.keys.map(&:to_sym)
|
142
|
+
|
143
|
+
define_method(as || attribute) do |args = {}, &block|
|
144
|
+
args = args.symbolize_keys
|
145
|
+
|
146
|
+
# Return the existing attribute if no args or block are passed and we're not in a custom-named method
|
147
|
+
return super() if args.blank? && block.nil? && as.nil?
|
148
|
+
|
149
|
+
if (missing_required_fields = (required_fields - args.keys)) && missing_required_fields.any?
|
150
|
+
message = "missing keyword#{"s" if missing_required_fields.size > 1}: #{missing_required_fields.map(&:inspect).join(", ")}"
|
151
|
+
raise ArgumentError, message
|
152
|
+
end
|
153
|
+
|
154
|
+
unknown_fields = (args.keys - fields)
|
155
|
+
unknown_fields.delete(:emoji) if plain_text_fields.any?
|
156
|
+
|
157
|
+
if unknown_fields.any?
|
158
|
+
message = "unknown keyword#{"s" if unknown_fields.size > 1}: #{unknown_fields.map(&:inspect).join(", ")}"
|
159
|
+
raise ArgumentError, message
|
160
|
+
end
|
161
|
+
|
162
|
+
mutually_exclusive = mutually_exclusive_fields & args.keys
|
163
|
+
if mutually_exclusive.length > 1
|
164
|
+
message = "mutually exclusive keywords: #{mutually_exclusive.map(&:inspect).join(", ")}"
|
165
|
+
raise ArgumentError, message
|
166
|
+
end
|
167
|
+
|
168
|
+
new_value = if is_array_attribute
|
169
|
+
public_send(:"#{attribute}=", []) if public_send(attribute).nil?
|
170
|
+
public_send(attribute) << type.cast(args)
|
171
|
+
public_send(attribute).last
|
172
|
+
else
|
173
|
+
public_send(:"#{attribute}=", type.cast(args))
|
174
|
+
public_send(attribute)
|
175
|
+
end
|
176
|
+
|
177
|
+
plain_text_fields.each do |field|
|
178
|
+
next unless args.key?(field)
|
179
|
+
|
180
|
+
new_value.public_send(field).emoji = args[:emoji] if args.key?(:emoji)
|
181
|
+
end
|
182
|
+
|
183
|
+
block&.call(new_value) if yields
|
184
|
+
|
185
|
+
self
|
186
|
+
end
|
187
|
+
end
|
188
|
+
private_class_method :dsl_method
|
189
|
+
end
|
190
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/module/delegation"
|
4
|
+
|
5
|
+
module BlockKit
|
6
|
+
class Blocks < Base
|
7
|
+
attribute :blocks, Types::Array.of(Types::Blocks.new(*Layout.all))
|
8
|
+
validates :blocks, "block_kit/validators/associated": true
|
9
|
+
fixes :blocks, associated: true
|
10
|
+
|
11
|
+
dsl_method :blocks, as: :actions, type: Layout::Actions
|
12
|
+
dsl_method :blocks, as: :context, type: Layout::Context
|
13
|
+
dsl_method :blocks, as: :divider, type: Layout::Divider, yields: false
|
14
|
+
dsl_method :blocks, as: :file, type: Layout::File, required_fields: [:external_id], yields: false
|
15
|
+
dsl_method :blocks, as: :header, type: Layout::Header, required_fields: [:text], yields: false
|
16
|
+
dsl_method :blocks, as: :input, type: Layout::Input
|
17
|
+
dsl_method :blocks, as: :markdown, type: Layout::Markdown, required_fields: [:text], yields: false
|
18
|
+
dsl_method :blocks, as: :rich_text, type: Layout::RichText
|
19
|
+
dsl_method :blocks, as: :section, type: Layout::Section
|
20
|
+
dsl_method :blocks, as: :video, type: Layout::Video, required_fields: [:alt_text, :title, :thumbnail_url, :video_url], yields: false
|
21
|
+
|
22
|
+
delegate_missing_to :blocks
|
23
|
+
|
24
|
+
def initialize(attributes = {})
|
25
|
+
attributes = attributes.with_indifferent_access
|
26
|
+
attributes[:blocks] ||= []
|
27
|
+
|
28
|
+
super
|
29
|
+
end
|
30
|
+
|
31
|
+
def image(alt_text:, image_url: nil, slack_file: nil, title: nil, emoji: nil, block_id: nil)
|
32
|
+
if (image_url.nil? && slack_file.nil?) || (image_url && slack_file)
|
33
|
+
raise ArgumentError, "Must provide either image_url or slack_file, but not both."
|
34
|
+
end
|
35
|
+
|
36
|
+
append(Layout::Image.new(image_url: image_url, slack_file: slack_file, alt_text: alt_text, title: title, block_id: block_id, emoji: emoji))
|
37
|
+
end
|
38
|
+
|
39
|
+
# Overridden to return `self`, allowing chaining.
|
40
|
+
def append(block)
|
41
|
+
blocks << block
|
42
|
+
|
43
|
+
self
|
44
|
+
end
|
45
|
+
|
46
|
+
def as_json(*)
|
47
|
+
blocks&.map(&:as_json)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BlockKit
|
4
|
+
module Composition
|
5
|
+
class ConfirmationDialog < Base
|
6
|
+
self.type = :confirmation_dialog
|
7
|
+
|
8
|
+
MAX_TITLE_TEXT_LENGTH = 100
|
9
|
+
MAX_TEXT_LENGTH = 300
|
10
|
+
MAX_BUTTON_TEXT_LENGTH = 30
|
11
|
+
VALID_STYLES = [
|
12
|
+
PRIMARY = "primary",
|
13
|
+
DANGER = "danger"
|
14
|
+
].freeze
|
15
|
+
|
16
|
+
plain_text_attribute :title
|
17
|
+
plain_text_attribute :text
|
18
|
+
plain_text_attribute :confirm
|
19
|
+
plain_text_attribute :deny
|
20
|
+
attribute :style, :string
|
21
|
+
|
22
|
+
validates :title, presence: true, length: {maximum: MAX_TITLE_TEXT_LENGTH}
|
23
|
+
fixes :title, truncate: {maximum: MAX_TITLE_TEXT_LENGTH}
|
24
|
+
|
25
|
+
validates :text, presence: true, length: {maximum: MAX_TEXT_LENGTH}
|
26
|
+
fixes :text, truncate: {maximum: MAX_TEXT_LENGTH}
|
27
|
+
|
28
|
+
validates :confirm, presence: true, length: {maximum: MAX_BUTTON_TEXT_LENGTH}
|
29
|
+
fixes :confirm, truncate: {maximum: MAX_BUTTON_TEXT_LENGTH}
|
30
|
+
|
31
|
+
validates :deny, presence: true, length: {maximum: MAX_BUTTON_TEXT_LENGTH}
|
32
|
+
fixes :deny, truncate: {maximum: MAX_BUTTON_TEXT_LENGTH}
|
33
|
+
|
34
|
+
validates :style, presence: true, inclusion: {in: VALID_STYLES}, allow_nil: true
|
35
|
+
fixes :style, null_value: {error_types: [:blank, :inclusion]}
|
36
|
+
|
37
|
+
def as_json(*)
|
38
|
+
super().except(:type).merge(
|
39
|
+
title: title&.as_json,
|
40
|
+
text: text&.as_json,
|
41
|
+
confirm: confirm&.as_json,
|
42
|
+
deny: deny&.as_json,
|
43
|
+
style: style
|
44
|
+
).compact
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|