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 Method Summary collapse

Constructor Details

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

Create a new roster

update roster entries the roster

Parameters:

  • stream (Blather::Stream)

    the stream the roster should use to

  • stanza (Blather::Stanza::Roster) (defaults to: nil)

    a roster stanza used to preload



15
16
17
18
19
# File 'lib/blather/roster.rb', line 15

def initialize(stream, stanza = nil)
  @stream = stream
  @items = {}
  stanza.items.each { |i| push i, false } if stanza
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:



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

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:



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

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



60
61
62
63
64
# File 'lib/blather/roster.rb', line 60

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:



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

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

#groupedHash<group => Array<RosterItem>>

A hash of items keyed by group

Returns:



99
100
101
102
103
104
# File 'lib/blather/roster.rb', line 99

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:



85
86
87
# File 'lib/blather/roster.rb', line 85

def items
  @items.dup
end

#lengthInteger

Number of items in the roster

Returns:

  • (Integer)

    the number of items in the roster



92
93
94
# File 'lib/blather/roster.rb', line 92

def length
  @items.length
end

#process(stanza) ⇒ Object

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

Parameters:

  • stanza (Blather::Stanza::Roster)

    a roster stanza



25
26
27
28
29
30
31
32
# File 'lib/blather/roster.rb', line 25

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 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:



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

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