Class: Volt::Dispatcher

Inherits:
Object show all
Defined in:
lib/volt/tasks/dispatcher.rb

Overview

The task dispatcher is responsible for taking incoming messages from the socket channel and dispatching them to the proper handler.

Instance Method Summary collapse

Instance Method Details

#dispatch(channel, message) ⇒ Object



5
6
7
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
# File 'lib/volt/tasks/dispatcher.rb', line 5

def dispatch(channel, message)
  callback_id, class_name, method_name, *args = message
  method_name = method_name.to_sym

  # Get the class
  klass = Object.send(:const_get, class_name)

  result = nil
  error = nil

  if safe_method?(klass, method_name)
    # Init and send the method
    begin
      result = klass.new(channel, self).send(method_name, *args)
    rescue => e
      # TODO: Log these errors better
      puts e.inspect
      puts e.backtrace
      error = e
    end
  else
    # Unsafe method
    error = RuntimeError.new("unsafe method: #{method_name}")
  end

  if callback_id
    # Callback with result
    channel.send_message('response', callback_id, result, error)
  end
end

#safe_method?(klass, method_name) ⇒ Boolean

Check if it is safe to use this method

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/volt/tasks/dispatcher.rb', line 37

def safe_method?(klass, method_name)
  # Make sure the class being called is a TaskHandler.
  return false unless klass.ancestors.include?(TaskHandler)

  # Make sure the method is defined on the klass we're using and not up the hiearchy.
  #   ^ This check prevents methods like #send, #eval, #instance_eval, #class_eval, etc...
  klass.ancestors.each do |ancestor_klass|
    if ancestor_klass.instance_methods(false).include?(method_name)
      return true
    elsif ancestor_klass == TaskHandler
      # We made it to TaskHandler and didn't find the method, that means it
      # was defined above TaskHandler, so we reject the call.
      return false
    end
  end

  return false
end