Class: Punchblock::RayoNode

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

Constant Summary collapse

@@registrations =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#call_idObject

Returns the value of attribute call_id.



10
11
12
# File 'lib/punchblock/rayo_node.rb', line 10

def call_id
  @call_id
end

#clientObject

Returns the value of attribute client.



10
11
12
# File 'lib/punchblock/rayo_node.rb', line 10

def client
  @client
end

#component_idObject

Returns the value of attribute component_id.



10
11
12
# File 'lib/punchblock/rayo_node.rb', line 10

def component_id
  @component_id
end

#connectionObject

Returns the value of attribute connection.



10
11
12
# File 'lib/punchblock/rayo_node.rb', line 10

def connection
  @connection
end

#original_componentObject

Returns the value of attribute original_component.



10
11
12
# File 'lib/punchblock/rayo_node.rb', line 10

def original_component
  @original_component
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



31
32
33
# File 'lib/punchblock/rayo_node.rb', line 31

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



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/punchblock/rayo_node.rb', line 41

def self.import(node, call_id = nil, component_id = nil)
  ns = (node.namespace.href if node.namespace)
  klass = class_from_registration(node.element_name, ns)
  event = if klass && klass != self
    klass.import node, call_id, component_id
  else
    new.inherit node
  end
  event.tap do |event|
    event.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



61
62
63
# File 'lib/punchblock/rayo_node.rb', line 61

def self.new(name = registered_name, doc = nil)
  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



20
21
22
23
24
# File 'lib/punchblock/rayo_node.rb', line 20

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)


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

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

#inspectObject Also known as: to_s



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

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

#inspect_attributesObject

:nodoc:



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

def inspect_attributes # :nodoc:
  [:call_id, :component_id]
end

#sourceRayoNode

Returns the original command issued that lead to this event.

Returns:

  • (RayoNode)

    the original command issued that lead to this event



80
81
82
83
# File 'lib/punchblock/rayo_node.rb', line 80

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