Class: Jabber::XMLStanza

Inherits:
REXML::Element show all
Defined in:
lib/xmpp4r/xmlstanza.rb

Overview

root class of all Jabber XML elements

Direct Known Subclasses

Iq, Message, Presence

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from REXML::Element

#delete_elements, #first_element, #first_element_text, #import, import, #replace_element_text

Class Method Details

.answer(xmlstanza, import = true) ⇒ Object

Compose a response by doing the following:

  • Create a new XMLStanza of the same subclass with the same element-name

  • Import xmlstanza if import is true

  • Swap 'to' and 'from'

  • Copy 'id'

  • Does not take care about the type

Attention: Be careful when answering to stanzas with type == :error - answering to an error may generate another error on the other side, which could be leading to a ping-pong effect quickly!

xmlstanza
XMLStanza

source

import
true or false

Copy attributes and children of source

result
XMLStanza

answer stanza



29
30
31
32
33
34
35
36
37
38
# File 'lib/xmpp4r/xmlstanza.rb', line 29

def XMLStanza.answer(xmlstanza, import=true)
  x = xmlstanza.class::new
  if import
    x.import(xmlstanza)
  end
  x.from = xmlstanza.to
  x.to = xmlstanza.from
  x.id = xmlstanza.id
  x
end

Instance Method Details

#answer(import = true) ⇒ Object

Compose a response of this XMLStanza (see XMLStanza.answer)

result
XMLStanza

New constructed stanza



63
64
65
# File 'lib/xmpp4r/xmlstanza.rb', line 63

def answer(import=true)
  XMLStanza.answer(self, import)
end

#errorObject

Return the first <error/> child



55
56
57
# File 'lib/xmpp4r/xmlstanza.rb', line 55

def error
  first_element('error')
end

#fromObject

get the from attribute

return
String

the element's from attribute



103
104
105
# File 'lib/xmpp4r/xmlstanza.rb', line 103

def from
  (a = attribute('from')).nil? ? a : JID::new(a.value)
end

#from=(v) ⇒ Object

set the from attribute

v
String

the value from set



111
112
113
# File 'lib/xmpp4r/xmlstanza.rb', line 111

def from= (v)
  add_attribute('from', v ? v.to_s : nil)
end

#idObject

get the id attribute

return
String

the element's id attribute



128
129
130
# File 'lib/xmpp4r/xmlstanza.rb', line 128

def id
  (a = attribute('id')).nil? ? a : a.value
end

#id=(v) ⇒ Object

set the id attribute

v
String

the value id set



136
137
138
# File 'lib/xmpp4r/xmlstanza.rb', line 136

def id= (v)
  add_attribute('id', v)
end

#normalizeObject

Makes some changes to the structure of an XML element to help it respect the specification. For example, in a message, we should have <subject/> < <body/> < { rest of tags }



71
72
# File 'lib/xmpp4r/xmlstanza.rb', line 71

def normalize
end

#set_from(v) ⇒ Object

set the from attribute (chaining-friendly)

v
String

the value from set



119
120
121
122
# File 'lib/xmpp4r/xmlstanza.rb', line 119

def set_from(v)
  add_attribute('from', v ? v.to_s : nil)
  self
end

#set_id(v) ⇒ Object

set the id attribute (chaining-friendly)

v
String

the value id set



144
145
146
147
# File 'lib/xmpp4r/xmlstanza.rb', line 144

def set_id(v)
  add_attribute('id', v)
  self
end

#set_to(v) ⇒ Object

set the to attribute (chaining-friendly)

v
String

the value to set



94
95
96
97
# File 'lib/xmpp4r/xmlstanza.rb', line 94

def set_to(v)
  self.to = v
  self
end

#set_type(v) ⇒ Object

set the type attribute (chaining-friendly)

v
String

the value type set



169
170
171
172
# File 'lib/xmpp4r/xmlstanza.rb', line 169

def set_type(v)
  add_attribute('type', v)
  self
end

#toObject

get the to attribute

return
String

the element's to attribute



78
79
80
# File 'lib/xmpp4r/xmlstanza.rb', line 78

def to
  (a = attribute('to')).nil? ? a : JID::new(a.value)
end

#to=(v) ⇒ Object

set the to attribute

v
String

the value to set



86
87
88
# File 'lib/xmpp4r/xmlstanza.rb', line 86

def to= (v)
  add_attribute('to', v ? v.to_s : nil)
end

#typeObject

get the type attribute

return
String

the element's type attribute



153
154
155
# File 'lib/xmpp4r/xmlstanza.rb', line 153

def type
  (a = attribute('type')).nil? ? a : a.value
end

#type=(v) ⇒ Object

set the type attribute

v
String

the value type set



161
162
163
# File 'lib/xmpp4r/xmlstanza.rb', line 161

def type= (v)
  add_attribute('type', v)
end

#typed_add(element) ⇒ Object

Add a sub-element

Will be converted to [Error] if named “error”

element
REXML::Element

to add



45
46
47
48
49
50
51
# File 'lib/xmpp4r/xmlstanza.rb', line 45

def typed_add(element)
  if element.kind_of?(REXML::Element) && (element.name == 'error')
    super(Error::import(element))
  else
    super(element)
  end
end