Class: SlackMessage::Dsl::Section

Inherits:
Object
  • Object
show all
Defined in:
lib/slack_message/dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ Section

Returns a new instance of Section.



153
154
155
156
157
# File 'lib/slack_message/dsl.rb', line 153

def initialize(parent)
  @parent = parent
  @body = { type: "section" }
  @list = List.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



276
277
278
# File 'lib/slack_message/dsl.rb', line 276

def method_missing(meth, *args, &blk)
  @parent.send meth, *args, &blk
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



151
152
153
# File 'lib/slack_message/dsl.rb', line 151

def body
  @body
end

Instance Method Details

#accessory_image(url, alt_text: nil) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/slack_message/dsl.rb', line 222

def accessory_image(url, alt_text: nil)
  if !@body[:accessory].nil?
    previous_type = @body[:accessory][:type]
    warn "WARNING: Overriding previous #{previous_type} in section to use accessory image instead: #{url}"
  end

  config = {
    accessory: {
      type: "image",
      image_url: url
    }
  }

  config[:accessory][:alt_text] = alt_text if !alt_text.nil?

  @body.merge!(config)
end

#blank_lineObject



254
255
256
# File 'lib/slack_message/dsl.rb', line 254

def blank_line
  text EMSPACE
end

#has_content?Boolean

Returns:

  • (Boolean)


258
259
260
# File 'lib/slack_message/dsl.rb', line 258

def has_content?
  @body.keys.length > 1 || @list.any?
end

for markdown links



241
242
243
# File 'lib/slack_message/dsl.rb', line 241

def link(label, target)
  "<#{target}|#{label}>"
end

styles: default, primary, danger



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/slack_message/dsl.rb', line 193

def link_button(label, target, style: :primary)
  if !@body[:accessory].nil?
    previous_type = @body[:accessory][:type]
    warn "WARNING: Overriding previous #{previous_type} in section to use link_button instead: #{label}"
  end

  unless /(^|\s)((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/i =~ target
    warn "WARNING: Passing a probably-invalid URL to link button #{label} (url: '#{target}')"
  end

  config = {
    accessory: {
      type: "button",
      url: target,
      text: {
        type: "plain_text",
        text: label,
        emoji: true
      },
    }
  }

  if style != :default
    config[:accessory][:style] = style
  end

  @body.merge!(config)
end

#list_item(title, value) ⇒ Object



245
246
247
248
249
250
251
252
# File 'lib/slack_message/dsl.rb', line 245

def list_item(title, value)
  if value == "" || value.nil?
    raise ArgumentError, "Can't create a list item for '#{title}' without a value."
  end

  value = @parent.enrich_text(value)
  @list.add(title, value)
end

#ol(elements) ⇒ Object

Raises:

  • (ArgumentError)


183
184
185
186
187
188
189
190
# File 'lib/slack_message/dsl.rb', line 183

def ol(elements)
  raise ArgumentError, "Please pass an array when creating an ol." unless elements.respond_to?(:map)

  msg = elements.map.with_index(1) { |text, idx| "#{EMSPACE}#{idx}. #{text}" }.join("\n")
  msg = @parent.enrich_text(msg)

  text(msg)
end

#renderObject



262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/slack_message/dsl.rb', line 262

def render
  unless has_content?
    raise ArgumentError, "Can't create a section with no content."
  end

  body[:fields] = @list.render if @list.any?

  if body[:text] && body[:text][:text] && !@parent.custom_notification
    @parent.notification_text(body[:text][:text])
  end

  body
end

#text(msg) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/slack_message/dsl.rb', line 159

def text(msg)
  if msg == "" || msg.nil?
    raise ArgumentError, "Cannot create a text node without a value."
  end

  msg = @parent.enrich_text(msg)

  if @body.include?(:text)
    @body[:text][:text] << "\n#{msg}"

  else
    @body.merge!({ text: { type: "mrkdwn", text: msg } })
  end
end

#ul(elements) ⇒ Object

Raises:

  • (ArgumentError)


174
175
176
177
178
179
180
181
# File 'lib/slack_message/dsl.rb', line 174

def ul(elements)
  raise ArgumentError, "Please pass an array when creating a ul." unless elements.respond_to?(:map)

  msg = elements.map { |text| "#{EMSPACE}#{text}" }.join("\n")
  msg = @parent.enrich_text(msg)

  text(msg)
end