Class: Banacle::Slack::Response

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) ⇒ Response

Returns a new instance of Response.



6
7
8
9
10
11
# File 'lib/banacle/slack.rb', line 6

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

Instance Attribute Details

#attachmentsObject

Returns the value of attribute attachments

Returns:

  • (Object)

    the current value of attachments



3
4
5
# File 'lib/banacle/slack.rb', line 3

def attachments
  @attachments
end

#replace_originalObject

Returns the value of attribute replace_original

Returns:

  • (Object)

    the current value of replace_original



3
4
5
# File 'lib/banacle/slack.rb', line 3

def replace_original
  @replace_original
end

#response_typeObject

Returns the value of attribute response_type

Returns:

  • (Object)

    the current value of response_type



3
4
5
# File 'lib/banacle/slack.rb', line 3

def response_type
  @response_type
end

#textObject

Returns the value of attribute text

Returns:

  • (Object)

    the current value of text



3
4
5
# File 'lib/banacle/slack.rb', line 3

def text
  @text
end

Instance Method Details

#as_jsonObject



38
39
40
# File 'lib/banacle/slack.rb', line 38

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

#set_default!Object



13
14
15
16
17
18
# File 'lib/banacle/slack.rb', line 13

def set_default!
  self.response_type ||= "in_channel"
  self.replace_original = true if self.replace_original.nil?
  self.text ||= ""
  self.attachments ||= []
end

#to_jsonObject



42
43
44
# File 'lib/banacle/slack.rb', line 42

def to_json
  as_json.to_json
end

#validate!Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/banacle/slack.rb', line 20

def validate!
  unless self.replace_original.is_a?(TrueClass) || self.replace_original.is_a?(FalseClass)
    raise ValidationError.new("replace_original must be TrueClass or FalseClass")
  end

  %i(response_type text).each do |label|
    unless self.send(label).is_a?(String)
      raise ValidationError.new("#{attr} must be String")
    end
  end

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