Class: Banacle::SlashCommand::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/banacle/slash_command/command.rb

Constant Summary collapse

CREATE_ACTION =
'create'.freeze
DELETE_ACTION =
'delete'.freeze
PERMITTED_ACTIONS =
[CREATE_ACTION, DELETE_ACTION].freeze
CODE_BLOCK_JSON_REGEX =
/```([^`]+)```/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action:, region:, vpc_id:, cidr_blocks:) ⇒ Command

Returns a new instance of Command.



20
21
22
23
24
25
# File 'lib/banacle/slash_command/command.rb', line 20

def initialize(action:, region:, vpc_id:, cidr_blocks:)
  @action = action
  @region = region
  @vpc_id = vpc_id
  @cidr_blocks = cidr_blocks
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



27
28
29
# File 'lib/banacle/slash_command/command.rb', line 27

def action
  @action
end

#cidr_blocksObject (readonly)

Returns the value of attribute cidr_blocks.



27
28
29
# File 'lib/banacle/slash_command/command.rb', line 27

def cidr_blocks
  @cidr_blocks
end

#regionObject (readonly)

Returns the value of attribute region.



27
28
29
# File 'lib/banacle/slash_command/command.rb', line 27

def region
  @region
end

#vpc_idObject (readonly)

Returns the value of attribute vpc_id.



27
28
29
# File 'lib/banacle/slash_command/command.rb', line 27

def vpc_id
  @vpc_id
end

Class Method Details

.new_from_original_message(message) ⇒ Object



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

def self.new_from_original_message(message)
  original_json = JSON.parse(
    message.match(CODE_BLOCK_JSON_REGEX)[1].strip, symbolize_names: true,
  )
  new(**original_json)
end

Instance Method Details

#executeObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/banacle/slash_command/command.rb', line 29

def execute
  case action
  when CREATE_ACTION
    create_nacl
  when DELETE_ACTION
    delete_nacl
  else
    # Do nothing
  end
end

#to_code_blockObject



40
41
42
43
44
45
46
# File 'lib/banacle/slash_command/command.rb', line 40

def to_code_block
  <<-EOS
```
#{JSON.pretty_generate(self.to_h)}
```
  EOS
end

#to_hObject



48
49
50
51
52
53
54
55
# File 'lib/banacle/slash_command/command.rb', line 48

def to_h
  {
    action: action,
    region: region,
    vpc_id: vpc_id,
    cidr_blocks: cidr_blocks,
  }
end