Class: Banacle::Slack::Attachment

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

Defined Under Namespace

Classes: ValidationError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Attachment

Returns a new instance of Attachment.



51
52
53
54
55
56
# File 'lib/banacle/slack.rb', line 51

def initialize(*args)
  super
  self.set_default!
  self.validate!
  self
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions

Returns:

  • (Object)

    the current value of actions



47
48
49
# File 'lib/banacle/slack.rb', line 47

def actions
  @actions
end

#attachment_typeObject

Returns the value of attribute attachment_type

Returns:

  • (Object)

    the current value of attachment_type



47
48
49
# File 'lib/banacle/slack.rb', line 47

def attachment_type
  @attachment_type
end

#callback_idObject

Returns the value of attribute callback_id

Returns:

  • (Object)

    the current value of callback_id



47
48
49
# File 'lib/banacle/slack.rb', line 47

def callback_id
  @callback_id
end

#colorObject

Returns the value of attribute color

Returns:

  • (Object)

    the current value of color



47
48
49
# File 'lib/banacle/slack.rb', line 47

def color
  @color
end

#fallbackObject

Returns the value of attribute fallback

Returns:

  • (Object)

    the current value of fallback



47
48
49
# File 'lib/banacle/slack.rb', line 47

def fallback
  @fallback
end

#textObject

Returns the value of attribute text

Returns:

  • (Object)

    the current value of text



47
48
49
# File 'lib/banacle/slack.rb', line 47

def text
  @text
end

Instance Method Details

#as_jsonObject



81
82
83
# File 'lib/banacle/slack.rb', line 81

def as_json
  self.to_h.tap { |h| h[:actions] = h[:actions].map(&:as_json) }
end

#set_default!Object



58
59
60
61
62
63
64
65
# File 'lib/banacle/slack.rb', line 58

def set_default!
  self.text ||= ''
  self.fallback ||= ''
  self.callback_id ||= ''
  self.color ||= ''
  self.attachment_type ||= ''
  self.actions ||= []
end

#validate!Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/banacle/slack.rb', line 67

def validate!
  %i(text fallback callback_id color attachment_type).each do |label|
    unless self.send(label).is_a?(String)
      raise ValidationError.new("#{attr} must be String")
    end
  end

  self.actions.each do |a|
    unless a.is_a?(Slack::Action)
      raise ValidationError.new("One of actions #{a.inspect} must be Slack::Action")
    end
  end
end