Class: Slackdraft::Message

Inherits:
Base
  • Object
show all
Defined in:
lib/slackdraft/message.rb

Direct Known Subclasses

Attachment

Instance Method Summary collapse

Methods inherited from Base

#initialize, #send!, #target

Constructor Details

This class inherits a constructor from Slackdraft::Base

Instance Method Details

#add_attachment(attachment) ⇒ Object

Add an attachment



25
26
27
# File 'lib/slackdraft/message.rb', line 25

def add_attachment(attachment)
  self.attachments.push(attachment.to_h)
end

#channelObject

Set the destination channel



19
20
21
22
# File 'lib/slackdraft/message.rb', line 19

def channel
  return '#general' if @channel.nil?
  @channel
end

#generate_payloadObject

Generate the payload if stuff was provided



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/slackdraft/message.rb', line 55

def generate_payload
    payload = {}
    payload[:channel]     = self.channel    unless self.channel.nil?
    payload[:username]    = self.username   unless self.username.nil?
    payload[:icon_url]    = self.icon_url   unless self.icon_url.nil?
    payload[:icon_emoji]  = self.icon_emoji unless self.icon_emoji.nil?
    payload[:text]        = self.text       unless self.text.nil?
    payload[:attachments] = self.attachments unless self.attachments.empty?

    payload
end

#icon_emojiObject

Set the emoji



35
36
37
# File 'lib/slackdraft/message.rb', line 35

def icon_emoji
  return ':fire:' if @icon_emoji.nil?
end

#icon_urlObject

Set the icon URL and default to the emoji



30
31
32
# File 'lib/slackdraft/message.rb', line 30

def icon_url
  @icon_url || icon_emoji
end

#parse_usernames_and_channels(text) ⇒ Object

Parse username and channels



40
41
42
# File 'lib/slackdraft/message.rb', line 40

def parse_usernames_and_channels(text)
  text.gsub(/(@\w+)/, '<\1>').gsub(/(#\w+)/, '<\1>')
end

#textObject

Set the text of the message



45
46
47
48
49
50
51
52
# File 'lib/slackdraft/message.rb', line 45

def text
  
  if @text.kind_of?(Array)
    return parse_usernames_and_channels(@text.join("\n"))
  end
  
  parse_usernames_and_channels(@text)
end

#usernameObject

Set the api call username



13
14
15
16
# File 'lib/slackdraft/message.rb', line 13

def username
  return 'slackdraft' if @username.nil?
  @username
end