Class: Slack::Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/slack/attachment.rb

Constant Summary collapse

ATTRIBUTES =
%i[
  fallback text title title_link image_url thumb_url color pretext author
  author_name author_link author_icon
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Attachment

Returns a new instance of Attachment.



16
17
18
19
20
21
22
# File 'lib/slack/attachment.rb', line 16

def initialize(options = {})
  @fields = []

  ATTRIBUTES.each do |attribute|
    send("#{attribute}=", options.delete(attribute))
  end
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



14
15
16
# File 'lib/slack/attachment.rb', line 14

def fields
  @fields
end

Instance Method Details

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



24
25
26
# File 'lib/slack/attachment.rb', line 24

def add_field(title, value, short = false)
  fields << Field.new(title, value, short)
end

#as_jsonObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/slack/attachment.rb', line 28

def as_json
  hash = {}

  ATTRIBUTES.each do |attribute|
    hash[attribute] = send(attribute) if send(attribute)
  end

  hash[:fields] = fields.map(&:as_json) unless fields.empty?
  hash[:author] = author.as_json if author

  hash
end