class Blather::Roster
Local Roster
Takes care of adding/removing JIDs through the stream
Attributes
Public Class Methods
Create a new roster
@param [Blather::Stream] stream the stream the roster should use to update roster entries @param [Blather::Stanza::Iq::Roster] stanza a roster stanza used to preload the roster @return [Blather::Roster]
# File lib/blather/roster.rb, line 17 def initialize(stream, stanza = nil) @stream = stream @items = {} process(stanza) if stanza end
Private Class Methods
Creates a stripped jid
# File lib/blather/roster.rb, line 111 def self.key(jid) JID.new(jid).stripped.to_s end
Public Instance Methods
Pushes a JID
into the roster
@param [String, Blather::JID
, jid
] elem a JID
to add to the roster @return [self] @see push
# File lib/blather/roster.rb, line 42 def <<(elem) push elem self end
Get a RosterItem
by JID
@param [String, Blather::JID] jid the jid of the item to return @return [Blather::RosterItem, nil] the associated RosterItem
# File lib/blather/roster.rb, line 74 def [](jid) items[key(jid)] end
Remove a JID
from the roster and update the server
@param [String, Blather::JID] jid the JID
to remove from the roster
# File lib/blather/roster.rb, line 63 def delete(jid) @items.delete key(jid) item = Stanza::Iq::Roster::RosterItem.new(jid, nil, :remove) @stream.write Stanza::Iq::Roster.new(:set, item) end
Iterate over all RosterItems
@yield [Blather::RosterItem] yields each RosterItem
# File lib/blather/roster.rb, line 81 def each(&block) items.values.each &block end
A hash of items keyed by group
@return [Hash<group => Array<RosterItem>>]
# File lib/blather/roster.rb, line 102 def grouped @items.values.sort.inject(Hash.new{|h,k|h[k]=[]}) do |hash, item| item.groups.each { |group| hash[group] << item } hash end end
Get a duplicate of all RosterItems
@return [Array<Blather::RosterItem>] a duplicate of all RosterItems
# File lib/blather/roster.rb, line 88 def items @items.dup end
Number of items in the roster
@return [Integer] the number of items in the roster
# File lib/blather/roster.rb, line 95 def length @items.length end
Process any incoming stanzas and either adds or removes the corresponding RosterItem
@param [Blather::Stanza::Iq::Roster] stanza a roster stanza
# File lib/blather/roster.rb, line 27 def process(stanza) @version = stanza.version stanza.items.each do |i| case i.subscription when :remove then @items.delete(key(i.jid)) else @items[key(i.jid)] = RosterItem.new(i) end end end
Push a JID
into the roster and update the server
@param [String, Blather::JID
, jid
] elem a jid to add to the roster @param [true, false] send send the update over the wire @see Blather::JID
# File lib/blather/roster.rb, line 52 def push(elem, send = true) jid = elem.respond_to?(:jid) && elem.jid ? elem.jid : JID.new(elem) @items[key(jid)] = node = RosterItem.new(elem) @stream.write(node.to_stanza(:set)) if send end
Private Instance Methods
Instance method to wrap around the class method
# File lib/blather/roster.rb, line 116 def key(jid) self.class.key(jid) end