class SlackMessage::Dsl
Constants
- EMSPACE
Attributes
body[R]
custom_bot_icon[R]
custom_bot_name[R]
custom_notification[RW]
default_section[R]
profile[R]
Public Class Methods
new(block, profile)
click to toggle source
# File lib/slack_message/dsl.rb, line 7 def initialize(block, profile) # Delegate missing methods to caller scope. Thanks 2008: # https://www.dan-manges.com/blog/ruby-dsls-instance-eval-with-delegation @caller_self = eval("self", block.binding) @body = [] @profile = profile @default_section = Section.new(self) @custom_bot_name = nil @custom_bot_icon = nil @custom_notification = nil end
Public Instance Methods
accessory_image(*args)
click to toggle source
# File lib/slack_message/dsl.rb, line 84 def accessory_image(*args); default_section.accessory_image(*args); end
blank_line(*args)
click to toggle source
# File lib/slack_message/dsl.rb, line 85 def blank_line(*args); default_section.blank_line(*args); end
bot_icon(icon)
click to toggle source
# File lib/slack_message/dsl.rb, line 99 def bot_icon(icon) @custom_bot_icon = icon end
bot_name(name)
click to toggle source
bot / notification overrides
# File lib/slack_message/dsl.rb, line 95 def bot_name(name) @custom_bot_name = name end
context(text)
click to toggle source
# File lib/slack_message/dsl.rb, line 57 def context(text) finalize_default_section if text == "" || text.nil? raise ArgumentError, "Cannot create a context block without a value." end text = self.enrich_text(text) previous_context = @body.find { |element| element[:type] && element[:type] == "context" } if previous_context previous_text = previous_context[:elements].first[:text] warn "WARNING: Overriding previous context in section: #{previous_text}" end @body.push({ type: "context", elements: [{ type: "mrkdwn", text: text }]}) end
divider()
click to toggle source
# File lib/slack_message/dsl.rb, line 33 def divider finalize_default_section @body.push({ type: "divider" }) end
enrich_text(text_body)
click to toggle source
replace emails w/ real user IDs
# File lib/slack_message/dsl.rb, line 123 def enrich_text(text_body) text_body.scan(SlackMessage::EMAIL_TAG_PATTERN).each do |email_tag| begin raw_email = email_tag.gsub(/[><]/, '') user_id = SlackMessage::Api::user_id_for(raw_email, profile) text_body.gsub!(email_tag, "<@#{user_id}>") if user_id rescue SlackMessage::ApiError => e # swallow errors for not-found users end end text_body end
image(url, alt_text:, title: nil)
click to toggle source
# File lib/slack_message/dsl.rb, line 39 def image(url, alt_text:, title: nil) finalize_default_section config = { type: "image", image_url: url, alt_text: alt_text, } if !title.nil? config[:title] = { type: "plain_text", text: title, emoji: true } end @body.push(config) end
link(*args)
click to toggle source
# File lib/slack_message/dsl.rb, line 86 def link(*args); default_section.link(*args); end
list_item(*args)
click to toggle source
# File lib/slack_message/dsl.rb, line 87 def list_item(*args); default_section.list_item(*args); end
method_missing(meth, *args, &blk)
click to toggle source
# File lib/slack_message/dsl.rb, line 118 def method_missing(meth, *args, &blk) @caller_self.send meth, *args, &blk end
notification_text(msg)
click to toggle source
# File lib/slack_message/dsl.rb, line 103 def notification_text(msg) if @custom_notification warn "WARNING: Overriding previous custom notification text: #{@custom_notification}" end @custom_notification = msg end
ol(*args)
click to toggle source
# File lib/slack_message/dsl.rb, line 89 def ol(*args); default_section.ol(*args); end
render()
click to toggle source
end bot name
# File lib/slack_message/dsl.rb, line 113 def render finalize_default_section @body end
section(&block)
click to toggle source
allowable top-level entities within a block
# File lib/slack_message/dsl.rb, line 23 def section(&block) finalize_default_section section = Section.new(self).tap do |s| s.instance_eval(&block) end @body.push(section.render) end
text(*args)
click to toggle source
delegation to allow terse syntax without e.g. ‘section`
# File lib/slack_message/dsl.rb, line 82 def text(*args); default_section.text(*args); end
ul(*args)
click to toggle source
# File lib/slack_message/dsl.rb, line 88 def ul(*args); default_section.ul(*args); end
Private Instance Methods
finalize_default_section()
click to toggle source
when doing things that would generate new top-levels, first try to finish the implicit section.
# File lib/slack_message/dsl.rb, line 142 def finalize_default_section if default_section.has_content? @body.push(default_section.render) end @default_section = Section.new(self) end