slack-ruby-block-kit 0.11.0 → 0.15.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.
@@ -8,11 +8,16 @@ module Slack
8
8
  # https://api.slack.com/reference/messaging/composition-objects#option
9
9
  # https://api.slack.com/reference/messaging/block-elements#select
10
10
  class Option
11
- def initialize(value:, text:, emoji: nil, description: nil, url: nil)
11
+ def initialize(value:, text:, initial: false, emoji: nil, description: nil, url: nil)
12
12
  @text = PlainText.new(text: text, emoji: emoji)
13
13
  @value = value
14
14
  @description = description && PlainText.new(text: description, emoji: emoji)
15
15
  @url = url
16
+ @initial = initial
17
+ end
18
+
19
+ def initial?
20
+ !!@initial
16
21
  end
17
22
 
18
23
  def as_json(*)
@@ -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
@@ -15,26 +15,16 @@ module Slack
15
15
  def initialize(action_id:)
16
16
  @action_id = action_id
17
17
  @options = []
18
- @initial_options = []
19
18
 
20
19
  yield(self) if block_given?
21
20
  end
22
21
 
23
- def option(value:, text:, description: nil)
22
+ def option(value:, text:, initial: false, description: nil)
24
23
  @options << Composition::Option.new(
25
24
  value: value,
26
25
  text: text,
27
- description: description
28
- )
29
-
30
- self
31
- end
32
-
33
- def initial(value:, text:, description: nil)
34
- @initial_options << Composition::Option.new(
35
- value: value,
36
- text: text,
37
- description: description
26
+ description: description,
27
+ initial: initial
38
28
  )
39
29
 
40
30
  self
@@ -45,10 +35,18 @@ module Slack
45
35
  type: TYPE,
46
36
  action_id: @action_id,
47
37
  options: @options.map(&:as_json),
48
- initial_options: @initial_options.any? ? @initial_options.map(&:as_json) : nil,
38
+ initial_options: initial_options&.map(&:as_json),
49
39
  confirm: confirm&.as_json
50
40
  }.compact
51
41
  end
42
+
43
+ private
44
+
45
+ def initial_options
46
+ initial = @options.select(&:initial?)
47
+
48
+ initial.empty? ? nil : initial
49
+ end
52
50
  end
53
51
  end
54
52
  end
@@ -17,12 +17,7 @@ module Slack
17
17
  def initialize(action_id:, placeholder: nil, initial: nil, emoji: nil)
18
18
  @action_id = action_id
19
19
  @initial_date = initial
20
- if placeholder
21
- @placeholder = Composition::PlainText.new(
22
- text: placeholder,
23
- emoji: emoji
24
- )
25
- end
20
+ @placeholder = placeholder_text(placeholder, emoji)
26
21
 
27
22
  yield(self) if block_given?
28
23
  end
@@ -36,6 +31,14 @@ module Slack
36
31
  confirm: confirm&.as_json
37
32
  }.compact
38
33
  end
34
+
35
+ private
36
+
37
+ def placeholder_text(text, emoji)
38
+ return unless text
39
+
40
+ Composition::PlainText.new(text: text, emoji: emoji)
41
+ end
39
42
  end
40
43
  end
41
44
  end
@@ -17,7 +17,7 @@ module Slack
17
17
 
18
18
  TYPE = 'multi_static_select'
19
19
 
20
- attr_accessor :options, :option_groups, :initial_options
20
+ attr_accessor :options, :option_groups
21
21
 
22
22
  def initialize(placeholder:, action_id:, emoji: nil, max_selected_items: nil)
23
23
  @placeholder = Composition::PlainText.new(text: placeholder, emoji: emoji)
@@ -26,17 +26,17 @@ module Slack
26
26
 
27
27
  @options = nil
28
28
  @option_groups = nil
29
- @initial_options = nil
30
29
 
31
30
  yield(self) if block_given?
32
31
  end
33
32
 
34
- def option(value:, text:, emoji: nil)
33
+ def option(value:, text:, initial: false, emoji: nil)
35
34
  @options ||= []
36
35
  @options << Composition::Option.new(
37
36
  value: value,
38
37
  text: text,
39
- emoji: emoji
38
+ emoji: emoji,
39
+ initial: initial
40
40
  )
41
41
 
42
42
  self
@@ -53,17 +53,6 @@ module Slack
53
53
  self
54
54
  end
55
55
 
56
- def initial(value:, text:, emoji: nil)
57
- @initial_options ||= []
58
- @initial_options << Composition::Option.new(
59
- value: value,
60
- text: text,
61
- emoji: emoji
62
- )
63
-
64
- self
65
- end
66
-
67
56
  def as_json(*)
68
57
  {
69
58
  type: TYPE,
@@ -71,11 +60,21 @@ module Slack
71
60
  action_id: @action_id,
72
61
  options: @options&.map(&:as_json),
73
62
  option_groups: @option_groups&.map(&:as_json),
74
- initial_options: @initial_options&.map(&:as_json),
63
+ initial_options: initial_options&.map(&:as_json),
75
64
  confirm: confirm&.as_json,
76
65
  max_selected_items: @max_selected_items
77
66
  }.compact
78
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
79
78
  end
80
79
  end
81
80
  end
@@ -28,6 +28,15 @@ module Slack
28
28
  @multiline = multiline
29
29
  @min_length = min_length
30
30
  @max_length = max_length
31
+ @dispatch_action_config = nil
32
+ end
33
+
34
+ def dispatch_action_config(triggers: nil)
35
+ @dispatch_action_config = Composition::DispatchActionConfiguration.new(triggers: triggers)
36
+
37
+ yield(@dispatch_action_config) if block_given?
38
+
39
+ self
31
40
  end
32
41
 
33
42
  def as_json(*)
@@ -38,7 +47,8 @@ module Slack
38
47
  multiline: @multiline,
39
48
  min_length: @min_length,
40
49
  max_length: @max_length,
41
- initial_value: @initial_value
50
+ initial_value: @initial_value,
51
+ dispatch_action_config: @dispatch_action_config&.as_json
42
52
  }.compact
43
53
  end
44
54
  end
@@ -11,10 +11,11 @@ module Slack
11
11
 
12
12
  TYPE = 'radio_buttons'
13
13
 
14
- attr_accessor :options, :initial_option
14
+ attr_accessor :options
15
15
 
16
16
  def initialize(action_id:)
17
17
  @action_id = action_id
18
+ @options = []
18
19
 
19
20
  yield(self) if block_given?
20
21
  end
@@ -22,14 +23,12 @@ module Slack
22
23
  def option(value:, text:, initial: false)
23
24
  option = Composition::Option.new(
24
25
  value: value,
25
- text: text
26
+ text: text,
27
+ initial: initial
26
28
  )
27
29
 
28
- @options ||= []
29
30
  @options << option
30
31
 
31
- @initial_option = option if initial
32
-
33
32
  self
34
33
  end
35
34
 
@@ -37,11 +36,17 @@ module Slack
37
36
  {
38
37
  type: TYPE,
39
38
  action_id: @action_id,
40
- options: @options&.map(&:as_json),
41
- initial_option: @initial_option&.as_json,
39
+ options: @options.map(&:as_json),
40
+ initial_option: initial_option&.as_json,
42
41
  confirm: confirm&.as_json
43
42
  }.compact
44
43
  end
44
+
45
+ private
46
+
47
+ def initial_option
48
+ @options&.find(&:initial?)
49
+ end
45
50
  end
46
51
  end
47
52
  end
@@ -17,7 +17,7 @@ module Slack
17
17
 
18
18
  TYPE = 'static_select'
19
19
 
20
- attr_accessor :options, :option_groups, :initial_option
20
+ attr_accessor :options, :option_groups
21
21
 
22
22
  def initialize(placeholder:, action_id:, emoji: nil)
23
23
  @placeholder = Composition::PlainText.new(text: placeholder, emoji: emoji)
@@ -26,12 +26,13 @@ module Slack
26
26
  yield(self) if block_given?
27
27
  end
28
28
 
29
- def option(value:, text:, emoji: nil)
29
+ def option(value:, text:, initial: false, emoji: nil)
30
30
  @options ||= []
31
31
  @options << Composition::Option.new(
32
32
  value: value,
33
33
  text: text,
34
- emoji: emoji
34
+ emoji: emoji,
35
+ initial: initial
35
36
  )
36
37
 
37
38
  self
@@ -48,27 +49,27 @@ module Slack
48
49
  self
49
50
  end
50
51
 
51
- def initial(value:, text:, emoji: nil)
52
- @initial_option = Composition::Option.new(
53
- value: value,
54
- text: text,
55
- emoji: emoji
56
- )
57
-
58
- self
59
- end
60
-
61
52
  def as_json(*)
62
53
  {
63
54
  type: TYPE,
64
55
  placeholder: @placeholder.as_json,
65
56
  action_id: @action_id,
66
- options: @options&.map(&:as_json),
67
- option_groups: @option_groups&.map(&:as_json),
68
- initial_option: @initial_option&.as_json,
57
+ options: options&.map(&:as_json),
58
+ option_groups: option_groups&.map(&:as_json),
59
+ initial_option: initial_option&.as_json,
69
60
  confirm: confirm&.as_json
70
61
  }.compact
71
62
  end
63
+
64
+ private
65
+
66
+ def initial_option
67
+ opts = options || option_groups&.flat_map(&:options)
68
+
69
+ return unless opts
70
+
71
+ opts.find(&:initial?)
72
+ end
72
73
  end
73
74
  end
74
75
  end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slack
4
+ module BlockKit
5
+ module Element
6
+ # An element which allows selection of a time of day.
7
+ #
8
+ # On desktop clients, this time picker will take the form of a dropdown
9
+ # list with free-text entry for precise choices. On mobile clients, the
10
+ # time picker will use native time picker UIs.
11
+ #
12
+ # https://api.slack.com/reference/block-kit/block-elements#timepicker
13
+ class Timepicker
14
+ include Composition::ConfirmationDialog::Confirmable
15
+
16
+ TYPE = 'timepicker'
17
+
18
+ def initialize(action_id:)
19
+ @placeholder, @initial_time = nil
20
+ @action_id = action_id
21
+
22
+ yield(self) if block_given?
23
+ end
24
+
25
+ def placeholder(text:, emoji: nil)
26
+ @placeholder = Composition::PlainText.new(text: text, emoji: emoji)
27
+
28
+ self
29
+ end
30
+
31
+ def initial_time(time_str)
32
+ @initial_time = time_str
33
+
34
+ self
35
+ end
36
+
37
+ def as_json(*)
38
+ {
39
+ type: TYPE,
40
+ action_id: @action_id,
41
+ placeholder: @placeholder&.as_json,
42
+ initial_time: @initial_time,
43
+ confirm: confirm&.as_json
44
+ }.compact
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slack
4
+ module BlockKit
5
+ module Layout
6
+ # A header is a plain-text block that displays in a larger, bold font.
7
+ # Use it to delineate between different groups of content in your app's
8
+ # surfaces.
9
+ #
10
+ # https://api.slack.com/reference/block-kit/blocks#header
11
+ class Header
12
+ TYPE = 'header'
13
+
14
+ def initialize(text:, block_id: nil, emoji: nil)
15
+ @text = Composition::PlainText.new(text: text, emoji: emoji)
16
+ @block_id = block_id
17
+ end
18
+
19
+ def as_json(*)
20
+ {
21
+ type: TYPE,
22
+ text: @text.as_json,
23
+ block_id: @block_id
24
+ }.compact
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -4,8 +4,8 @@ module Slack
4
4
  module BlockKit
5
5
  module Layout
6
6
  # A block that collects information from users - it can hold a plain-text
7
- # input element, a select menu element, a multi-select menu element, or a
8
- # datepicker.
7
+ # input element, a checkbox element, a radio button element,
8
+ # a select menu element, a multi-select menu element, or a datepicker.
9
9
  #
10
10
  # https://api.slack.com/reference/block-kit/blocks#input
11
11
  class Input
@@ -15,17 +15,212 @@ module Slack
15
15
 
16
16
  def initialize(
17
17
  label:,
18
- element:,
18
+ element: nil,
19
19
  block_id: nil,
20
20
  hint: nil,
21
21
  optional: nil,
22
- emoji: nil
22
+ emoji: nil,
23
+ dispatch_action: nil
23
24
  )
24
25
  @label = Composition::PlainText.new(text: label, emoji: emoji) if label
25
26
  @hint = Composition::PlainText.new(text: hint, emoji: emoji) if hint
26
27
  @block_id = block_id
27
28
  @optional = optional
28
29
  @element = element
30
+ @dispatch_action = dispatch_action
31
+ end
32
+
33
+ def conversation_select(placeholder:, action_id:, initial: nil, emoji: nil)
34
+ @element = Element::ConversationsSelect.new(
35
+ placeholder: placeholder,
36
+ action_id: action_id,
37
+ initial: initial,
38
+ emoji: emoji
39
+ )
40
+
41
+ yield(@element) if block_given?
42
+
43
+ self
44
+ end
45
+
46
+ def multi_conversations_select(placeholder:, action_id:, initial: nil, emoji: nil, max_selected_items: nil)
47
+ @element = Element::MultiConversationsSelect.new(
48
+ placeholder: placeholder,
49
+ action_id: action_id,
50
+ initial: initial,
51
+ emoji: emoji,
52
+ max_selected_items: max_selected_items
53
+ )
54
+
55
+ yield(@element) if block_given?
56
+
57
+ self
58
+ end
59
+
60
+ def channels_select(placeholder:, action_id:, initial: nil, emoji: nil)
61
+ @element = Element::ChannelsSelect.new(
62
+ placeholder: placeholder,
63
+ action_id: action_id,
64
+ initial: initial,
65
+ emoji: emoji
66
+ )
67
+
68
+ yield(@element) if block_given?
69
+
70
+ self
71
+ end
72
+
73
+ def checkboxes(action_id:)
74
+ @element = Element::Checkboxes.new(action_id: action_id)
75
+
76
+ yield(@element) if block_given?
77
+
78
+ self
79
+ end
80
+
81
+ def datepicker(action_id:, placeholder: nil, initial: nil, emoji: nil)
82
+ @element = Element::DatePicker.new(
83
+ action_id: action_id,
84
+ placeholder: placeholder,
85
+ initial: initial,
86
+ emoji: emoji
87
+ )
88
+
89
+ yield(@element) if block_given?
90
+
91
+ self
92
+ end
93
+
94
+ def multi_channels_select(placeholder:, action_id:, initial: nil, emoji: nil, max_selected_items: nil)
95
+ @element = Element::MultiChannelsSelect.new(
96
+ placeholder: placeholder,
97
+ action_id: action_id,
98
+ initial: initial,
99
+ emoji: emoji,
100
+ max_selected_items: max_selected_items
101
+ )
102
+
103
+ yield(@element) if block_given?
104
+
105
+ self
106
+ end
107
+
108
+ def static_select(placeholder:, action_id:, emoji: nil)
109
+ @element = Element::StaticSelect.new(
110
+ placeholder: placeholder,
111
+ action_id: action_id,
112
+ emoji: emoji
113
+ )
114
+
115
+ yield(@element) if block_given?
116
+
117
+ self
118
+ end
119
+
120
+ def multi_static_select(placeholder:, action_id:, emoji: nil, max_selected_items: nil)
121
+ @element = Element::MultiStaticSelect.new(
122
+ placeholder: placeholder,
123
+ action_id: action_id,
124
+ emoji: emoji,
125
+ max_selected_items: max_selected_items
126
+ )
127
+
128
+ yield(@element) if block_given?
129
+
130
+ self
131
+ end
132
+
133
+ def external_select(placeholder:, action_id:, initial: nil, min_query_length: nil, emoji: nil)
134
+ @element = Element::ExternalSelect.new(
135
+ placeholder: placeholder,
136
+ action_id: action_id,
137
+ initial: initial,
138
+ min_query_length: min_query_length,
139
+ emoji: emoji
140
+ )
141
+
142
+ yield(@element) if block_given?
143
+
144
+ self
145
+ end
146
+
147
+ def multi_external_select(
148
+ placeholder:,
149
+ action_id:,
150
+ initial: nil,
151
+ min_query_length: nil,
152
+ emoji: nil,
153
+ max_selected_items: nil
154
+ )
155
+ @element = Element::MultiExternalSelect.new(
156
+ placeholder: placeholder,
157
+ action_id: action_id,
158
+ initial: initial,
159
+ min_query_length: min_query_length,
160
+ emoji: emoji,
161
+ max_selected_items: max_selected_items
162
+ )
163
+
164
+ yield(@element) if block_given?
165
+
166
+ self
167
+ end
168
+
169
+ def plain_text_input(
170
+ action_id:,
171
+ placeholder: nil,
172
+ emoji: nil,
173
+ initial_value: nil,
174
+ multiline: nil,
175
+ min_length: nil,
176
+ max_length: nil
177
+ )
178
+ @element = Element::PlainTextInput.new(
179
+ action_id: action_id,
180
+ placeholder: placeholder,
181
+ emoji: emoji,
182
+ initial_value: initial_value,
183
+ multiline: multiline,
184
+ min_length: min_length,
185
+ max_length: max_length
186
+ )
187
+
188
+ self
189
+ end
190
+
191
+ def radio_buttons(action_id:)
192
+ @element = Element::RadioButtons.new(action_id: action_id)
193
+
194
+ yield(@element) if block_given?
195
+
196
+ self
197
+ end
198
+
199
+ def users_select(placeholder:, action_id:, initial: nil, emoji: nil)
200
+ @element = Element::UsersSelect.new(
201
+ placeholder: placeholder,
202
+ action_id: action_id,
203
+ initial: initial,
204
+ emoji: emoji
205
+ )
206
+
207
+ yield(@element) if block_given?
208
+
209
+ self
210
+ end
211
+
212
+ def multi_users_select(placeholder:, action_id:, initial: nil, emoji: nil, max_selected_items: nil)
213
+ @element = Element::MultiUsersSelect.new(
214
+ placeholder: placeholder,
215
+ action_id: action_id,
216
+ initial: initial,
217
+ emoji: emoji,
218
+ max_selected_items: max_selected_items
219
+ )
220
+
221
+ yield(@element) if block_given?
222
+
223
+ self
29
224
  end
30
225
 
31
226
  def as_json(*)
@@ -35,7 +230,8 @@ module Slack
35
230
  label: @label&.as_json,
36
231
  hint: @hint&.as_json,
37
232
  block_id: @block_id,
38
- optional: optional
233
+ optional: optional,
234
+ dispatch_action: @dispatch_action
39
235
  }.compact
40
236
  end
41
237
  end