aboutsummaryrefslogtreecommitdiffstats
path: root/lib/action_cable
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2015-06-22 16:28:53 +0200
committerDavid Heinemeier Hansson <david@loudthinking.com>2015-06-22 16:28:53 +0200
commit82f1e19674b290fcee32a048707055e9b82aa310 (patch)
treeb558346cf154a42702d87769d9689b947dfe7cbe /lib/action_cable
parent04aed03c896f661143bce1e4b879cff480963fe6 (diff)
downloadrails-82f1e19674b290fcee32a048707055e9b82aa310.tar.gz
rails-82f1e19674b290fcee32a048707055e9b82aa310.tar.bz2
rails-82f1e19674b290fcee32a048707055e9b82aa310.zip
Feature envy detected, so move execute_command to Subscriptions
Diffstat (limited to 'lib/action_cable')
-rw-r--r--lib/action_cable/connection/base.rb23
-rw-r--r--lib/action_cable/connection/subscriptions.rb31
2 files changed, 27 insertions, 27 deletions
diff --git a/lib/action_cable/connection/base.rb b/lib/action_cable/connection/base.rb
index aa3eb6472d..7c79abcb89 100644
--- a/lib/action_cable/connection/base.rb
+++ b/lib/action_cable/connection/base.rb
@@ -39,7 +39,7 @@ module ActionCable
def receive(data_in_json)
if websocket_alive?
- execute_command decode_json(data_in_json)
+ subscriptions.execute_command ActiveSupport::JSON.decode(data_in_json)
else
logger.error "Received data without a live websocket (#{data.inspect})"
end
@@ -105,24 +105,6 @@ module ActionCable
end
- def execute_command(data)
- case data['command']
- when 'subscribe' then subscriptions.add data
- when 'unsubscribe' then subscriptions.remove data
- when 'message' then subscriptions.find(message['identifier']).perform_action decode_json(message['data'])
- else
- logger.error "Received unrecognized command in #{data.inspect}"
- end
- rescue Exception => e
- logger.error "Could not execute command from #{data.inspect})"
- log_exception(e)
- end
-
- def decode_json(json)
- ActiveSupport::JSON.decode json
- end
-
-
def respond_to_invalid_request
logger.info finished_request_message
[ 404, { 'Content-Type' => 'text/plain' }, [ 'Page not found' ] ]
@@ -157,8 +139,7 @@ module ActionCable
def log_exception(e)
- logger.error "There was an exception: #{e.class} - #{e.message}"
- logger.error e.backtrace.join("\n")
+ logger.error "Exception raised #{e.class} - #{e.message}: #{e.backtrace.first(5).join(" | ")}"
end
def log_tags
diff --git a/lib/action_cable/connection/subscriptions.rb b/lib/action_cable/connection/subscriptions.rb
index d6525b61c3..e0a3a133c5 100644
--- a/lib/action_cable/connection/subscriptions.rb
+++ b/lib/action_cable/connection/subscriptions.rb
@@ -6,6 +6,19 @@ module ActionCable
@subscriptions = {}
end
+ def execute_command(data)
+ case data['command']
+ when 'subscribe' then add data
+ when 'unsubscribe' then remove data
+ when 'message' then perform_action data
+ else
+ logger.error "Received unrecognized command in #{data.inspect}"
+ end
+ rescue Exception => e
+ logger.error "Could not execute command from #{data.inspect})"
+ connection.log_exception(e)
+ end
+
def add(data)
id_key = data['identifier']
id_options = ActiveSupport::JSON.decode(id_key).with_indifferent_access
@@ -27,14 +40,11 @@ module ActionCable
subscriptions.delete(data['identifier'])
end
- def find(identifier)
- if subscription = subscriptions[identifier]
- subscription
- else
- raise "Unable to find subscription with identifier: #{identifier}"
- end
+ def perform_action(data)
+ find(data).perform_action ActiveSupport::JSON.decode(data['data'])
end
+
def identifiers
subscriptions.keys
end
@@ -43,9 +53,18 @@ module ActionCable
subscriptions.each { |id, channel| channel.perform_disconnection }
end
+
private
attr_reader :connection, :subscriptions
delegate :logger, to: :connection
+
+ def find(data)
+ if subscription = subscriptions[data['identifier']]
+ subscription
+ else
+ raise "Unable to find subscription with identifier: #{identifier}"
+ end
+ end
end
end
end \ No newline at end of file