Sha256: 0c8a118af175921e54dd197ca1d43c0dbb25345f7cdcf48d67f73c6256373965

Contents?: true

Size: 1.74 KB

Versions: 10

Compression:

Stored size: 1.74 KB

Contents

# encoding: utf-8

module Punchblock
  class Client
    extend ActiveSupport::Autoload

    autoload :ComponentRegistry

    include HasGuardedHandlers

    attr_reader :connection, :event_queue, :component_registry

    delegate :run, :stop, :to => :connection

    # @param [Hash] options
    # @option options [Connection::XMPP] :connection The Punchblock connection to use for this session
    #
    def initialize(options = {})
      @event_queue = Queue.new
      @connection = options[:connection]
      @connection.event_handler = lambda { |event| self.handle_event event } if @connection
      @component_registry = ComponentRegistry.new
      @write_timeout = options[:write_timeout] || 3
    end

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

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

    def register_component(component)
      component_registry << component
    end

    def find_component_by_id(component_id)
      component_registry.find_by_id component_id
    end

    def delete_component_registration(component)
      component_registry.delete component
    end

    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
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
punchblock-1.9.4 lib/punchblock/client.rb
punchblock-1.9.3 lib/punchblock/client.rb
punchblock-1.9.2 lib/punchblock/client.rb
punchblock-1.9.1 lib/punchblock/client.rb
punchblock-1.9.0 lib/punchblock/client.rb
punchblock-1.8.2 lib/punchblock/client.rb
punchblock-1.8.1 lib/punchblock/client.rb
punchblock-1.8.0 lib/punchblock/client.rb
punchblock-1.7.1 lib/punchblock/client.rb
punchblock-1.7.0 lib/punchblock/client.rb