Class: Blather::XMPPNode

Inherits:
Niceogiri::XML::Node
  • Object
show all
Defined in:
lib/blather/xmpp_node.rb

Overview

Base XML Node All XML classes subclass XMPPNode it allows the addition of helpers

Constant Summary collapse

BASE_NAMES =
%w[presence message iq].freeze
@@registrations =
{}

Class Method Summary collapse

Instance Method Summary collapse

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



33
34
35
# File 'lib/blather/xmpp_node.rb', line 33

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

.decorator_modulesObject



71
72
73
74
75
76
77
# File 'lib/blather/xmpp_node.rb', line 71

def self.decorator_modules
  if self.const_defined?(:InstanceMethods)
    [self::InstanceMethods]
  else
    []
  end
end

.import(node, *decorators) ⇒ 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



43
44
45
46
47
48
49
50
51
# File 'lib/blather/xmpp_node.rb', line 43

def self.import(node, *decorators)
  ns = (node.namespace.href if node.namespace)
  klass = class_from_registration(node.element_name, ns)
  if klass && klass != self
    klass.import(node, *decorators)
  else
    new(node.element_name).decorate(*decorators).inherit(node)
  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



67
68
69
# File 'lib/blather/xmpp_node.rb', line 67

def self.new(name = registered_name, doc = nil)
  super name, doc, BASE_NAMES.include?(name.to_s) ? nil : self.registered_ns
end

.parse(string) ⇒ Object

Parse a string as XML and import to the appropriate class

Parameters:

  • string (String)

    the string to parse

Returns:

  • the appropriate object based on the node name and namespace



57
58
59
# File 'lib/blather/xmpp_node.rb', line 57

def self.parse(string)
  import Nokogiri::XML(string).root
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



22
23
24
25
26
# File 'lib/blather/xmpp_node.rb', line 22

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

Instance Method Details

#decorate(*decorators) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/blather/xmpp_node.rb', line 79

def decorate(*decorators)
  decorators.each do |decorator|
    decorator.decorator_modules.each do |mod|
      extend mod
    end

    @handler_hierarchy.unshift decorator.handler_hierarchy.first if decorator.respond_to?(:handler_hierarchy)
  end
  self
end

#to_stanzaObject

Turn the object into a proper stanza

Returns:

  • a stanza object



93
94
95
# File 'lib/blather/xmpp_node.rb', line 93

def to_stanza
  self.class.import self
end