Class: Slackdraft::Attachment

Inherits:
Message show all
Defined in:
lib/slackdraft/attachment.rb

Direct Known Subclasses

Format::Alert

Instance Method Summary collapse

Methods inherited from Message

#add_attachment, #channel, #generate_payload, #icon_emoji, #icon_url, #parse_usernames_and_channels, #text, #username

Methods inherited from Base

#target

Constructor Details

#initialize(url = nil) ⇒ Attachment

Capture the url if we pass it in



22
23
24
25
26
# File 'lib/slackdraft/attachment.rb', line 22

def initialize(url=nil)
  unless url.nil?
    @target = url
  end
end

Instance Method Details

#add_field(title, value, short = true) ⇒ Object

Short defines if it’s 1 column or not, default: not



29
30
31
# File 'lib/slackdraft/attachment.rb', line 29

def add_field(title, value, short=true)
    fields.push({:title => title, :value => value, :short => short })
end

#generate_attachmentObject

Generate the payload for slack attachments



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/slackdraft/attachment.rb', line 34

def generate_attachment
    payload = {}
    payload[:fallback]    = self.fallback    unless self.fallback.nil?
    payload[:color]       = self.color       unless self.color.nil?
    payload[:pretext]     = self.pretext     unless self.pretext.nil?
    payload[:author_name] = self.author_name unless self.author_name.nil?
    payload[:author_link] = self.author_link unless self.author_link.nil?
    payload[:author_icon] = self.author_icon unless self.author_icon.nil?
    payload[:title]       = self.title       unless self.title.nil?
    payload[:title_link]  = self.title_link  unless self.title_link.nil?
    payload[:text]        = self.message     unless self.message.nil?
    
    unless self.fields.nil?
        payload[:fields]      = self.fields if self.fields.length > 0
    end

    payload[:image_url]   = self.image_url   unless self.image_url.nil?

    payload
end

#send!Object

Send it directly from Attachment



61
62
63
64
# File 'lib/slackdraft/attachment.rb', line 61

def send!
    add_attachment(generate_attachment)
    super
end

#to_hObject

To hash, for use in Message



56
57
58
# File 'lib/slackdraft/attachment.rb', line 56

def to_h
    generate_attachment
end