slack-ruby-block-kit 0.8.0 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.deepsource.toml +15 -0
  3. data/.github/workflows/ci.yml +33 -0
  4. data/.gitignore +2 -0
  5. data/.rubocop.yml +23 -1
  6. data/.rubocop_todo.yml +10 -0
  7. data/CHANGELOG.md +93 -0
  8. data/Gemfile +14 -1
  9. data/README.md +3 -1
  10. data/lib/slack/block_kit.rb +46 -2
  11. data/lib/slack/block_kit/blocks.rb +12 -0
  12. data/lib/slack/block_kit/composition/confirmation_dialog.rb +16 -0
  13. data/lib/slack/block_kit/composition/option.rb +6 -1
  14. data/lib/slack/block_kit/composition/option_group.rb +2 -2
  15. data/lib/slack/block_kit/element/button.rb +3 -11
  16. data/lib/slack/block_kit/element/channels_select.rb +3 -11
  17. data/lib/slack/block_kit/element/checkboxes.rb +53 -0
  18. data/lib/slack/block_kit/element/conversations_select.rb +3 -11
  19. data/lib/slack/block_kit/element/date_picker.rb +3 -9
  20. data/lib/slack/block_kit/element/external_select.rb +3 -12
  21. data/lib/slack/block_kit/element/multi_channels_select.rb +5 -14
  22. data/lib/slack/block_kit/element/multi_conversations_select.rb +5 -14
  23. data/lib/slack/block_kit/element/multi_external_select.rb +5 -14
  24. data/lib/slack/block_kit/element/multi_static_select.rb +18 -25
  25. data/lib/slack/block_kit/element/multi_users_select.rb +5 -14
  26. data/lib/slack/block_kit/element/overflow_menu.rb +4 -11
  27. data/lib/slack/block_kit/element/radio_buttons.rb +53 -0
  28. data/lib/slack/block_kit/element/static_select.rb +20 -25
  29. data/lib/slack/block_kit/element/timepicker.rb +49 -0
  30. data/lib/slack/block_kit/element/users_select.rb +3 -11
  31. data/lib/slack/block_kit/layout/header.rb +29 -0
  32. data/lib/slack/block_kit/layout/image.rb +1 -4
  33. data/lib/slack/block_kit/layout/input.rb +198 -3
  34. data/lib/slack/block_kit/layout/section.rb +21 -1
  35. data/lib/slack/block_kit/version.rb +7 -0
  36. data/lib/slack/surfaces/home.rb +35 -0
  37. data/lib/slack/surfaces/message.rb +34 -0
  38. data/lib/slack/surfaces/modal.rb +76 -0
  39. data/slack-ruby-block-kit.gemspec +1 -18
  40. metadata +20 -138
  41. data/.circleci/config.yml +0 -49
  42. data/Gemfile.lock +0 -74
@@ -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
@@ -13,35 +13,26 @@ module Slack
13
13
  #
14
14
  # https://api.slack.com/reference/block-kit/block-elements#users_multi_select
15
15
  class MultiUsersSelect
16
- TYPE = 'multi_users_select'
16
+ include Composition::ConfirmationDialog::Confirmable
17
17
 
18
- attr_accessor :confirm
18
+ TYPE = 'multi_users_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_user = initial
24
- @confirm = nil
23
+ @initial_users = 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_user: @initial_user,
44
- confirm: @confirm&.as_json,
34
+ initial_users: @initial_users,
35
+ confirm: confirm&.as_json,
45
36
  max_selected_items: @max_selected_items
46
37
  }.compact
47
38
  end
@@ -15,14 +15,15 @@ module Slack
15
15
  #
16
16
  # https://api.slack.com/reference/messaging/block-elements#overflow
17
17
  class OverflowMenu
18
+ include Composition::ConfirmationDialog::Confirmable
19
+
18
20
  TYPE = 'overflow'
19
21
 
20
- attr_accessor :options, :confirm
22
+ attr_accessor :options
21
23
 
22
24
  def initialize(action_id:)
23
25
  @action_id = action_id
24
26
  @options = []
25
- @confirm = nil
26
27
 
27
28
  yield(self) if block_given?
28
29
  end
@@ -38,20 +39,12 @@ module Slack
38
39
  self
39
40
  end
40
41
 
41
- def confirmation_dialog
42
- @confirm = Composition::ConfirmationDialog.new
43
-
44
- yield(@confirm) if block_given?
45
-
46
- self
47
- end
48
-
49
42
  def as_json(*)
50
43
  {
51
44
  type: TYPE,
52
45
  action_id: @action_id,
53
46
  options: @options.map(&:as_json),
54
- confirm: @confirm&.as_json
47
+ confirm: confirm&.as_json
55
48
  }.compact
56
49
  end
57
50
  end