Class: Blather::XMPPNode
- Inherits:
-
Niceogiri::XML::Node
- Object
- Niceogiri::XML::Node
- Blather::XMPPNode
- Defined in:
- lib/blather/xmpp_node.rb
Overview
Base XML Node All XML classes subclass XMPPNode it allows the addition of helpers
Direct Known Subclasses
Stanza, Stanza::DiscoInfo::Feature, Stanza::DiscoInfo::Identity, Stanza::DiscoItems::Item, Stanza::Iq::Roster::RosterItem, Stanza::Iq::S5b::StreamHost, Stanza::Iq::S5b::StreamHostUsed, Stanza::Iq::Si::Si, Stanza::Iq::Si::Si::Feature, Stanza::Iq::Si::Si::File, Stanza::Iq::Si::Si::Range, Stanza::Iq::Vcard::Vcard, Stanza::Message::Delay, Stanza::Message::MUCUser::InviteBase, Stanza::Presence::MUCUser::Item, Stanza::Presence::MUCUser::Status, Stanza::PubSubItem, Stanza::X, Stanza::X::Field, Stanza::X::Field::Option
Constant Summary
- BASE_NAMES =
%w[presence message iq].freeze
- @@registrations =
{}
Class Method Summary (collapse)
-
+ (Class?) class_from_registration(name, ns = nil)
Find the class to use given the name and namespace of a stanza.
- + (Object) decorator_modules
-
+ (Object) import(node, *decorators)
Import an XML::Node to the appropriate class.
-
+ (Object) new(name = registered_name, doc = nil)
Create a new Node object.
-
+ (Object) parse(string)
Parse a string as XML and import to the appropriate class.
-
+ (Object) register(name, ns = nil)
Register a new stanza class to a name and/or namespace.
Instance Method Summary (collapse)
- - (Object) decorate(*decorators)
-
- (Object) to_stanza
Turn the object into a proper stanza.
Class Method Details
+ (Class?) class_from_registration(name, ns = nil)
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 |
+ (Object) decorator_modules
71 72 73 |
# File 'lib/blather/xmpp_node.rb', line 71 def self.decorator_modules [self::InstanceMethods] end |
+ (Object) import(node, *decorators)
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 |
+ (Object) new(name = registered_name, doc = nil)
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 |
+ (Object) parse(string)
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 |
+ (Object) register(name, ns = nil)
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
- (Object) decorate(*decorators)
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 |
- (Object) to_stanza
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 |