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

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



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
# File 'lib/blather/xmpp_node.rb', line 71

def self.decorator_modules
  [self::InstanceMethods]
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



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



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



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



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



75
76
77
78
79
80
81
82
83
84
# File 'lib/blather/xmpp_node.rb', line 75

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

    @handler_hierarchy.unshift decorator.handler_hierarchy.first
  end
  self
end

#to_stanzaObject

Turn the object into a proper stanza



89
90
91
# File 'lib/blather/xmpp_node.rb', line 89

def to_stanza
  self.class.import self
end