slack-ruby-block-kit 0.4.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 (35) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +49 -0
  3. data/.gitignore +13 -0
  4. data/.rspec +1 -0
  5. data/.rubocop.yml +15 -0
  6. data/.rubocop_todo.yml +38 -0
  7. data/Gemfile +6 -0
  8. data/Gemfile.lock +59 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +95 -0
  11. data/Rakefile +8 -0
  12. data/lib/slack-ruby-block-kit.rb +3 -0
  13. data/lib/slack/block_kit.rb +26 -0
  14. data/lib/slack/block_kit/blocks.rb +71 -0
  15. data/lib/slack/block_kit/composition/confirmation_dialog.rb +53 -0
  16. data/lib/slack/block_kit/composition/mrkdwn.rb +28 -0
  17. data/lib/slack/block_kit/composition/option.rb +25 -0
  18. data/lib/slack/block_kit/composition/option_group.rb +35 -0
  19. data/lib/slack/block_kit/composition/plain_text.rb +27 -0
  20. data/lib/slack/block_kit/element/button.rb +48 -0
  21. data/lib/slack/block_kit/element/channels_select.rb +48 -0
  22. data/lib/slack/block_kit/element/conversations_select.rb +49 -0
  23. data/lib/slack/block_kit/element/date_picker.rb +48 -0
  24. data/lib/slack/block_kit/element/external_select.rb +56 -0
  25. data/lib/slack/block_kit/element/image.rb +29 -0
  26. data/lib/slack/block_kit/element/overflow_menu.rb +58 -0
  27. data/lib/slack/block_kit/element/static_select.rb +81 -0
  28. data/lib/slack/block_kit/element/users_select.rb +48 -0
  29. data/lib/slack/block_kit/layout/actions.rb +138 -0
  30. data/lib/slack/block_kit/layout/context.rb +48 -0
  31. data/lib/slack/block_kit/layout/divider.rb +26 -0
  32. data/lib/slack/block_kit/layout/image.rb +37 -0
  33. data/lib/slack/block_kit/layout/section.rb +173 -0
  34. data/slack-ruby-block-kit.gemspec +37 -0
  35. metadata +161 -0
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slack
4
+ module BlockKit
5
+ module Composition
6
+ # An object containing some text, formatted using Slack's "mrkdwn".
7
+ #
8
+ # https://api.slack.com/reference/messaging/composition-objects#text
9
+ # https://api.slack.com/messaging/composing/formatting
10
+ class Mrkdwn
11
+ TYPE = 'mrkdwn'
12
+
13
+ def initialize(text:, verbatim: nil)
14
+ @text = text
15
+ @verbatim = verbatim
16
+ end
17
+
18
+ def as_json(*)
19
+ {
20
+ type: TYPE,
21
+ text: @text,
22
+ verbatim: @verbatim
23
+ }.compact
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slack
4
+ module BlockKit
5
+ module Composition
6
+ # An object that represents a single selectable item in a select menu.
7
+ #
8
+ # https://api.slack.com/reference/messaging/composition-objects#option
9
+ # https://api.slack.com/reference/messaging/block-elements#select
10
+ class Option
11
+ def initialize(value:, text:, emoji: nil)
12
+ @text = PlainText.new(text: text, emoji: emoji)
13
+ @value = value
14
+ end
15
+
16
+ def as_json(*)
17
+ {
18
+ text: @text.as_json,
19
+ value: @value
20
+ }
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slack
4
+ module BlockKit
5
+ module Composition
6
+ # Provides a way to group options in a select menu.
7
+ #
8
+ # https://api.slack.com/reference/messaging/composition-objects#option-group
9
+ # https://api.slack.com/reference/messaging/block-elements#select
10
+ class OptionGroup
11
+ attr_accessor :options
12
+
13
+ def initialize(label:, emoji: nil)
14
+ @label = PlainText.new(text: label, emoji: emoji)
15
+ @options = []
16
+
17
+ yield(self) if block_given?
18
+ end
19
+
20
+ def option(text:, value:, emoji: nil)
21
+ @options << Option.new(text: text, value: value, emoji: emoji)
22
+
23
+ self
24
+ end
25
+
26
+ def as_json(*)
27
+ {
28
+ label: @label.as_json,
29
+ options: @options.map(&:as_json)
30
+ }
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slack
4
+ module BlockKit
5
+ module Composition
6
+ # An object containing some text, formatted using plain text.
7
+ #
8
+ # https://api.slack.com/reference/messaging/composition-objects#text
9
+ class PlainText
10
+ TYPE = 'plain_text'
11
+
12
+ def initialize(text:, emoji: nil)
13
+ @text = text
14
+ @emoji = emoji
15
+ end
16
+
17
+ def as_json(*)
18
+ {
19
+ type: TYPE,
20
+ text: @text,
21
+ emoji: @emoji
22
+ }.compact
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slack
4
+ module BlockKit
5
+ module Element
6
+ # An interactive element that inserts a button.
7
+ # The button can be a trigger for anything from opening a simple link
8
+ # to starting a complex workflow.
9
+ #
10
+ # https://api.slack.com/reference/messaging/block-elements#button
11
+ class Button
12
+ TYPE = 'button'
13
+
14
+ attr_accessor :confirm
15
+
16
+ def initialize(text:, action_id:, style: nil, emoji: nil, url: nil, value: nil)
17
+ @text = Composition::PlainText.new(text: text, emoji: emoji)
18
+ @action_id = action_id
19
+ @url = url
20
+ @style = style
21
+ @value = value
22
+
23
+ yield(self) if block_given?
24
+ end
25
+
26
+ def confirmation_dialog
27
+ @confirm = Composition::ConfirmationDialog.new
28
+
29
+ yield(@confirm) if block_given?
30
+
31
+ @confirm
32
+ end
33
+
34
+ def as_json(*)
35
+ {
36
+ type: TYPE,
37
+ text: @text.as_json,
38
+ action_id: @action_id,
39
+ url: @url,
40
+ value: @value,
41
+ style: @style,
42
+ confirm: @confirm&.as_json
43
+ }.compact
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slack
4
+ module BlockKit
5
+ module Element
6
+ # A select menu, just as with a standard HTML <select> tag, creates a drop
7
+ # down menu with a list of options for a user to choose. The select menu
8
+ # also includes type-ahead functionality, where a user can type a part or
9
+ # all of an option string to filter the list.
10
+ #
11
+ # This select menu will populate its options with a list of public
12
+ # channels visible to the current user in the active workspace.
13
+ #
14
+ # https://api.slack.com/reference/messaging/block-elements#channel-select
15
+ class ChannelsSelect
16
+ TYPE = 'channels_select'
17
+
18
+ attr_accessor :confirm
19
+
20
+ def initialize(placeholder:, action_id:, initial: nil, emoji: nil)
21
+ @placeholder = Composition::PlainText.new(text: placeholder, emoji: emoji)
22
+ @action_id = action_id
23
+ @initial_channel = initial
24
+
25
+ yield(self) if block_given?
26
+ end
27
+
28
+ def confirmation_dialog
29
+ @confirm = Composition::ConfirmationDialog.new
30
+
31
+ yield(@confirm) if block_given?
32
+
33
+ self
34
+ end
35
+
36
+ def as_json(*)
37
+ {
38
+ type: TYPE,
39
+ placeholder: @placeholder.as_json,
40
+ action_id: @action_id,
41
+ initial_channel: @initial_channel,
42
+ confirm: @confirm&.as_json
43
+ }.compact
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slack
4
+ module BlockKit
5
+ module Element
6
+ # A select menu, just as with a standard HTML <select> tag, creates a drop
7
+ # down menu with a list of options for a user to choose. The select menu
8
+ # also includes type-ahead functionality, where a user can type a part or
9
+ # all of an option string to filter the list.
10
+ #
11
+ # This select menu will populate its options with a list of public and
12
+ # private channels, DMs, and MPIMs visible to the current user in the
13
+ # active workspace.
14
+ #
15
+ # https://api.slack.com/reference/messaging/block-elements#conversation-select
16
+ class ConversationsSelect
17
+ TYPE = 'conversations_select'
18
+
19
+ attr_accessor :confirm
20
+
21
+ def initialize(placeholder:, action_id:, initial: nil, emoji: nil)
22
+ @placeholder = Composition::PlainText.new(text: placeholder, emoji: emoji)
23
+ @action_id = action_id
24
+ @initial_conversation = initial
25
+
26
+ yield(self) if block_given?
27
+ end
28
+
29
+ def confirmation_dialog
30
+ @confirm = Composition::ConfirmationDialog.new
31
+
32
+ yield(@confirm) if block_given?
33
+
34
+ self
35
+ end
36
+
37
+ def as_json(*)
38
+ {
39
+ type: TYPE,
40
+ placeholder: @placeholder.as_json,
41
+ action_id: @action_id,
42
+ initial_conversation: @initial_conversation,
43
+ confirm: @confirm&.as_json
44
+ }.compact
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slack
4
+ module BlockKit
5
+ module Element
6
+ # An element which lets users easily select a date from a calendar style
7
+ # UI.
8
+ #
9
+ # Date picker elements can be used inside of section and actions blocks.
10
+ #
11
+ # https://api.slack.com/reference/messaging/block-elements#datepicker
12
+ class DatePicker
13
+ TYPE = 'datepicker'
14
+
15
+ def initialize(action_id:, placeholder: nil, initial: nil, emoji: nil)
16
+ @action_id = action_id
17
+ @initial_date = initial
18
+ if placeholder
19
+ @placeholder = Composition::PlainText.new(
20
+ text: placeholder,
21
+ emoji: emoji
22
+ )
23
+ end
24
+
25
+ yield(self) if block_given?
26
+ end
27
+
28
+ def confirmation_dialog
29
+ @confirm = Composition::ConfirmationDialog.new
30
+
31
+ yield(@confirm) if block_given?
32
+
33
+ self
34
+ end
35
+
36
+ def as_json(*)
37
+ {
38
+ type: TYPE,
39
+ action_id: @action_id,
40
+ placeholder: @placeholder&.as_json,
41
+ initial_date: @initial_date,
42
+ confirm: @confirm&.as_json
43
+ }.compact
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slack
4
+ module BlockKit
5
+ module Element
6
+ # A select menu, just as with a standard HTML <select> tag, creates a drop
7
+ # down menu with a list of options for a user to choose. The select menu
8
+ # also includes type-ahead functionality, where a user can type a part or
9
+ # all of an option string to filter the list.
10
+ #
11
+ # This select menu will load its options from an external data source,
12
+ # allowing for a dynamic list of options.
13
+ #
14
+ # Each time a select menu of this type is opened or the user starts typing
15
+ # in the typeahead field, we'll send a request to your specified URL. Your
16
+ # app should return an HTTP 200 OK response, along with an
17
+ # application/json post body with an object containing either an options
18
+ # array, or an option_groups array.
19
+ #
20
+ # https://api.slack.com/reference/messaging/block-elements#external-select
21
+ class ExternalSelect
22
+ TYPE = 'external_select'
23
+
24
+ attr_accessor :confirm
25
+
26
+ def initialize(placeholder:, action_id:, initial: nil, min_query_length: nil, emoji: nil)
27
+ @placeholder = Composition::PlainText.new(text: placeholder, emoji: emoji)
28
+ @action_id = action_id
29
+ @initial_option = initial
30
+ @min_query_length = min_query_length
31
+
32
+ yield(self) if block_given?
33
+ end
34
+
35
+ def confirmation_dialog
36
+ @confirm = Composition::ConfirmationDialog.new
37
+
38
+ yield(@confirm) if block_given?
39
+
40
+ self
41
+ end
42
+
43
+ def as_json(*)
44
+ {
45
+ type: TYPE,
46
+ placeholder: @placeholder.as_json,
47
+ action_id: @action_id,
48
+ initial_option: @initial_option&.as_json,
49
+ min_query_length: min_query_length,
50
+ confirm: @confirm&.as_json
51
+ }.compact
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slack
4
+ module BlockKit
5
+ module Element
6
+ # An element to insert an image - this element can be used in section and
7
+ # context blocks only. If you want a block with only an image in it,
8
+ # you're looking for the image block. (Slack::BlockKit::Layout::Image)
9
+ #
10
+ # https://api.slack.com/reference/messaging/block-elements#image
11
+ class Image
12
+ TYPE = 'image'
13
+
14
+ def initialize(image_url:, alt_text:)
15
+ @image_url = image_url
16
+ @alt_text = alt_text
17
+ end
18
+
19
+ def as_json(*)
20
+ {
21
+ type: TYPE,
22
+ image_url: @image_url,
23
+ alt_text: @alt_text
24
+ }
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slack
4
+ module BlockKit
5
+ module Element
6
+ # This is like a cross between a button and a select menu - when a user
7
+ # clicks on this overflow button, they will be presented with a list of
8
+ # options to choose from. Unlike the select menu, there is no typeahead
9
+ # field, and the button always appears with an ellipsis ("…") rather than
10
+ # customisable text.
11
+ #
12
+ # As such, it is usually used if you want a more compact layout than a
13
+ # select menu, or to supply a list of less visually important actions
14
+ # after a row of buttons.
15
+ #
16
+ # https://api.slack.com/reference/messaging/block-elements#overflow
17
+ class OverflowMenu
18
+ TYPE = 'overflow'
19
+
20
+ attr_accessor :options, :confirm
21
+
22
+ def initialize(action_id:)
23
+ @action_id = action_id
24
+ @options = []
25
+
26
+ yield(self) if block_given?
27
+ end
28
+
29
+ def option(value:, text:, emoji: nil)
30
+ @options << Composition::Option.new(
31
+ value: value,
32
+ text: text,
33
+ emoji: emoji
34
+ )
35
+
36
+ self
37
+ end
38
+
39
+ def confirmation_dialog
40
+ @confirm = Composition::ConfirmationDialog.new
41
+
42
+ yield(@confirm) if block_given?
43
+
44
+ self
45
+ end
46
+
47
+ def as_json(*)
48
+ {
49
+ type: TYPE,
50
+ action_id: @action_id,
51
+ options: @options.map(&:as_json),
52
+ confirm: @confirm&.as_json
53
+ }.compact
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end