Class: Rubyfox::Client::ExtensionHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyfox/client/extension_handler.rb

Defined Under Namespace

Classes: Request

Instance Method Summary collapse

Constructor Details

#initialize(event_handler) ⇒ ExtensionHandler

Returns a new instance of ExtensionHandler.



6
7
8
9
# File 'lib/rubyfox/client/extension_handler.rb', line 6

def initialize(event_handler)
  @handler        = Hash.new { |hash, type| hash[type] = [] }
  @event_handler  = event_handler
end

Instance Method Details

#add(*names, &block) ⇒ Object



22
23
24
25
26
# File 'lib/rubyfox/client/extension_handler.rb', line 22

def add(*names, &block)
  names.each do |name|
    @handler[name.to_s] << block
  end
end

#dispatch(request) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/rubyfox/client/extension_handler.rb', line 34

def dispatch(request)
  command = request.command

  @handler[command].each do |handler|
    handler.call(request)
  end
end

#registerObject



11
12
13
14
15
16
# File 'lib/rubyfox/client/extension_handler.rb', line 11

def register
  @event_handler.add(:extension_response) do |event|
    request = Request.new(*event.arguments.values_at("cmd", "params", "sourceRoom", "packetId"))
    dispatch(request)
  end
end

#remove(*names) ⇒ Object



28
29
30
31
32
# File 'lib/rubyfox/client/extension_handler.rb', line 28

def remove(*names)
  names.each do |name|
    @handler[name.to_s].clear
  end
end

#unregisterObject



18
19
20
# File 'lib/rubyfox/client/extension_handler.rb', line 18

def unregister
  @event_handler.remove(:extension_response)
end