Class: ActionChannels::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/action_channels/channel.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Channel

Returns a new instance of Channel.



14
15
16
17
18
# File 'lib/action_channels/channel.rb', line 14

def initialize(attrs)
  @name = attrs.fetch(:name)
  @clients = Set.new
  @message_sender = attrs.fetch :message_sender, MessageSenders::WebSocket.new
end

Instance Attribute Details

#clientsObject (readonly)

Returns the value of attribute clients.



11
12
13
# File 'lib/action_channels/channel.rb', line 11

def clients
  @clients
end

#message_senderObject

Returns the value of attribute message_sender.



12
13
14
# File 'lib/action_channels/channel.rb', line 12

def message_sender
  @message_sender
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/action_channels/channel.rb', line 11

def name
  @name
end

Class Method Details

.input_message_typesObject



3
4
5
# File 'lib/action_channels/channel.rb', line 3

def self.input_message_types
  %w(subscribe unsubscribe publish)
end

.output_message_typesObject



7
8
9
# File 'lib/action_channels/channel.rb', line 7

def self.output_message_types
  %w(subscribed unsubscribed published news invalid_message)
end

Instance Method Details

#add_client(client) ⇒ Object



24
25
26
27
28
29
# File 'lib/action_channels/channel.rb', line 24

def add_client(client)
  clients << client

  ActionChannels.logger.info "The channel ##{self.name} added a client"
  ActionChannels.logger.debug "Count of client of channel ##{self.name} is #{self.clients.count}."
end

#notify_all(message) ⇒ Object



38
39
40
41
42
# File 'lib/action_channels/channel.rb', line 38

def notify_all(message)
  clients.each do |client|
    send_message client, message
  end
end

#process_message(message) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/action_channels/channel.rb', line 44

def process_message(message)
  ActionChannels.logger.debug "The channel ##{self.name} received a message #{message.inspect}"

  case message.type
  when 'subscribe'
    on_subscribe message.author, message.details
  when 'unsubscribe'
    on_unsubscribe message.author, message.details
  when 'publish'
    on_publish message.author, message.details
  else
    on_unknown_type_message message.author, message.type
  end
end

#remove_client(client) ⇒ Object



31
32
33
34
35
36
# File 'lib/action_channels/channel.rb', line 31

def remove_client(client)
  clients.delete client

  ActionChannels.logger.info "The channel ##{self.name} removed a client"
  ActionChannels.logger.debug "Count of client of channel ##{self.name} is #{self.clients.count}."
end

#send_message(receiver, message) ⇒ Object



20
21
22
# File 'lib/action_channels/channel.rb', line 20

def send_message(receiver, message)
  message_sender.do_send(receiver, message)
end