Class: Blather::Stanza

Inherits:
XMPPNode show all
Defined in:
lib/blather/stanza.rb,
lib/blather/stanza/x.rb,
lib/blather/stanza/iq.rb,
lib/blather/stanza/disco.rb,
lib/blather/stanza/pubsub.rb,
lib/blather/stanza/message.rb,
lib/blather/stanza/iq/query.rb,
lib/blather/stanza/iq/vcard.rb,
lib/blather/stanza/presence.rb,
lib/blather/stanza/iq/roster.rb,
lib/blather/stanza/iq/command.rb,
lib/blather/stanza/pubsub/event.rb,
lib/blather/stanza/pubsub/items.rb,
lib/blather/stanza/pubsub_owner.rb,
lib/blather/stanza/pubsub/create.rb,
lib/blather/stanza/pubsub/errors.rb,
lib/blather/stanza/pubsub/publish.rb,
lib/blather/stanza/pubsub/retract.rb,
lib/blather/stanza/presence/status.rb,
lib/blather/stanza/disco/disco_info.rb,
lib/blather/stanza/pubsub/subscribe.rb,
lib/blather/stanza/disco/disco_items.rb,
lib/blather/stanza/pubsub/unsubscribe.rb,
lib/blather/stanza/pubsub_owner/purge.rb,
lib/blather/stanza/pubsub/affiliations.rb,
lib/blather/stanza/pubsub/subscription.rb,
lib/blather/stanza/pubsub_owner/delete.rb,
lib/blather/stanza/pubsub/subscriptions.rb,
lib/blather/stanza/presence/subscription.rb

Overview

# Base XMPP Stanza

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

Direct Known Subclasses

Iq, Message, Presence

Defined Under Namespace

Classes: Disco, DiscoInfo, DiscoItems, Iq, Message, Presence, PubSub, PubSubErrors, PubSubItem, PubSubOwner, X

Constant Summary collapse

@@last_id =
0
@@handler_list =
[]

Constants inherited from XMPPNode

XMPPNode::BASE_NAMES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from XMPPNode

class_from_registration, #content_from, import, #inherit, #inherit_attrs, #inspect, #namespace=, #namespace_href, new, #nokogiri_namespace=, #read_attr, #read_content, #remove_child, #remove_children, #set_content_for, #to_stanza, #write_attr

Methods inherited from Nokogiri::XML::Node

#[]=, #attr_set, #find_first, #nokogiri_xpath, #xpath

Class Method Details

.handler_listArray<Symbol>

The handler stack for the current stanza class

Returns:

  • (Array<Symbol>)


36
37
38
# File 'lib/blather/stanza.rb', line 36

def self.handler_list
  @@handler_list
end

.next_idString

Helper method that creates a unique ID for stanzas

Returns:

  • (String)

    a new unique ID



43
44
45
46
# File 'lib/blather/stanza.rb', line 43

def self.next_id
  @@last_id += 1
  'blather%04x' % @@last_id
end

.register(handler, name = nil, ns = nil) ⇒ Object

Registers a callback onto the callback stack

Parameters:

  • handler (Symbol)

    the name of the handler

  • name (Symbol, String, nil) (defaults to: nil)

    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.

  • ns (String, nil) (defaults to: nil)

    the namespace of the stanza



24
25
26
27
28
29
30
31
# File 'lib/blather/stanza.rb', line 24

def self.register(handler, name = nil, ns = nil)
  @@handler_list << handler
  self.handler_hierarchy ||= [:stanza]
  self.handler_hierarchy.unshift handler

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

Instance Method Details

#as_error(name, type, text = nil, extras = []) ⇒ Blather::StanzaError

Create an error stanza from the current stanza

the error

Parameters:

  • name (String)

    the error name

  • type (<Blather::StanzaError::VALID_TYPES>)

    the error type

  • text (String, nil) (defaults to: nil)

    the error text

  • extras (Array<XML::Node>) (defaults to: [])

    an array of extra nodes to attach to

Returns:



135
136
137
# File 'lib/blather/stanza.rb', line 135

def as_error(name, type, text = nil, extras = [])
  StanzaError.new self, name, type, text, extras
end

#error?true, false

Check if the stanza is an error stanza

Returns:

  • (true, false)


51
52
53
# File 'lib/blather/stanza.rb', line 51

def error?
  self.type == :error
end

#fromBlather::JID?

Get the stanza’s from

Returns:



101
102
103
# File 'lib/blather/stanza.rb', line 101

def from
  JID.new(self[:from]) if self[:from]
end

#from=(from) ⇒ Object

Set the stanza’s from field

Parameters:

  • from (#to_s)

    the new JID for the from field



108
109
110
# File 'lib/blather/stanza.rb', line 108

def from=(from)
  write_attr :from, from
end

#idString?

Get the stanza’s ID

Returns:

  • (String, nil)


73
74
75
# File 'lib/blather/stanza.rb', line 73

def id
  read_attr :id
end

#id=(id) ⇒ Object

Set the stanza’s ID

Parameters:

  • id (#to_s)

    the new stanza ID



80
81
82
# File 'lib/blather/stanza.rb', line 80

def id=(id)
  write_attr :id, id
end

#replyBlather::Stanza

Creates a copy with to and from swapped

Returns:



58
59
60
# File 'lib/blather/stanza.rb', line 58

def reply
  self.dup.reply!
end

#reply!self

Swaps from and to

Returns:

  • (self)


65
66
67
68
# File 'lib/blather/stanza.rb', line 65

def reply!
  self.to, self.from = self.from, self.to
  self
end

#toBlather::JID?

Get the stanza’s to

Returns:



87
88
89
# File 'lib/blather/stanza.rb', line 87

def to
  JID.new(self[:to]) if self[:to]
end

#to=(to) ⇒ Object

Set the stanza’s to field

Parameters:

  • to (#to_s)

    the new JID for the to field



94
95
96
# File 'lib/blather/stanza.rb', line 94

def to=(to)
  write_attr :to, to
end

#typeSymbol?

Get the stanza’s type

Returns:

  • (Symbol, nil)


115
116
117
# File 'lib/blather/stanza.rb', line 115

def type
  read_attr :type, :to_sym
end

#type=(type) ⇒ Object

Set the stanza’s type

Parameters:

  • type (#to_s)

    the new stanza type



122
123
124
# File 'lib/blather/stanza.rb', line 122

def type=(type)
  write_attr :type, type
end