Class: Punchblock::Client

Inherits:
Object
  • 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



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

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.



9
10
11
# File 'lib/punchblock/client.rb', line 9

def component_registry
  @component_registry
end

#connectionObject (readonly)

Returns the value of attribute connection.



9
10
11
# File 'lib/punchblock/client.rb', line 9

def connection
  @connection
end

#event_queueObject (readonly)

Returns the value of attribute event_queue.



9
10
11
# File 'lib/punchblock/client.rb', line 9

def event_queue
  @event_queue
end

Instance Method Details

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



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

def execute_command(command, options = {})
  async = options.has_key?(:async) ? options.delete(:async) : true
  command.client = self
  if command.respond_to?(:register_event_handler)
    command.register_event_handler 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



48
49
50
# File 'lib/punchblock/client.rb', line 48

def find_component_by_id(component_id)
  component_registry.find_by_id component_id
end

#handle_event(event) ⇒ Object



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

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



44
45
46
# File 'lib/punchblock/client.rb', line 44

def register_component(component)
  component_registry << component
end

#register_event_handler(*guards, &block) ⇒ Object



34
35
36
# File 'lib/punchblock/client.rb', line 34

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

#register_initial_handlersObject



38
39
40
41
42
# File 'lib/punchblock/client.rb', line 38

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