Sha256: d9dfd382f2fe50244ed1c1fb8c240e674086ceac8fd8cde7d858e87e5290696c

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

# encoding: utf-8

require 'has_guarded_handlers'
require 'active_support/core_ext/module/delegation'

module Adhearsion
  module Rayo
    class Client
      include HasGuardedHandlers

      attr_reader :connection, :component_registry

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

      # @param [Hash] options
      # @option options [Connection::XMPP] :connection The 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
end

require 'adhearsion/rayo/client/component_registry'

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
adhearsion-3.0.0.rc1 lib/adhearsion/rayo/client.rb
adhearsion-3.0.0.beta2 lib/adhearsion/rayo/client.rb
adhearsion-3.0.0.beta1 lib/adhearsion/rayo/client.rb