Class: ActionChannels::Channels::Base

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

Direct Known Subclasses

NewsChannel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Base

Returns a new instance of Base.



7
8
9
10
11
# File 'lib/action_channels/channels/base.rb', line 7

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.



4
5
6
# File 'lib/action_channels/channels/base.rb', line 4

def clients
  @clients
end

#message_senderObject

Returns the value of attribute message_sender.



5
6
7
# File 'lib/action_channels/channels/base.rb', line 5

def message_sender
  @message_sender
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/action_channels/channels/base.rb', line 4

def name
  @name
end

Instance Method Details

#add_client(client) ⇒ Object



17
18
19
20
21
22
# File 'lib/action_channels/channels/base.rb', line 17

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

#after_message_subscribe(subscriber, details) ⇒ Object



61
62
63
# File 'lib/action_channels/channels/base.rb', line 61

def after_message_subscribe(subscriber, details)
  # nothing
end

#after_message_unsubscribe(subscriber, details) ⇒ Object



65
66
67
# File 'lib/action_channels/channels/base.rb', line 65

def after_message_unsubscribe(subscriber, details)
  # nothing
end

#notify_all(message) ⇒ Object



31
32
33
34
35
# File 'lib/action_channels/channels/base.rb', line 31

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

#process_custom_message(message) ⇒ Object



57
58
59
# File 'lib/action_channels/channels/base.rb', line 57

def process_custom_message(message)
  # nothing
end

#process_message(message) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/action_channels/channels/base.rb', line 37

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

  if message.systemic?
    process_system_message message
  else
    process_custom_message message
  end
end

#process_system_message(message) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/action_channels/channels/base.rb', line 47

def process_system_message(message)
  case message.type
  when 'subscribe'
    on_subscribe message.author, message.details
  when 'unsubscribe'
    on_unsubscribe message.author, message.details
  else
  end
end

#remove_client(client) ⇒ Object



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

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



13
14
15
# File 'lib/action_channels/channels/base.rb', line 13

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