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 Attribute Summary collapse
-
#version ⇒ Object
readonly
Returns the value of attribute version.
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.
-
#length ⇒ Integer
Number of items in the roster.
-
#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
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
#version ⇒ Object (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
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
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
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
81 82 83 |
# File 'lib/blather/roster.rb', line 81 def each(&block) items.values.each &block end |
#grouped ⇒ Hash<group => Array<RosterItem>>
A hash of items keyed by group
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 |
#items ⇒ Array<Blather::RosterItem>
Get a duplicate of all RosterItems
88 89 90 |
# File 'lib/blather/roster.rb', line 88 def items @items.dup end |
#length ⇒ Integer
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
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
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 |