Class: Banacle::SlashCommand::Builder
- Inherits:
-
Object
- Object
- Banacle::SlashCommand::Builder
show all
- Defined in:
- lib/banacle/slash_command/builder.rb
Defined Under Namespace
Classes: InvalidActionError, InvalidCidrBlockError, InvalidRegionError, InvalidVpcError
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(action:, region:, vpc_id_or_name:, cidr_blocks:) ⇒ Builder
Returns a new instance of Builder.
19
20
21
22
23
24
|
# File 'lib/banacle/slash_command/builder.rb', line 19
def initialize(action:, region:, vpc_id_or_name:, cidr_blocks:)
@action = action
@region = region
@vpc_id_or_name = vpc_id_or_name
@cidr_blocks = cidr_blocks
end
|
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
26
27
28
|
# File 'lib/banacle/slash_command/builder.rb', line 26
def action
@action
end
|
#cidr_blocks ⇒ Object
Returns the value of attribute cidr_blocks.
26
27
28
|
# File 'lib/banacle/slash_command/builder.rb', line 26
def cidr_blocks
@cidr_blocks
end
|
#region ⇒ Object
Returns the value of attribute region.
26
27
28
|
# File 'lib/banacle/slash_command/builder.rb', line 26
def region
@region
end
|
#vpc_id ⇒ Object
Returns the value of attribute vpc_id.
27
28
29
|
# File 'lib/banacle/slash_command/builder.rb', line 27
def vpc_id
@vpc_id
end
|
#vpc_id_or_name ⇒ Object
Returns the value of attribute vpc_id_or_name.
26
27
28
|
# File 'lib/banacle/slash_command/builder.rb', line 26
def vpc_id_or_name
@vpc_id_or_name
end
|
Class Method Details
.build(action:, region:, vpc_id_or_name:, cidr_blocks:) ⇒ Object
15
16
17
|
# File 'lib/banacle/slash_command/builder.rb', line 15
def self.build(action:, region:, vpc_id_or_name:, cidr_blocks:)
new(action: action, region: region, vpc_id_or_name: vpc_id_or_name, cidr_blocks: cidr_blocks).build
end
|
Instance Method Details
#build ⇒ Object
29
30
31
32
33
34
35
|
# File 'lib/banacle/slash_command/builder.rb', line 29
def build
validate!
set_vpc_id!
normalize_cidr_blocks!
Command.new(action: action, region: region, vpc_id: vpc_id, cidr_blocks: cidr_blocks)
end
|
#normalize_cidr_blocks! ⇒ Object
92
93
94
95
96
97
|
# File 'lib/banacle/slash_command/builder.rb', line 92
def normalize_cidr_blocks!
cidr_blocks.map! do |c|
ip = IPAddr.new(c)
"#{ip}/#{ip.prefix}"
end
end
|
#validate! ⇒ Object
37
38
39
40
41
42
|
# File 'lib/banacle/slash_command/builder.rb', line 37
def validate!
validate_action!
validate_region!
validate_vpc_id_or_name!
validate_cidr_blocks!
end
|
#validate_action! ⇒ Object
#validate_cidr_blocks! ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/banacle/slash_command/builder.rb', line 66
def validate_cidr_blocks!
if !cidr_blocks || cidr_blocks.empty?
raise InvalidVpcError.new("at least one cidr_block is required with #{action} action")
end
cidr_blocks.each do |cidr_block|
begin
IPAddr.new(cidr_block)
rescue IPAddr::InvalidAddressError
raise InvalidCidrBlockError.new("#{cidr_block} is invalid address")
end
end
end
|
#validate_region! ⇒ Object
54
55
56
57
58
|
# File 'lib/banacle/slash_command/builder.rb', line 54
def validate_region!
if !region || region.empty?
raise InvalidRegionError.new("region is required")
end
end
|
#validate_vpc_id_or_name! ⇒ Object
60
61
62
63
64
|
# File 'lib/banacle/slash_command/builder.rb', line 60
def validate_vpc_id_or_name!
unless vpc_id_or_name
raise InvalidVpcError.new("vpc_id or vpc_name is required with #{action} action")
end
end
|