Class: Operationcode::Slack::Im::Interactive

Inherits:
Object
  • Object
show all
Defined in:
lib/operationcode/slack/im/interactive.rb

Constant Summary collapse

REQUIRED_ACTION_KEYS =
i(name text value)
VALID_STYLES =
i(default primary danger)

Instance Method Summary collapse

Constructor Details

#initialize(text:, id:, color: '#3AA3E3', actions:) ⇒ Interactive



8
9
10
11
12
13
14
15
# File 'lib/operationcode/slack/im/interactive.rb', line 8

def initialize(text:, id:, color: '#3AA3E3', actions:)
  @text = text
  @id = id
  @color = color
  @actions = actions

  validate_actions!
end

Instance Method Details

#action_payloadObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/operationcode/slack/im/interactive.rb', line 36

def action_payload
  @actions.map do |action|
    action[:style] = :default if action[:style].nil?
    {
      name: action[:name],
      text: action[:text],
      value: action[:value],
      style: action[:style],
      type: :button
    }
  end
end

#payloadObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/operationcode/slack/im/interactive.rb', line 23

def payload
  [
    {
      text: @text,
      fallback: @text,
      callback_id: @id,
      color: @color,
      attachment_type: 'default',
      actions: action_payload
    }
  ]
end

#validate_actions!Object

Raises:

  • (ArgumentError)


17
18
19
20
21
# File 'lib/operationcode/slack/im/interactive.rb', line 17

def validate_actions!
  raise(ArgumentError, 'actions must be an array') unless @actions.is_a? Array
  raise(ArgumentError, 'actions cannot be empty') if @actions.empty?
  @actions.each { |a| raise ArgumentError, "action #{a} is invalid" unless a.keys == REQUIRED_ACTION_KEYS }
end