Class: Volt::Dispatcher
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
- #dispatch(channel, message) ⇒ Object
-
#safe_method?(klass, method_name) ⇒ Boolean
Check if it is safe to use this method.
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, ) callback_id, class_name, method_name, *args = 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.('response', callback_id, result, error) end end |
#safe_method?(klass, method_name) ⇒ Boolean
Check if it is safe to use this method
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 |