Class: Blather::Roster
- Inherits:
-
Object
- Object
- Blather::Roster
- Includes:
- Enumerable
- Defined in:
- lib/blather/roster.rb
Overview
Local Roster Takes care of adding/removing JIDs through the stream
Instance Method Summary collapse
-
#<<(elem) ⇒ self
Pushes a JID into the roster.
-
#[](jid) ⇒ Blather::RosterItem?
Get a RosterItem by JID.
-
#delete(jid) ⇒ Object
(also: #remove)
Remove a JID from the roster and update the server.
-
#each {|Blather::RosterItem| ... } ⇒ Object
Iterate over all RosterItems.
-
#grouped ⇒ Hash<group => Array<RosterItem>>
A hash of items keyed by group.
-
#initialize(stream, stanza = nil) ⇒ Blather::Roster
constructor
Create a new roster.
-
#items ⇒ Array<Blather::RosterItem>
Get a duplicate of all RosterItems.
-
#process(stanza) ⇒ Object
Process any incoming stanzas and either adds or removes the corresponding RosterItem.
-
#push(elem, send = true) ⇒ Object
(also: #add)
Push a JID into the roster and update the server.
Constructor Details
#initialize(stream, stanza = nil) ⇒ Blather::Roster
Create a new roster
update roster entries the roster
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
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
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
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
78 79 80 |
# File 'lib/blather/roster.rb', line 78 def each(&block) items.each &block end |
#grouped ⇒ Hash<group => Array<RosterItem>>
A hash of items keyed by group
92 93 94 95 96 97 |
# File 'lib/blather/roster.rb', line 92 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 ⇒ Array<Blather::RosterItem>
Get a duplicate of all RosterItems
85 86 87 |
# File 'lib/blather/roster.rb', line 85 def items @items.dup end |
#process(stanza) ⇒ Object
Process any incoming stanzas and either adds or removes the corresponding RosterItem
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
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 |