slack-ruby-block-kit 0.9.0 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.deepsource.toml +15 -0
  3. data/.github/workflows/ci.yml +33 -0
  4. data/.rubocop.yml +22 -2
  5. data/CHANGELOG.md +98 -0
  6. data/Gemfile +13 -1
  7. data/Gemfile.lock +61 -43
  8. data/README.md +3 -1
  9. data/lib/slack/block_kit.rb +2 -2
  10. data/lib/slack/block_kit/blocks.rb +18 -0
  11. data/lib/slack/block_kit/composition/confirmation_dialog.rb +16 -0
  12. data/lib/slack/block_kit/composition/option.rb +6 -1
  13. data/lib/slack/block_kit/composition/option_group.rb +2 -2
  14. data/lib/slack/block_kit/element/button.rb +3 -11
  15. data/lib/slack/block_kit/element/channels_select.rb +3 -11
  16. data/lib/slack/block_kit/element/checkboxes.rb +53 -0
  17. data/lib/slack/block_kit/element/conversations_select.rb +3 -11
  18. data/lib/slack/block_kit/element/date_picker.rb +3 -9
  19. data/lib/slack/block_kit/element/external_select.rb +3 -12
  20. data/lib/slack/block_kit/element/multi_channels_select.rb +5 -14
  21. data/lib/slack/block_kit/element/multi_conversations_select.rb +5 -14
  22. data/lib/slack/block_kit/element/multi_external_select.rb +5 -14
  23. data/lib/slack/block_kit/element/multi_static_select.rb +18 -25
  24. data/lib/slack/block_kit/element/multi_users_select.rb +5 -14
  25. data/lib/slack/block_kit/element/overflow_menu.rb +4 -11
  26. data/lib/slack/block_kit/element/radio_buttons.rb +53 -0
  27. data/lib/slack/block_kit/element/static_select.rb +20 -25
  28. data/lib/slack/block_kit/element/timepicker.rb +49 -0
  29. data/lib/slack/block_kit/element/users_select.rb +3 -11
  30. data/lib/slack/block_kit/layout/header.rb +29 -0
  31. data/lib/slack/block_kit/layout/input.rb +196 -3
  32. data/lib/slack/block_kit/layout/section.rb +20 -3
  33. data/lib/slack/block_kit/version.rb +7 -0
  34. data/slack-ruby-block-kit.gemspec +1 -18
  35. metadata +17 -138
  36. data/.circleci/config.yml +0 -49
  37. data/.rubocop_todo.yml +0 -33
@@ -17,8 +17,8 @@ module Slack
17
17
  yield(self) if block_given?
18
18
  end
19
19
 
20
- def option(text:, value:, emoji: nil)
21
- @options << Option.new(text: text, value: value, emoji: emoji)
20
+ def option(text:, value:, emoji: nil, initial: false)
21
+ @options << Option.new(text: text, value: value, emoji: emoji, initial: initial)
22
22
 
23
23
  self
24
24
  end
@@ -9,9 +9,9 @@ module Slack
9
9
  #
10
10
  # https://api.slack.com/reference/messaging/block-elements#button
11
11
  class Button
12
- TYPE = 'button'
12
+ include Composition::ConfirmationDialog::Confirmable
13
13
 
14
- attr_accessor :confirm
14
+ TYPE = 'button'
15
15
 
16
16
  def initialize(text:, action_id:, style: nil, emoji: nil, url: nil, value: nil)
17
17
  @text = Composition::PlainText.new(text: text, emoji: emoji)
@@ -23,14 +23,6 @@ module Slack
23
23
  yield(self) if block_given?
24
24
  end
25
25
 
26
- def confirmation_dialog
27
- @confirm = Composition::ConfirmationDialog.new
28
-
29
- yield(@confirm) if block_given?
30
-
31
- @confirm
32
- end
33
-
34
26
  def as_json(*)
35
27
  {
36
28
  type: TYPE,
@@ -39,7 +31,7 @@ module Slack
39
31
  url: @url,
40
32
  value: @value,
41
33
  style: @style,
42
- confirm: @confirm&.as_json
34
+ confirm: confirm&.as_json
43
35
  }.compact
44
36
  end
45
37
  end
@@ -13,9 +13,9 @@ module Slack
13
13
  #
14
14
  # https://api.slack.com/reference/messaging/block-elements#channel-select
15
15
  class ChannelsSelect
16
- TYPE = 'channels_select'
16
+ include Composition::ConfirmationDialog::Confirmable
17
17
 
18
- attr_accessor :confirm
18
+ TYPE = 'channels_select'
19
19
 
20
20
  def initialize(placeholder:, action_id:, initial: nil, emoji: nil)
21
21
  @placeholder = Composition::PlainText.new(text: placeholder, emoji: emoji)
@@ -25,21 +25,13 @@ module Slack
25
25
  yield(self) if block_given?
26
26
  end
27
27
 
28
- def confirmation_dialog
29
- @confirm = Composition::ConfirmationDialog.new
30
-
31
- yield(@confirm) if block_given?
32
-
33
- self
34
- end
35
-
36
28
  def as_json(*)
37
29
  {
38
30
  type: TYPE,
39
31
  placeholder: @placeholder.as_json,
40
32
  action_id: @action_id,
41
33
  initial_channel: @initial_channel,
42
- confirm: @confirm&.as_json
34
+ confirm: confirm&.as_json
43
35
  }.compact
44
36
  end
45
37
  end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slack
4
+ module BlockKit
5
+ module Element
6
+ # A checkbox group that allows a user to choose multiple items from
7
+ # a list of possible options.
8
+ #
9
+ # https://api.slack.com/reference/messaging/block-elements#checkboxes
10
+ class Checkboxes
11
+ include Composition::ConfirmationDialog::Confirmable
12
+
13
+ TYPE = 'checkboxes'
14
+
15
+ def initialize(action_id:)
16
+ @action_id = action_id
17
+ @options = []
18
+
19
+ yield(self) if block_given?
20
+ end
21
+
22
+ def option(value:, text:, initial: false, description: nil)
23
+ @options << Composition::Option.new(
24
+ value: value,
25
+ text: text,
26
+ description: description,
27
+ initial: initial
28
+ )
29
+
30
+ self
31
+ end
32
+
33
+ def as_json(*)
34
+ {
35
+ type: TYPE,
36
+ action_id: @action_id,
37
+ options: @options.map(&:as_json),
38
+ initial_options: initial_options&.map(&:as_json),
39
+ confirm: confirm&.as_json
40
+ }.compact
41
+ end
42
+
43
+ private
44
+
45
+ def initial_options
46
+ initial = @options.select(&:initial?)
47
+
48
+ initial.empty? ? nil : initial
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -14,9 +14,9 @@ module Slack
14
14
  #
15
15
  # https://api.slack.com/reference/messaging/block-elements#conversation-select
16
16
  class ConversationsSelect
17
- TYPE = 'conversations_select'
17
+ include Composition::ConfirmationDialog::Confirmable
18
18
 
19
- attr_accessor :confirm
19
+ TYPE = 'conversations_select'
20
20
 
21
21
  def initialize(placeholder:, action_id:, initial: nil, emoji: nil)
22
22
  @placeholder = Composition::PlainText.new(text: placeholder, emoji: emoji)
@@ -27,14 +27,6 @@ module Slack
27
27
  yield(self) if block_given?
28
28
  end
29
29
 
30
- def confirmation_dialog
31
- @confirm = Composition::ConfirmationDialog.new
32
-
33
- yield(@confirm) if block_given?
34
-
35
- self
36
- end
37
-
38
30
  def filter(only: nil,
39
31
  exclude_external_shared_channels: nil,
40
32
  exclude_bot_users: nil)
@@ -53,7 +45,7 @@ module Slack
53
45
  placeholder: @placeholder.as_json,
54
46
  action_id: @action_id,
55
47
  initial_conversation: @initial_conversation,
56
- confirm: @confirm&.as_json,
48
+ confirm: confirm&.as_json,
57
49
  filter: @filter&.as_json
58
50
  }.compact
59
51
  end
@@ -10,6 +10,8 @@ module Slack
10
10
  #
11
11
  # https://api.slack.com/reference/messaging/block-elements#datepicker
12
12
  class DatePicker
13
+ include Composition::ConfirmationDialog::Confirmable
14
+
13
15
  TYPE = 'datepicker'
14
16
 
15
17
  def initialize(action_id:, placeholder: nil, initial: nil, emoji: nil)
@@ -25,21 +27,13 @@ module Slack
25
27
  yield(self) if block_given?
26
28
  end
27
29
 
28
- def confirmation_dialog
29
- @confirm = Composition::ConfirmationDialog.new
30
-
31
- yield(@confirm) if block_given?
32
-
33
- self
34
- end
35
-
36
30
  def as_json(*)
37
31
  {
38
32
  type: TYPE,
39
33
  action_id: @action_id,
40
34
  placeholder: @placeholder&.as_json,
41
35
  initial_date: @initial_date,
42
- confirm: @confirm&.as_json
36
+ confirm: confirm&.as_json
43
37
  }.compact
44
38
  end
45
39
  end
@@ -19,28 +19,19 @@ module Slack
19
19
  #
20
20
  # https://api.slack.com/reference/messaging/block-elements#external-select
21
21
  class ExternalSelect
22
- TYPE = 'external_select'
22
+ include Composition::ConfirmationDialog::Confirmable
23
23
 
24
- attr_accessor :confirm
24
+ TYPE = 'external_select'
25
25
 
26
26
  def initialize(placeholder:, action_id:, initial: nil, min_query_length: nil, emoji: nil)
27
27
  @placeholder = Composition::PlainText.new(text: placeholder, emoji: emoji)
28
28
  @action_id = action_id
29
29
  @initial_option = initial
30
30
  @min_query_length = min_query_length
31
- @confirm = nil
32
31
 
33
32
  yield(self) if block_given?
34
33
  end
35
34
 
36
- def confirmation_dialog
37
- @confirm = Composition::ConfirmationDialog.new
38
-
39
- yield(@confirm) if block_given?
40
-
41
- self
42
- end
43
-
44
35
  def as_json(*)
45
36
  {
46
37
  type: TYPE,
@@ -48,7 +39,7 @@ module Slack
48
39
  action_id: @action_id,
49
40
  initial_option: @initial_option&.as_json,
50
41
  min_query_length: @min_query_length,
51
- confirm: @confirm&.as_json
42
+ confirm: confirm&.as_json
52
43
  }.compact
53
44
  end
54
45
  end
@@ -13,35 +13,26 @@ module Slack
13
13
  #
14
14
  # https://api.slack.com/reference/block-kit/block-elements#channel_multi_select
15
15
  class MultiChannelsSelect
16
- TYPE = 'multi_channels_select'
16
+ include Composition::ConfirmationDialog::Confirmable
17
17
 
18
- attr_accessor :confirm
18
+ TYPE = 'multi_channels_select'
19
19
 
20
20
  def initialize(placeholder:, action_id:, initial: nil, emoji: nil, max_selected_items: nil)
21
21
  @placeholder = Composition::PlainText.new(text: placeholder, emoji: emoji)
22
22
  @action_id = action_id
23
- @initial_channel = initial
24
- @confirm = nil
23
+ @initial_channels = initial
25
24
  @max_selected_items = max_selected_items
26
25
 
27
26
  yield(self) if block_given?
28
27
  end
29
28
 
30
- def confirmation_dialog
31
- @confirm = Composition::ConfirmationDialog.new
32
-
33
- yield(@confirm) if block_given?
34
-
35
- self
36
- end
37
-
38
29
  def as_json(*)
39
30
  {
40
31
  type: TYPE,
41
32
  placeholder: @placeholder.as_json,
42
33
  action_id: @action_id,
43
- initial_channel: @initial_channel,
44
- confirm: @confirm&.as_json,
34
+ initial_channels: @initial_channels,
35
+ confirm: confirm&.as_json,
45
36
  max_selected_items: @max_selected_items
46
37
  }.compact
47
38
  end
@@ -14,29 +14,20 @@ module Slack
14
14
  #
15
15
  # https://api.slack.com/reference/block-kit/block-elements#conversation_multi_select
16
16
  class MultiConversationsSelect
17
- TYPE = 'multi_conversations_select'
17
+ include Composition::ConfirmationDialog::Confirmable
18
18
 
19
- attr_accessor :confirm
19
+ TYPE = 'multi_conversations_select'
20
20
 
21
21
  def initialize(placeholder:, action_id:, initial: nil, emoji: nil, max_selected_items: nil)
22
22
  @placeholder = Composition::PlainText.new(text: placeholder, emoji: emoji)
23
23
  @action_id = action_id
24
- @initial_conversation = initial
25
- @confirm = nil
24
+ @initial_conversations = initial
26
25
  @max_selected_items = max_selected_items
27
26
  @filter = nil
28
27
 
29
28
  yield(self) if block_given?
30
29
  end
31
30
 
32
- def confirmation_dialog
33
- @confirm = Composition::ConfirmationDialog.new
34
-
35
- yield(@confirm) if block_given?
36
-
37
- self
38
- end
39
-
40
31
  def filter(only: nil,
41
32
  exclude_external_shared_channels: nil,
42
33
  exclude_bot_users: nil)
@@ -54,8 +45,8 @@ module Slack
54
45
  type: TYPE,
55
46
  placeholder: @placeholder.as_json,
56
47
  action_id: @action_id,
57
- initial_conversation: @initial_conversation,
58
- confirm: @confirm&.as_json,
48
+ initial_conversations: @initial_conversations,
49
+ confirm: confirm&.as_json,
59
50
  max_selected_items: @max_selected_items,
60
51
  filter: @filter&.as_json
61
52
  }.compact
@@ -19,39 +19,30 @@ module Slack
19
19
  #
20
20
  # https://api.slack.com/reference/block-kit/block-elements#external_multi_select
21
21
  class MultiExternalSelect
22
- TYPE = 'multi_external_select'
22
+ include Composition::ConfirmationDialog::Confirmable
23
23
 
24
- attr_accessor :confirm
24
+ TYPE = 'multi_external_select'
25
25
 
26
26
  def initialize(placeholder:, action_id:,
27
27
  initial: nil, min_query_length: nil, emoji: nil, max_selected_items: nil)
28
28
 
29
29
  @placeholder = Composition::PlainText.new(text: placeholder, emoji: emoji)
30
30
  @action_id = action_id
31
- @initial_option = initial
31
+ @initial_options = initial
32
32
  @min_query_length = min_query_length
33
- @confirm = nil
34
33
  @max_selected_items = max_selected_items
35
34
 
36
35
  yield(self) if block_given?
37
36
  end
38
37
 
39
- def confirmation_dialog
40
- @confirm = Composition::ConfirmationDialog.new
41
-
42
- yield(@confirm) if block_given?
43
-
44
- self
45
- end
46
-
47
38
  def as_json(*)
48
39
  {
49
40
  type: TYPE,
50
41
  placeholder: @placeholder.as_json,
51
42
  action_id: @action_id,
52
- initial_option: @initial_option&.as_json,
43
+ initial_options: @initial_options&.map(&:as_json),
53
44
  min_query_length: @min_query_length,
54
- confirm: @confirm&.as_json,
45
+ confirm: confirm&.as_json,
55
46
  max_selected_items: @max_selected_items
56
47
  }.compact
57
48
  end
@@ -13,37 +13,30 @@ module Slack
13
13
  #
14
14
  # https://api.slack.com/reference/block-kit/block-elements#static_multi_select
15
15
  class MultiStaticSelect
16
+ include Composition::ConfirmationDialog::Confirmable
17
+
16
18
  TYPE = 'multi_static_select'
17
19
 
18
- attr_accessor :confirm, :options, :option_groups, :initial_option
20
+ attr_accessor :options, :option_groups
19
21
 
20
22
  def initialize(placeholder:, action_id:, emoji: nil, max_selected_items: nil)
21
23
  @placeholder = Composition::PlainText.new(text: placeholder, emoji: emoji)
22
24
  @action_id = action_id
23
- @confirm = nil
24
25
  @max_selected_items = max_selected_items
25
26
 
26
27
  @options = nil
27
28
  @option_groups = nil
28
- @initial_option = nil
29
29
 
30
30
  yield(self) if block_given?
31
31
  end
32
32
 
33
- def confirmation_dialog
34
- @confirm = Composition::ConfirmationDialog.new
35
-
36
- yield(@confirm) if block_given?
37
-
38
- self
39
- end
40
-
41
- def option(value:, text:, emoji: nil)
33
+ def option(value:, text:, initial: false, emoji: nil)
42
34
  @options ||= []
43
35
  @options << Composition::Option.new(
44
36
  value: value,
45
37
  text: text,
46
- emoji: emoji
38
+ emoji: emoji,
39
+ initial: initial
47
40
  )
48
41
 
49
42
  self
@@ -60,16 +53,6 @@ module Slack
60
53
  self
61
54
  end
62
55
 
63
- def initial(value:, text:, emoji: nil)
64
- @initial_option = Composition::Option.new(
65
- value: value,
66
- text: text,
67
- emoji: emoji
68
- )
69
-
70
- self
71
- end
72
-
73
56
  def as_json(*)
74
57
  {
75
58
  type: TYPE,
@@ -77,11 +60,21 @@ module Slack
77
60
  action_id: @action_id,
78
61
  options: @options&.map(&:as_json),
79
62
  option_groups: @option_groups&.map(&:as_json),
80
- initial_option: @initial_option&.as_json,
81
- confirm: @confirm&.as_json,
63
+ initial_options: initial_options&.map(&:as_json),
64
+ confirm: confirm&.as_json,
82
65
  max_selected_items: @max_selected_items
83
66
  }.compact
84
67
  end
68
+
69
+ private
70
+
71
+ def initial_options
72
+ all_options = options || option_groups&.flat_map(&:options)
73
+
74
+ initial = all_options&.select(&:initial?)
75
+
76
+ initial&.empty? ? nil : initial
77
+ end
85
78
  end
86
79
  end
87
80
  end