Class: InvokeStaticMethodHandler

Inherits:
AbstractCommandHandler show all
Defined in:
lib/hypertube-ruby-sdk/core/handler/invoke_static_method_handler.rb

Instance Method Summary collapse

Methods inherited from AbstractCommandHandler

#handle_command, #iterate

Constructor Details

#initializeInvokeStaticMethodHandler

Returns a new instance of InvokeStaticMethodHandler.



6
7
8
# File 'lib/hypertube-ruby-sdk/core/handler/invoke_static_method_handler.rb', line 6

def initialize
  @required_parameters_count = 2
end

Instance Method Details

#invoke_static_method(command) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hypertube-ruby-sdk/core/handler/invoke_static_method_handler.rb', line 14

def invoke_static_method(command)
  raise ArgumentError.new 'Static method parameters mismatch' if command.payload.length < @required_parameters_count

  type = command.payload[0]
  method_name = command.payload[1]
  if command.payload.length > @required_parameters_count
    args = command.payload[2..]
    result = type.send(method_name, *args)
  else
    result = type.send(method_name)
  end
  result
rescue NoMethodError
  methods = command.payload[0].methods
  message = "Method #{method_name} not found in class #{command.payload[0].name}. Available methods:\n"
  methods.each { |method_iter| message += "#{method_iter}\n" }
  raise Exception, message
end

#process(command) ⇒ Object



10
11
12
# File 'lib/hypertube-ruby-sdk/core/handler/invoke_static_method_handler.rb', line 10

def process(command)
  invoke_static_method(command)
end