Method: Nodo::Core#call_js_method

Defined in:
lib/nodo/core.rb

#call_js_method(method, args) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/nodo/core.rb', line 184

def call_js_method(method, args)
  raise CallError, 'Node process not ready' unless node_pid
  raise CallError, "Class #{clsid} not defined" unless self.class.class_defined? || method == DEFINE_METHOD
  function = self.class.functions[method]
  raise NameError, "undefined function `#{method}' for #{self.class}" unless function || method == DEFINE_METHOD
  request = Net::HTTP::Post.new("/#{clsid}/#{method}", 'Content-Type': 'application/json')
  request.body = JSON.dump(args)
  client = Client.new("unix://#{socket_path}")
  client.read_timeout = function.timeout if function
  response = client.request(request)
  if response.is_a?(Net::HTTPOK)
    parse_response(response)
  else
    handle_error(response, function)
  end
rescue Net::ReadTimeout
  raise TimeoutError, "function call #{self.class}##{method} timed out"
rescue Errno::EPIPE, IOError
  # TODO: restart or something? If this happens the process is completely broken
  raise Error, 'Node process failed'
end