Sha256: 607dfd82e60b5edd71e92edd7ba4574834550143775dedce4a41e46d82fbb77a

Contents?: true

Size: 1.85 KB

Versions: 3

Compression:

Stored size: 1.85 KB

Contents

module Punchblock
  module Component
    extend ActiveSupport::Autoload

    autoload :Input
    autoload :Output
    autoload :Record
    autoload :Tropo

    InvalidActionError = Class.new StandardError

    class ComponentNode < CommandNode
      include HasGuardedHandlers

      attr_accessor :complete_event

      def initialize(*args)
        super
        @complete_event = FutureResource.new
        register_initial_handlers
      end

      def register_initial_handlers
        register_event_handler Event::Complete do |event|
          complete!
          complete_event.resource = event
          throw :pass
        end
      end

      def add_event(event)
        event.original_component = self
        trigger_handler :event, event
      end

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

      def write_action(action)
        connection.write call_id, action, component_id
      end

      def response=(other)
        super
        if other.is_a?(Blather::Stanza::Iq)
          ref = other.rayo_node
          if ref.is_a?(Ref)
            @component_id = ref.id
            @connection.record_command_id_for_iq_id @component_id, other.id
          end
        end
      end

      ##
      # Create an Rayo stop message
      #
      # @return [Stop] an Rayo stop message
      #
      def stop_action
        Stop.new :component_id => component_id, :call_id => call_id
      end

      ##
      # Sends an Rayo stop message for the current component
      #
      def stop!(options = {})
        raise InvalidActionError, "Cannot stop a #{self.class.name.split("::").last} that is not executing" unless executing?
        stop_action.tap { |action| write_action action }
      end
    end

    class Stop < CommandNode # :nodoc:
      register :stop, :core
    end
  end # Component
end # Punchblock

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
punchblock-0.4.3 lib/punchblock/component.rb
punchblock-0.4.2 lib/punchblock/component.rb
punchblock-0.4.1 lib/punchblock/component.rb