Sha256: 80bf48b5d58a767fe81eeb6c198d5419797d68f1cbdc4aaab69c8cad56d0b285

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

# encoding: utf-8

module Punchblock
  class Client
    extend ActiveSupport::Autoload

    autoload :ComponentRegistry

    include HasGuardedHandlers

    attr_reader :connection, :component_registry

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

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

    def handle_event(event)
      event.client = self
      if event.source
        event.source.add_event event
      else
        trigger_handler :event, 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_uri(uri)
      component_registry.find_by_uri uri
    end

    def delete_component_registration(component)
      component_registry.delete component
    end

    def execute_command(command, options = {})
      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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
punchblock-2.3.1 lib/punchblock/client.rb
punchblock-2.3.0 lib/punchblock/client.rb