Class: Blather::Roster

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, stanza = nil) ⇒ Blather::Roster

Create a new roster

update roster entries the roster

Parameters:



17
18
19
20
21
# File 'lib/blather/roster.rb', line 17

def initialize(stream, stanza = nil)
  @stream = stream
  @items = {}
  process(stanza) if stanza
end

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version.



8
9
10
# File 'lib/blather/roster.rb', line 8

def version
  @version
end

Instance Method Details

#<<(elem) ⇒ self

Pushes a JID into the roster

Parameters:

  • elem (String, Blather::JID, #jid)

    a JID to add to the roster

Returns:

  • (self)

See Also:



42
43
44
45
# File 'lib/blather/roster.rb', line 42

def <<(elem)
  push elem
  self
end

#[](jid) ⇒ Blather::RosterItem?

Get a RosterItem by JID

Parameters:

  • jid (String, Blather::JID)

    the jid of the item to return

Returns:



74
75
76
# File 'lib/blather/roster.rb', line 74

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

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

Remove a JID from the roster and update the server

Parameters:

  • jid (String, Blather::JID)

    the JID to remove from the roster



63
64
65
66
67
# 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

#each {|Blather::RosterItem| ... } ⇒ Object

Iterate over all RosterItems

Yields:



81
82
83
# File 'lib/blather/roster.rb', line 81

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

#groupedHash<group => Array<RosterItem>>

A hash of items keyed by group

Returns:



102
103
104
105
106
107
# 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

#itemsArray<Blather::RosterItem>

Get a duplicate of all RosterItems

Returns:



88
89
90
# File 'lib/blather/roster.rb', line 88

def items
  @items.dup
end

#lengthInteger

Number of items in the roster

Returns:

  • (Integer)

    the number of items in the roster



95
96
97
# File 'lib/blather/roster.rb', line 95

def length
  @items.length
end

#process(stanza) ⇒ Object

Process any incoming stanzas and either adds or removes the corresponding RosterItem

Parameters:



27
28
29
30
31
32
33
34
35
# 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) ⇒ Object Also known as: add

Push a JID into the roster and update the server

Parameters:

  • elem (String, Blather::JID, #jid)

    a jid to add to the roster

  • send (true, false) (defaults to: true)

    send the update over the wire

See Also:



52
53
54
55
56
57
# 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