Class: Blather::Roster

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/blather/roster.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(stream, stanza = nil) ⇒ Roster

Returns a new instance of Roster.



9
10
11
12
13
# File 'lib/blather/roster.rb', line 9

def initialize(stream, stanza = nil)
  @stream = stream
  @items = {}
  stanza.items.each { |i| push i, false } if stanza
end

Instance Method Details

#<<(elem) ⇒ Object

Pushes a JID into the roster then returns self to allow for chaining



30
31
32
33
# File 'lib/blather/roster.rb', line 30

def <<(elem)
  push elem
  self
end

#[](jid) ⇒ Object

Get a RosterItem by JID



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

def [](jid)
  items[key(jid)]
end

#delete(jid) ⇒ Object Also known as: remove

Remove a JID from the roster Sends a remove query stanza to the server



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

def delete(jid)
  @items.delete key(jid)
  @stream.write Stanza::Iq::Roster.new(:set, Stanza::Iq::Roster::RosterItem.new(jid, nil, :remove))
end

#each(&block) ⇒ Object

Iterate over all RosterItems



64
65
66
# File 'lib/blather/roster.rb', line 64

def each(&block)
  items.each &block
end

#groupedObject

A hash of items keyed by group



76
77
78
79
80
81
# File 'lib/blather/roster.rb', line 76

def grouped
  self.inject(Hash.new{|h,k|h[k]=[]}) do |hash, item|
    item[1].groups.each { |group| hash[group] << item[1] }
    hash
  end
end

#itemsObject

Returns a duplicate of all RosterItems



70
71
72
# File 'lib/blather/roster.rb', line 70

def items
  @items.dup
end

#process(stanza) ⇒ Object

Process any incoming stanzas adn either add or remove the corresponding RosterItem



18
19
20
21
22
23
24
25
# File 'lib/blather/roster.rb', line 18

def process(stanza)
  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) ⇒ Object Also known as: add

Push a JID into the roster Will send the new item to the server unless overridden by calling #push(elem, false)



39
40
41
42
43
44
# File 'lib/blather/roster.rb', line 39

def push(elem, send = true)
  jid = elem.respond_to?(:jid) ? elem.jid : JID.new(elem)
  @items[key(jid)] = node = RosterItem.new(elem)

  @stream.write(node.to_stanza(:set)) if send
end