Class: ActionCable::Connection::Subscriptions
Do not use. This class is for internal use only.
| Relationships & Source Files | |
| Namespace Children | |
|
Exceptions:
| |
| Inherits: | Object |
| Defined in: | actioncable/lib/action_cable/connection/subscriptions.rb |
Overview
Collection class for all the channel subscriptions established on a given connection. Responsible for routing incoming commands that arrive on the connection to the proper channel.
Class Method Summary
- .new(connection) ⇒ Subscriptions constructor
Instance Attribute Summary
- #logger readonly
- #connection readonly private
- #subscriptions readonly private
Instance Method Summary
- #add(data)
- #execute_command(data)
- #identifiers
- #perform_action(data)
- #remove(data)
- #remove_subscription(subscription)
- #unsubscribe_from_all
- #filtered_command(data) private
- #filtered_identifier(id_key) private
- #filtered_json(json) private
- #find(data) private
- #parameter_filter private
- #subscription_from_identifier(id_key) private
Constructor Details
.new(connection) ⇒ Subscriptions
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 52
def initialize(connection) @connection = connection @subscriptions = {} end
Instance Attribute Details
#connection (readonly, private)
[ GitHub ]# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 113
attr_reader :connection, :subscriptions
#logger (readonly)
[ GitHub ]# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 114
delegate :logger, to: :connection
#subscriptions (readonly, private)
[ GitHub ]# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 113
attr_reader :connection, :subscriptions
Instance Method Details
#add(data)
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 67
def add(data) id_key = data["identifier"] raise MalformedCommandError, filtered_command(data) unless id_key.present? raise AlreadySubscribedError, filtered_identifier(id_key) if subscriptions.key?(id_key) subscription = subscription_from_identifier(id_key) if subscription subscriptions[id_key] = subscription subscription.subscribe_to_channel else = ActiveSupport::JSON.decode(id_key).with_indifferent_access raise ChannelNotFound, [:channel] end end
#execute_command(data)
[ GitHub ]# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 57
def execute_command(data) case data["command"] when "subscribe" then add data when "unsubscribe" then remove data when "message" then perform_action data else raise UnknownCommandError, data["command"] end end
#filtered_command(data) (private)
[ GitHub ]# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 133
def filtered_command(data) data = data.merge("data" => filtered_json(data["data"]) || ActiveSupport::ParameterFilter::FILTERED) if data.key?("data") parameter_filter.filter(data) rescue StandardError ActiveSupport::ParameterFilter::FILTERED end
#filtered_identifier(id_key) (private)
[ GitHub ]# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 129
def filtered_identifier(id_key) filtered_json(id_key)&.to_json || ActiveSupport::ParameterFilter::FILTERED end
#filtered_json(json) (private)
[ GitHub ]# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 140
def filtered_json(json) decoded = ActiveSupport::JSON.decode(json) parameter_filter.filter(decoded) if decoded.is_a?(Hash) rescue StandardError nil end
#find(data) (private)
[ GitHub ]# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 116
def find(data) subscriptions[data["identifier"]] end
#identifiers
[ GitHub ]# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 104
def identifiers subscriptions.keys end
#parameter_filter (private)
[ GitHub ]# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 147
def parameter_filter @parameter_filter ||= ActiveSupport::ParameterFilter.new(connection.config.filter_parameters) end
#perform_action(data)
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 98
def perform_action(data) subscription = find(data) raise UnknownSubscription.new(filtered_identifier(data["identifier"])) unless subscription subscription.perform_action ActiveSupport::JSON.decode(data["data"]) end
#remove(data)
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 85
def remove(data) raise MissingIdentifier unless data["identifier"].present? logger.info "Unsubscribing from channel: #{filtered_identifier(data["identifier"])}" subscription = find(data) remove_subscription(subscription) if subscription end
#remove_subscription(subscription)
[ GitHub ]# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 93
def remove_subscription(subscription) subscription.unsubscribe_from_channel subscriptions.delete(subscription.identifier) end
#subscription_from_identifier(id_key) (private)
[ GitHub ]# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 120
def subscription_from_identifier(id_key) = ActiveSupport::JSON.decode(id_key).with_indifferent_access subscription_klass = [:channel]&.safe_constantize if subscription_klass && ActionCable::Channel::Base > subscription_klass subscription_klass.new(connection, id_key, ) end end
#unsubscribe_from_all
[ GitHub ]# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 108
def unsubscribe_from_all subscriptions.each { |id, channel| remove_subscription(channel) } end