Class: GetInstanceFieldHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/hypertube-ruby-sdk/sdk/tools/get_instance_field_helper.rb

Class Method Summary collapse

Class Method Details

.get_field_value(command, field_name) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hypertube-ruby-sdk/sdk/tools/get_instance_field_helper.rb', line 8

def get_field_value(command, field_name)
  return nil if command.nil? || command.payload.nil? || command.payload.empty?

  if command.command_type == CommandType::SET_INSTANCE_FIELD && command.payload.length >= 3
    name = get_payload_value(command.payload[1])
    return get_payload_value(command.payload[2]) if name.is_a?(String) && name == field_name
  end

  if command.command_type == CommandType::ARRAY
    command.payload.each do |item|
      if item.is_a?(Command)
        result = get_field_value(item, field_name)
        return result unless result.nil?
      end
    end
    return nil
  end

  first_payload = command.payload[0]
  return get_field_value(first_payload, field_name) if first_payload.is_a?(Command)

  nil
end