class Blather::Roster

Local Roster Takes care of adding/removing JIDs through the stream

Attributes

version[R]

Public Class Methods

new(stream, stanza = nil) click to toggle source

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

key(jid) click to toggle source

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

<<(elem) click to toggle source

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
[](jid) click to toggle source

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
add(elem, send = true)
Alias for: push
delete(jid) click to toggle source

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
Also aliased as: remove
each(&block) click to toggle source

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
grouped() click to toggle source

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
items() click to toggle source

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
length() click to toggle source

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(stanza) click to toggle source

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(elem, send = true) click to toggle source

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
Also aliased as: add
remove(jid)
Alias for: delete

Private Instance Methods

key(jid) click to toggle source

Instance method to wrap around the class method

# File lib/blather/roster.rb, line 116
def key(jid)
  self.class.key(jid)
end