class Blather::Stanza

# Base XMPP Stanza

All stanzas inherit this class. It provides a set of methods and helpers common to all XMPP Stanzas

@handler :stanza

Attributes

handler_hierarchy[W]

Public Class Methods

handler_list() click to toggle source

The handler stack for the current stanza class

@return [Array<Symbol>]

# File lib/blather/stanza.rb, line 46
def self.handler_list
  @@handler_list
end
new(*args) click to toggle source
Calls superclass method
# File lib/blather/stanza.rb, line 34
def initialize(*args)
  super
  @handler_hierarchy = []
end
next_id() click to toggle source

Helper method that creates a unique ID for stanzas

@return [String] a new unique ID

# File lib/blather/stanza.rb, line 53
def self.next_id
  @@last_id += 1
  'blather%04x' % @@last_id
end
register(handler, name = nil, ns = nil) click to toggle source

Registers a callback onto the callback stack

@param [Symbol] handler the name of the handler @param [Symbol, String, nil] name the name of the first element in the

stanza. If nil the inherited name will be used. If that's nil the
handler name will be used.

@param [String, nil] ns the namespace of the stanza

Calls superclass method
# File lib/blather/stanza.rb, line 25
def self.register(handler, name = nil, ns = nil)
  @@handler_list << handler
  self.handler_hierarchy ||= [:stanza]
  self.handler_hierarchy = [handler] + self.handler_hierarchy

  name = name || self.registered_name || handler
  super name, ns
end

Public Instance Methods

as_error(name, type, text = nil, extras = []) click to toggle source

Create an error stanza from the current stanza

@param [String] name the error name @param [<Blather::StanzaError::VALID_TYPES>] type the error type @param [String, nil] text the error text @param [Array<XML::Node>] extras an array of extra nodes to attach to the error

@return [Blather::StanzaError]

# File lib/blather/stanza.rb, line 153
def as_error(name, type, text = nil, extras = [])
  StanzaError.new self, name, type, text, extras
end
error?() click to toggle source

Check if the stanza is an error stanza

@return [true, false]

# File lib/blather/stanza.rb, line 61
def error?
  self.type == :error
end
from() click to toggle source

Get the stanza's from

@return [Blather::JID, nil]

# File lib/blather/stanza.rb, line 119
def from
  JID.new(self[:from]) if self[:from]
end
from=(from) click to toggle source

Set the stanza's from field

@param [#to_s] from the new JID for the from field

# File lib/blather/stanza.rb, line 126
def from=(from)
  write_attr :from, from
end
handler_hierarchy() click to toggle source
# File lib/blather/stanza.rb, line 39
def handler_hierarchy
  @handler_hierarchy + self.class.handler_hierarchy
end
id() click to toggle source

Get the stanza's ID

@return [String, nil]

# File lib/blather/stanza.rb, line 91
def id
  read_attr :id
end
id=(id) click to toggle source

Set the stanza's ID

@param [#to_s] id the new stanza ID

# File lib/blather/stanza.rb, line 98
def id=(id)
  write_attr :id, id
end
reply(opts = {}) click to toggle source

Creates a copy with to and from swapped

@param [Hash] opts options to pass to reply! @option opts [Boolean] :remove_children Wether or not to remove child nodes when replying

@return [Blather::Stanza]

# File lib/blather/stanza.rb, line 71
def reply(opts = {})
  self.dup.reply! opts
end
reply!(opts = {}) click to toggle source

Swaps from and to

@param [Hash] opts Misc options @option opts [Boolean] :remove_children Wether or not to remove child nodes when replying

@return [self]

# File lib/blather/stanza.rb, line 81
def reply!(opts = {})
  opts = {:remove_children => false}.merge opts
  self.to, self.from = self.from, self.to
  self.children.remove if opts[:remove_children]
  self
end
to() click to toggle source

Get the stanza's to

@return [Blather::JID, nil]

# File lib/blather/stanza.rb, line 105
def to
  JID.new(self[:to]) if self[:to]
end
to=(to) click to toggle source

Set the stanza's to field

@param [#to_s] to the new JID for the to field

# File lib/blather/stanza.rb, line 112
def to=(to)
  write_attr :to, to
end
type() click to toggle source

Get the stanza's type

@return [Symbol, nil]

# File lib/blather/stanza.rb, line 133
def type
  read_attr :type, :to_sym
end
type=(type) click to toggle source

Set the stanza's type

@param [#to_s] type the new stanza type

# File lib/blather/stanza.rb, line 140
def type=(type)
  write_attr :type, type
end

Protected Instance Methods

reply_if_needed!() click to toggle source

@private

# File lib/blather/stanza.rb, line 159
def reply_if_needed!
  unless @reversed_endpoints
    reply!
    @reversed_endpoints = true
  end
  self
end