Class: Punchblock::Client

Inherits:
Object show all
Extended by:
ActiveSupport::Autoload
Includes:
HasGuardedHandlers
Defined in:
lib/punchblock/client.rb,
lib/punchblock/client/component_registry.rb

Defined Under Namespace

Classes: ComponentRegistry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :connection (Connection::XMPP)

    The Punchblock connection to use for this session



18
19
20
21
22
23
24
25
# File 'lib/punchblock/client.rb', line 18

def initialize(options = {})
  @event_queue = Queue.new
  @connection = options[:connection]
  @connection.event_handler = lambda { |event| self.handle_event event } if @connection
  register_initial_handlers
  @component_registry = ComponentRegistry.new
  @write_timeout = options[:write_timeout] || 3
end

Instance Attribute Details

#component_registryObject (readonly)

Returns the value of attribute component_registry.



11
12
13
# File 'lib/punchblock/client.rb', line 11

def component_registry
  @component_registry
end

#connectionObject (readonly)

Returns the value of attribute connection.



11
12
13
# File 'lib/punchblock/client.rb', line 11

def connection
  @connection
end

#event_queueObject (readonly)

Returns the value of attribute event_queue.



11
12
13
# File 'lib/punchblock/client.rb', line 11

def event_queue
  @event_queue
end

Instance Method Details

#execute_command(command, options = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/punchblock/client.rb', line 54

def execute_command(command, options = {})
  async = options.has_key?(:async) ? options.delete(:async) : true
  command.client = self
  if command.respond_to?(:register_handler)
    command.register_handler :internal do |event|
      trigger_handler :event, event
    end
  end
  connection.write command, options
  command.response(@write_timeout).tap { |result| raise result if result.is_a? Exception } unless async
end

#find_component_by_id(component_id) ⇒ Object



50
51
52
# File 'lib/punchblock/client.rb', line 50

def find_component_by_id(component_id)
  component_registry.find_by_id component_id
end

#handle_event(event) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/punchblock/client.rb', line 27

def handle_event(event)
  event.client = self
  if event.source
    event.source.add_event event
  else
    trigger_handler :event, event
  end
end

#register_component(component) ⇒ Object



46
47
48
# File 'lib/punchblock/client.rb', line 46

def register_component(component)
  component_registry << component
end

#register_event_handler(*guards, &block) ⇒ Object



36
37
38
# File 'lib/punchblock/client.rb', line 36

def register_event_handler(*guards, &block)
  register_handler :event, *guards, &block
end

#register_initial_handlersObject



40
41
42
43
44
# File 'lib/punchblock/client.rb', line 40

def register_initial_handlers
  register_handler_with_priority :event, -10 do |event|
    event_queue.push event
  end
end