Class: ChefEc2NodeRm::SqsMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/chef_ec2_node_rm/sqs_message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg) ⇒ SqsMessage

Returns a new instance of SqsMessage.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/chef_ec2_node_rm/sqs_message.rb', line 5

def initialize(msg)
  msg_parsed = JSON.parse(msg)
  raise SqsMessageNotHash, "Expected a Hash but got a #{msg_parsed.class}" unless msg_parsed.is_a?(Hash)

  attr_map = {
    instance_id: ['detail', 'instance-id'],
    state: %w[detail state]
  }
  attr_map.each do |name, path|
    value = msg_parsed.dig(*path)
    raise ChefEc2NodeRm::SqsMessageMissingAttribute, "Missing value for #{name} (msg='#{msg}', path='#{path.join('.')}')" if value.nil?
    instance_variable_set("@#{name}", value)
  end
rescue JSON::ParserError => e
  raise ChefEc2NodeRm::SqsMessageMalformedJson, "Malformed JSON: #{e}"
end

Instance Attribute Details

#instance_idObject (readonly)

Returns the value of attribute instance_id.



3
4
5
# File 'lib/chef_ec2_node_rm/sqs_message.rb', line 3

def instance_id
  @instance_id
end

#stateObject (readonly)

Returns the value of attribute state.



3
4
5
# File 'lib/chef_ec2_node_rm/sqs_message.rb', line 3

def state
  @state
end