aboutsummaryrefslogtreecommitdiffstats
path: root/lib/action_cable/remote_connection.rb
blob: e2e2786dc1c7ebb002fa4408deab23168eb8a68a (plain) (blame)
1
2
3
4
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
module ActionCable
  class RemoteConnection
    class InvalidIdentifiersError < StandardError; end

    include Connection::Identification, Connection::InternalChannel

    def initialize(server, ids)
      @server = server
      set_identifier_instance_vars(ids)
    end

    def disconnect
      server.broadcast internal_redis_channel, type: 'disconnect'
    end

    def identifiers
      server.connection_identifiers
    end

    private
      attr_reader :server

      def set_identifier_instance_vars(ids)
        raise InvalidIdentifiersError unless valid_identifiers?(ids)
        ids.each { |k,v| instance_variable_set("@#{k}", v) }
      end

      def valid_identifiers?(ids)
        keys = ids.keys
        identifiers.all? { |id| keys.include?(id) }
      end
  end
end