Class: Jabber::XMPPStanza

Inherits:
XMPPElement show all
Defined in:
lib/gems/xmpp4r-0.4/lib/xmpp4r/xmppstanza.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 XMPPElement

class_for_name_xmlns, #clone, force_xmlns, force_xmlns?, import, #initialize, name_xmlns, name_xmlns_for_class, #parent=, #set_xml_lang, #typed_add, #xml_lang, #xml_lang=

Methods inherited from REXML::Element

#==, #delete_elements, #first_element, #first_element_text, #import, import, #replace_element_text, #typed_add

Constructor Details

This class inherits a constructor from Jabber::XMPPElement

Class Method Details

.answer(xmppstanza, import = true) ⇒ Object

Compose a response by doing the following:

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

  • Import xmppstanza 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!

xmppstanza
XMPPStanza

source

import
true or false

Copy attributes and children of source

result
XMPPStanza

answer stanza



30
31
32
33
34
35
36
37
38
39
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/xmppstanza.rb', line 30

def XMPPStanza.answer(xmppstanza, import=true)
  x = xmppstanza.class.new
  if import
    x.import(xmppstanza)
  end
  x.from = xmppstanza.to
  x.to = xmppstanza.from
  x.id = xmppstanza.id
  x
end

Instance Method Details

#answer(import = true) ⇒ Object

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

result
XMPPStanza

New constructed stanza



51
52
53
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/xmppstanza.rb', line 51

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

#errorObject

Return the first <error/> child



43
44
45
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/xmppstanza.rb', line 43

def error
  first_element('error')
end

#fromObject

get the from attribute

return
String

the element’s from attribute



91
92
93
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/xmppstanza.rb', line 91

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



99
100
101
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/xmppstanza.rb', line 99

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

#idObject

get the id attribute

return
String

the element’s id attribute



116
117
118
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/xmppstanza.rb', line 116

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

#id=(v) ⇒ Object

set the id attribute

v
String

the value id set



124
125
126
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/xmppstanza.rb', line 124

def id= (v)
  add_attribute('id', v.to_s)
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 }



59
60
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/xmppstanza.rb', line 59

def normalize
end

#set_from(v) ⇒ Object

set the from attribute (chaining-friendly)

v
String

the value from set



107
108
109
110
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/xmppstanza.rb', line 107

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



132
133
134
135
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/xmppstanza.rb', line 132

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

#set_to(v) ⇒ Object

set the to attribute (chaining-friendly)

v
String

the value to set



82
83
84
85
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/xmppstanza.rb', line 82

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



157
158
159
160
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/xmppstanza.rb', line 157

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

#toObject

get the to attribute

return
String

the element’s to attribute



66
67
68
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/xmppstanza.rb', line 66

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



74
75
76
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/xmppstanza.rb', line 74

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

#typeObject

get the type attribute

return
String

the element’s type attribute



141
142
143
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/xmppstanza.rb', line 141

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

#type=(v) ⇒ Object

set the type attribute

v
String

the value type set



149
150
151
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/xmppstanza.rb', line 149

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