Class: Hypertube::Utils::Command

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runtime_name, command_type, *payload) ⇒ Command

Returns a new instance of Command.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 13

def initialize(runtime_name, command_type, *payload)
  @runtime_name = runtime_name
  @command_type = command_type

  @payload = if payload.empty?
               []
             elsif payload.size == 1 && payload[0].is_a?(Array)
               # caller passed a single array -> reuse it (no extra copy)
               payload[0] || []
             else
               # varargs or multiple args -> use as provided
               payload
             end
end

Instance Attribute Details

#command_typeObject (readonly)

Returns the value of attribute command_type.



11
12
13
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 11

def command_type
  @command_type
end

#payloadObject (readonly)

Returns the value of attribute payload.



11
12
13
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 11

def payload
  @payload
end

#runtime_nameObject (readonly)

Returns the value of attribute runtime_name.



11
12
13
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 11

def runtime_name
  @runtime_name
end

Class Method Details

.create_reference(guid, runtime_name) ⇒ Object



32
33
34
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 32

def self.create_reference(guid, runtime_name)
  Hypertube::Utils::Command.new(runtime_name, Hypertube::Utils::CommandType::REFERENCE, guid)
end

.create_response(response, runtime_name) ⇒ Object



28
29
30
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 28

def self.create_response(response, runtime_name)
  Hypertube::Utils::Command.new(runtime_name, Hypertube::Utils::CommandType::VALUE, response)
end

Instance Method Details

#add_arg_to_payload(argument) ⇒ Object



43
44
45
46
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 43

def add_arg_to_payload(argument)
  new_payload = @payload + [argument]
  Hypertube::Utils::Command.new(@runtime_name, @command_type, new_payload)
end

#drop_first_payload_argumentObject



36
37
38
39
40
41
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 36

def drop_first_payload_argument
  return Hypertube::Utils::Command.new(@runtime_name, @command_type, []) if @payload.length <= 1

  new_payload = @payload[1..-1] || []
  Hypertube::Utils::Command.new(@runtime_name, @command_type, new_payload)
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 97

def eql?(other)
  return true if equal?(other)
  return false unless other.is_a?(Hypertube::Utils::Command)
  return false unless runtime_name == other.runtime_name && command_type == other.command_type
  return false unless payload.length == other.payload.length

  payload.each_with_index do |item, i|
    return false unless item.eql?(other.payload[i])
  end

  true
end

#prepend_arg_to_payload(arg_command) ⇒ Object



48
49
50
51
52
53
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 48

def prepend_arg_to_payload(arg_command)
  return self if arg_command.nil?

  new_payload = [arg_command] + @payload
  Hypertube::Utils::Command.new(@runtime_name, @command_type, new_payload)
end

#to_sObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 55

def to_s
  result = '  { '
  result += "RuntimeName: #{@runtime_name}, "
  result += "Hypertube::Utils::CommandType: #{@command_type}, "
  result += "Payload: \n"
  result += '    ['

  payload = @payload
  len = payload.length

  if len > 0
    result += "\n"
    len.times do |i|
      item = payload[i]
      item_str = if item.nil?
                   'null'
                 elsif item.is_a?(String)
                   "  \"#{item}\""
                 elsif item.is_a?(Hypertube::Utils::Command)
                   item.to_string.gsub("\n", "\n    ")
                 else
                   "  #{item}"
                 end

      result += "    #{item_str}"
      result += i < len - 1 ? ",\n" : "\n"
    end
    result += "    ]\n"
  else
    result += "  ]\n"
  end

  result += '  }'
  result
rescue StandardError => e
  "Error while converting command to string:#{e.message}"
end

#to_stringObject



93
94
95
# File 'lib/hypertube-ruby-sdk/utils/command.rb', line 93

def to_string
  to_s
end