Class: SlackMessage::Dsl

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

Defined Under Namespace

Classes: List, Section

Constant Summary collapse

EMSPACE =

unicode emspace, Slack won’t compress this

""

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block, profile) ⇒ Dsl

Returns a new instance of Dsl.



7
8
9
10
11
12
13
14
15
16
17
18
19
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



118
119
120
# File 'lib/slack_message/dsl.rb', line 118

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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



2
3
4
# File 'lib/slack_message/dsl.rb', line 2

def body
  @body
end

#custom_bot_iconObject (readonly)

Returns the value of attribute custom_bot_icon.



2
3
4
# File 'lib/slack_message/dsl.rb', line 2

def custom_bot_icon
  @custom_bot_icon
end

#custom_bot_nameObject (readonly)

Returns the value of attribute custom_bot_name.



2
3
4
# File 'lib/slack_message/dsl.rb', line 2

def custom_bot_name
  @custom_bot_name
end

#custom_notificationObject

Returns the value of attribute custom_notification.



3
4
5
# File 'lib/slack_message/dsl.rb', line 3

def custom_notification
  @custom_notification
end

#default_sectionObject (readonly)

Returns the value of attribute default_section.



2
3
4
# File 'lib/slack_message/dsl.rb', line 2

def default_section
  @default_section
end

#profileObject (readonly)

Returns the value of attribute profile.



2
3
4
# File 'lib/slack_message/dsl.rb', line 2

def profile
  @profile
end

Instance Method Details

#accessory_image(*args, **kwargs) ⇒ Object



84
# File 'lib/slack_message/dsl.rb', line 84

def accessory_image(*args, **kwargs); default_section.accessory_image(*args, **kwargs); end

#blank_line(*args) ⇒ Object



85
# File 'lib/slack_message/dsl.rb', line 85

def blank_line(*args); default_section.blank_line(*args); end

#bot_icon(icon) ⇒ Object



99
100
101
# File 'lib/slack_message/dsl.rb', line 99

def bot_icon(icon)
  @custom_bot_icon = icon
end

#bot_name(name) ⇒ Object

bot / notification overrides



95
96
97
# File 'lib/slack_message/dsl.rb', line 95

def bot_name(name)
  @custom_bot_name = name
end

#context(text) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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

#dividerObject



33
34
35
36
37
# File 'lib/slack_message/dsl.rb', line 33

def divider
  finalize_default_section

  @body.push({ type: "divider" })
end

#enrich_text(text_body) ⇒ Object

replace emails w/ real user IDs



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# 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) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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


86
# File 'lib/slack_message/dsl.rb', line 86

def link(*args); default_section.link(*args); end


83
# File 'lib/slack_message/dsl.rb', line 83

def link_button(*args, **kwargs); default_section.link_button(*args, **kwargs); end

#list_item(*args) ⇒ Object



87
# File 'lib/slack_message/dsl.rb', line 87

def list_item(*args); default_section.list_item(*args); end

#notification_text(msg) ⇒ Object



103
104
105
106
107
108
109
# 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) ⇒ Object



89
# File 'lib/slack_message/dsl.rb', line 89

def ol(*args); default_section.ol(*args); end

#renderObject

end bot name



113
114
115
116
# File 'lib/slack_message/dsl.rb', line 113

def render
  finalize_default_section
  @body
end

#section(&block) ⇒ Object

allowable top-level entities within a block



23
24
25
26
27
28
29
30
31
# 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) ⇒ Object

delegation to allow terse syntax without e.g. ‘section`



82
# File 'lib/slack_message/dsl.rb', line 82

def text(*args); default_section.text(*args); end

#ul(*args) ⇒ Object



88
# File 'lib/slack_message/dsl.rb', line 88

def ul(*args); default_section.ul(*args); end