Class: Jabber::Roster::IqQueryRoster

Inherits:
IqQuery show all
Defined in:
lib/xmpp4r/roster/iq/roster.rb

Overview

Class for handling roster updates

You must do 'client.send(Iq.new_rosterget)' or else you will have nothing to put in receive_iq()

You must require 'xmpp4r/rosterquery' to use this class as its functionality is not needed for a working XMPP implementation. This will make [IqQuery] convert all Queries with namespace 'jabber:iq:roster' to [IqQueryRoster]

This <query/> contains multiple <item/> children. See RosterItem.

Instance Method Summary collapse

Methods inherited from IqQuery

add_namespaceclass, import

Methods inherited from REXML::Element

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

Constructor Details

#initializeIqQueryRoster

Create a new <query xmlns='jabber:iq:roster'/>

stream
Stream

Stream to handle


25
26
27
28
# File 'lib/xmpp4r/roster/iq/roster.rb', line 25

def initialize
  super
  add_namespace('jabber:iq:roster')
end

Instance Method Details

#[](jid) ⇒ Object

Get roster item by JID

jid
JID

or [Nil]

result
RosterItem

60
61
62
63
64
65
# File 'lib/xmpp4r/roster/iq/roster.rb', line 60

def [](jid)
  each { |item|
    return(item) if item.jid == jid
  }
  nil
end

#each(&block) ⇒ Object

Iterate through all items

&block

Yield for every [RosterItem]


49
50
51
52
53
54
# File 'lib/xmpp4r/roster/iq/roster.rb', line 49

def each(&block)
  each_element { |item|
    # XPath won't work here as it's missing a prefix...
    yield(item) if item.kind_of?(RosterItem)
  }
end

#inspectObject

Output for ā€œpā€

JIDs of all contained [RosterItem] elements are joined with a comma

result
String

96
97
98
99
# File 'lib/xmpp4r/roster/iq/roster.rb', line 96

def inspect
  jids = to_a.collect { |item| item.jid.inspect }
  jids.join(', ')
end

#receive_iq(iq, filter = true) ⇒ Object

Update roster by <iq/> stanza (to be fed by an iq_callback)

iq
Iq

Containing new roster

filter
Boolean

If false import non-roster-like results too


83
84
85
86
87
88
89
# File 'lib/xmpp4r/roster/iq/roster.rb', line 83

def receive_iq(iq, filter=true)
  if filter && (((iq.type != :set) && (iq.type != :result)) || (iq.queryns != 'jabber:iq:roster'))
    return
  end
  
  import(iq.query)
end

#to_aObject

Get all items

result
Array

of [RosterItem]


70
71
72
73
74
75
76
# File 'lib/xmpp4r/roster/iq/roster.rb', line 70

def to_a
  a = []
  each { |item|
    a.push(item)
  }
  a
end

#typed_add(element) ⇒ Object

Add an element to the roster

Converts <item/> elements to RosterItem

Previous RosterItems with the same JID will not be deleted!


37
38
39
40
41
42
43
44
# File 'lib/xmpp4r/roster/iq/roster.rb', line 37

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