aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2015-06-19 22:29:43 +0200
committerDavid Heinemeier Hansson <david@loudthinking.com>2015-06-19 22:29:43 +0200
commita44033e623f15524ce87235c79c9e547a54c8e69 (patch)
treea5442d4faf79aa8d3f9dea282ec99b1de3b68333 /lib
parent2258344e0fbeb1af74ebe477f392a76fa6d6f4ef (diff)
downloadrails-a44033e623f15524ce87235c79c9e547a54c8e69.tar.gz
rails-a44033e623f15524ce87235c79c9e547a54c8e69.tar.bz2
rails-a44033e623f15524ce87235c79c9e547a54c8e69.zip
Use a perform_action router to handle incoming data
Similar to a controller in ActionPack.
Diffstat (limited to 'lib')
-rw-r--r--lib/action_cable/channel/base.rb12
-rw-r--r--lib/action_cable/connection/base.rb2
2 files changed, 9 insertions, 5 deletions
diff --git a/lib/action_cable/channel/base.rb b/lib/action_cable/channel/base.rb
index cc9580ec52..c18593bf6f 100644
--- a/lib/action_cable/channel/base.rb
+++ b/lib/action_cable/channel/base.rb
@@ -35,12 +35,16 @@ module ActionCable
run_subscribe_callbacks
end
- def receive_data(data)
+ def perform_action(data)
if authorized?
- if respond_to?(:receive)
- receive(data)
+ action = (data['action'].presence || :receive).to_sym
+ signature = "#{self.class.name}##{action}: #{data}"
+
+ if self.class.instance_methods(false).include?(action)
+ logger.info "Processing #{signature}"
+ public_send action, data
else
- logger.error "#{self.class.name} received data (#{data}) but #{self.class.name}#receive callback is not defined"
+ logger.error "Failed to process #{signature}"
end
else
unauthorized
diff --git a/lib/action_cable/connection/base.rb b/lib/action_cable/connection/base.rb
index 5c2ee14258..e5ed07b5cc 100644
--- a/lib/action_cable/connection/base.rb
+++ b/lib/action_cable/connection/base.rb
@@ -150,7 +150,7 @@ module ActionCable
def process_message(message)
if @subscriptions[message['identifier']]
- @subscriptions[message['identifier']].receive_data(ActiveSupport::JSON.decode message['data'])
+ @subscriptions[message['identifier']].perform_action(ActiveSupport::JSON.decode message['data'])
else
raise "Unable to process message because no subscription was found (#{message.inspect})"
end