Class: Punchblock::RayoNode

Inherits:
Niceogiri::XML::Node
  • Object
show all
Defined in:
lib/punchblock/rayo_node.rb

Constant Summary collapse

InvalidNodeError =
Class.new Punchblock::Error
@@registrations =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientObject

Returns the value of attribute client.



14
15
16
# File 'lib/punchblock/rayo_node.rb', line 14

def client
  @client
end

#component_idObject

Returns the value of attribute component_id.



14
15
16
# File 'lib/punchblock/rayo_node.rb', line 14

def component_id
  @component_id
end

#connectionObject

Returns the value of attribute connection.



14
15
16
# File 'lib/punchblock/rayo_node.rb', line 14

def connection
  @connection
end

#domainObject

Returns the value of attribute domain.



14
15
16
# File 'lib/punchblock/rayo_node.rb', line 14

def domain
  @domain
end

#original_componentObject

Returns the value of attribute original_component.



14
15
16
# File 'lib/punchblock/rayo_node.rb', line 14

def original_component
  @original_component
end

#target_call_idObject

Returns the value of attribute target_call_id.



14
15
16
# File 'lib/punchblock/rayo_node.rb', line 14

def target_call_id
  @target_call_id
end

#target_mixer_nameObject

Returns the value of attribute target_mixer_name.



14
15
16
# File 'lib/punchblock/rayo_node.rb', line 14

def target_mixer_name
  @target_mixer_name
end

Class Method Details

.class_from_registration(name, ns = nil) ⇒ Class?

Find the class to use given the name and namespace of a stanza

Parameters:

  • name (#to_s)

    the name to lookup

  • xmlns (String, nil)

    the namespace the node belongs to

Returns:

  • (Class, nil)

    the class appropriate for the name/ns combination



35
36
37
# File 'lib/punchblock/rayo_node.rb', line 35

def self.class_from_registration(name, ns = nil)
  @@registrations[[name.to_s, ns]]
end

.import(node, call_id = nil, component_id = nil) ⇒ Object

Import an XML::Node to the appropriate class

Looks up the class the node should be then creates it based on the elements of the XML::Node

Parameters:

  • node (XML::Node)

    the node to import

Returns:

  • the appropriate object based on the node name and namespace



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/punchblock/rayo_node.rb', line 45

def self.import(node, call_id = nil, component_id = nil)
  node = Nokogiri::XML(node.to_xml).root if Punchblock.jruby?
  ns = (node.namespace.href if node.namespace)
  klass = class_from_registration(node.element_name, ns)
  if klass && klass != self
    klass.import node, call_id, component_id
  else
    new.inherit node
  end.tap do |event|
    event.target_call_id = call_id
    event.component_id = component_id
  end
end

.new(name = registered_name, doc = nil) ⇒ Object

Create a new Node object

not provided one will be created

Parameters:

  • name (String, nil) (defaults to: registered_name)

    the element name

  • doc (XML::Document, nil) (defaults to: nil)

    the document to attach the node to. If

Returns:

  • a new object with the registered name and namespace

Raises:



65
66
67
68
# File 'lib/punchblock/rayo_node.rb', line 65

def self.new(name = registered_name, doc = nil)
  raise InvalidNodeError, "Trying to create a new #{self} with no name" unless name
  super name, doc, registered_ns
end

.register(name, ns = nil) ⇒ Object

Register a new stanza class to a name and/or namespace

This registers a namespace that is used when looking up the class name of the object to instantiate when a new stanza is received

Parameters:

  • name (#to_s)

    the name of the node

  • ns (String, nil) (defaults to: nil)

    the namespace the node belongs to



24
25
26
27
28
# File 'lib/punchblock/rayo_node.rb', line 24

def self.register(name, ns = nil)
  self.registered_name = name.to_s
  self.registered_ns = ns.is_a?(Symbol) ? RAYO_NAMESPACES[ns] : ns
  @@registrations[[self.registered_name, self.registered_ns]] = self
end

Instance Method Details

#eql?(o, *fields) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/punchblock/rayo_node.rb', line 78

def eql?(o, *fields)
  super o, *(fields + inspect_attributes)
end

#inspectObject Also known as: to_s



74
75
76
# File 'lib/punchblock/rayo_node.rb', line 74

def inspect
  "#<#{self.class} #{inspect_attributes.map { |c| "#{c}=#{self.__send__(c).inspect rescue nil}" }.compact * ', '}>"
end

#inspect_attributesObject

:nodoc:



70
71
72
# File 'lib/punchblock/rayo_node.rb', line 70

def inspect_attributes # :nodoc:
  [:target_call_id, :component_id, :target_mixer_name]
end

#sourceRayoNode

Returns the original command issued that lead to this event.

Returns:

  • (RayoNode)

    the original command issued that lead to this event



85
86
87
88
# File 'lib/punchblock/rayo_node.rb', line 85

def source
  @source ||= client.find_component_by_id component_id if client && component_id
  @source ||= original_component
end