lib/blather/stanza/iq/roster.rb in blather-0.2.1 vs lib/blather/stanza/iq/roster.rb in blather-0.2.2

- old
+ new

@@ -3,83 +3,83 @@ class Iq class Roster < Query register :roster, nil, 'jabber:iq:roster' + ## + # Any new items are added to the query def initialize(type = nil, item = nil) - super(type) + super type query << item if item end + ## + # Inherit the XMPPNode to create a proper Roster object. + # Creates RosterItem objects out of each roster item as well. def inherit(node) + # remove the current set of nodes items.each { |i| i.remove! } - @items = nil super + # transmogrify nodes into RosterItems items.each { |i| query << RosterItem.new(i); i.remove! } - @items = nil self end + ## + # Roster items def items - query.find('item')#.map { |g| RosterItem.new g } + items = query.find('//item', self.class.ns) + items = query.find('//query_ns:item', :query_ns => self.class.ns) if items.empty? + items.map { |i| RosterItem.new(i) } end class RosterItem < XMPPNode + ## + # [jid] may be either a JID or XMPPNode. + # [name] name alias of the given JID + # [subscription] subscription type + # [ask] ask subscription sub-state def initialize(jid = nil, name = nil, subscription = nil, ask = nil) - super('item') + super :item + if jid.is_a?(XML::Node) self.inherit jid else self.jid = jid self.name = name self.subscription = subscription self.ask = ask end end + ## + # Roster item's JID def jid - (j = attributes['jid']) ? JID.new(j) : nil + (j = attributes[:jid]) ? JID.new(j) : nil end + attribute_writer :jid - def jid=(jid) - attributes['jid'] = jid - end + attribute_accessor :name, :to_sym => false - def name - attributes['name'] - end + attribute_accessor :subscription, :ask - def name=(name) - attributes['name'] = name - end - - def subscription - attributes['subscription'].to_sym if attributes['subscription'] - end - - def subscription=(subscription) - attributes['subscription'] = subscription - end - - def ask - attributes['ask'].to_sym if attributes['ask'] - end - - def ask=(ask) - attributes['ask'] = ask - end - + ## + # The groups roster item belongs to def groups - @groups ||= find('group').map { |g| g.content } + find(:group).map { |g| g.content } end - def groups=(grps) - find('group').each { |g| g.remove! } - @groups = nil - - grps.uniq.each { |g| add_node XMPPNode.new('group', g.to_s) } if grps + ## + # Set the roster item's groups + # must be an array + def groups=(new_groups) + find(:group).each { |g| g.remove! } + new_groups.uniq.each { |g| self << XMPPNode.new(:group, g) } if new_groups end + ## + # Convert the roster item to a proper stanza all wrapped up + # This facilitates new subscriptions def to_stanza Roster.new(:set, self) end end #RosterItem end #Roster \ No newline at end of file