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

- (Client) initialize(options = {})

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

- (Object) component_registry (readonly)

Returns the value of attribute component_registry



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

def component_registry
  @component_registry
end

- (Object) connection (readonly)

Returns the value of attribute connection



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

def connection
  @connection
end

- (Object) event_queue (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

- (Object) execute_command(command, options = {})



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

- (Object) find_component_by_id(component_id)



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

- (Object) handle_event(event)



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

- (Object) register_component(component)



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

def register_component(component)
  component_registry << component
end

- (Object) register_event_handler(*guards, &block)



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

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

- (Object) register_initial_handlers



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