class SlackMessage::Dsl::Section

Attributes

body[R]

Public Class Methods

new(parent) click to toggle source
# File lib/slack_message/dsl.rb, line 153
def initialize(parent)
  @parent = parent
  @body = { type: "section" }
  @list = List.new
end

Public Instance Methods

accessory_image(url, alt_text: nil) click to toggle source
# 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_line() click to toggle source
# File lib/slack_message/dsl.rb, line 254
def blank_line
  text EMSPACE
end
has_content?() click to toggle source
# File lib/slack_message/dsl.rb, line 258
def has_content?
  @body.keys.length > 1 || @list.any?
end
list_item(title, value) click to toggle source
# 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
method_missing(meth, *args, &blk) click to toggle source
# File lib/slack_message/dsl.rb, line 276
def method_missing(meth, *args, &blk)
  @parent.send meth, *args, &blk
end
ol(elements) click to toggle source
# 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
render() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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