Class: InvokeGlobalFunctionHandler

Inherits:
AbstractCommandHandler show all
Defined in:
lib/javonet-ruby-sdk/core/handler/invoke_global_function_handler.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/Linux/X64/core/handler/invoke_global_function_handler.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/MacOs/X64/core/handler/invoke_global_function_handler.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/Windows/X64/core/handler/invoke_global_function_handler.rb

Instance Method Summary collapse

Methods inherited from AbstractCommandHandler

#handle_command, #iterate

Instance Method Details

#invoke_function(command) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/javonet-ruby-sdk/core/handler/invoke_global_function_handler.rb', line 8

def invoke_function(command)
  begin
    payload = command.payload
    raise ArgumentError, "InvokeGlobalFunction requires at least one parameter" if payload.empty?

    # Expect the first parameter to be a string in the form "ModuleName.function_name"
    full_method_string = payload[0]
    unless full_method_string.is_a?(String) && full_method_string.include?(".")
      raise ArgumentError, "Expected a string in the format 'ModuleName.function_name'"
    end

    parts = full_method_string.split(".")
    if parts.size < 2
      raise ArgumentError, "Invalid function identifier format. Expected 'ModuleName.function_name'"
    end

    function_name = parts.pop
    module_name = parts.join("::") # Convert dot-separated names to Ruby's "::" notation

    begin
      target = Object.const_get(module_name)
    rescue NameError => e
      raise NameError, "Module/Class #{module_name} not found: #{e.message}"
    end

    args = payload[1..-1] || []

    if target.respond_to?(function_name)
      target.send(function_name, *args)
    else
      available_methods = target.methods.join(', ')
      raise NoMethodError, "Method #{function_name} not found in module #{module_name}. Available methods: #{available_methods}"
    end
  rescue Exception => e
    e
  end
end

#process(command) ⇒ Object



4
5
6
# File 'lib/javonet-ruby-sdk/core/handler/invoke_global_function_handler.rb', line 4

def process(command)
  invoke_function(command)
end