class Blather::RosterItem
RosterItems hold internal representations of the user's roster including each JID's status.
Constants
- VALID_SUBSCRIPTION_TYPES
@private
Attributes
Public Class Methods
@private
# File lib/blather/roster_item.rb, line 17 def self.new(item) return item if item.is_a?(self) super end
Create a new RosterItem
@overload initialize(jid)
Create a new RosterItem based on a JID @param [Blather::JID] jid the JID object
@overload initialize(jid)
Create a new RosterItem based on a JID string @param [String] jid a JID string
@overload initialize(node)
Create a new RosterItem based on a stanza @param [Blather::Stanza::Iq::Roster::RosterItem] node a RosterItem stanza
@return [Blather::RosterItem] the new RosterItem
# File lib/blather/roster_item.rb, line 35 def initialize(item) @statuses = [] @groups = [] case item when JID self.jid = item.stripped when String self.jid = JID.new(item).stripped when XMPPNode self.jid = JID.new(item[:jid]).stripped self.name = item[:name] self.subscription = item[:subscription] self.ask = item[:ask] item.groups.each { |g| @groups << g } end @groups = [nil] if @groups.empty? end
Public Instance Methods
@private
# File lib/blather/roster_item.rb, line 141 def ==(o) eql?(o) end
Set the ask value
@param [nil, :subscribe] ask the new ask
# File lib/blather/roster_item.rb, line 84 def ask=(ask) if ask && (ask = ask.to_sym) != :subscribe raise ArgumentError, "Invalid Type (#{ask}), can only be :subscribe" end @ask = ask ? ask : nil end
Compare two RosterItem
objects by name, type and category @param [RosterItem] o the Identity object to compare against @return [true, false]
# File lib/blather/roster_item.rb, line 134 def eql?(o, *fields) o.is_a?(self.class) && o.jid == self.jid && o.groups == self.groups end
Set the jid
@param [String, Blather::JID] jid the new jid @see Blather::JID
# File lib/blather/roster_item.rb, line 59 def jid=(jid) @jid = JID.new(jid).stripped end
The status with the highest priority
@param [String, nil] resource the resource to get the status of
# File lib/blather/roster_item.rb, line 103 def status(resource = nil) top = if resource @statuses.detect { |s| s.from.resource == resource } else @statuses.last end end
Set the status then sorts them according to priority
@param [Blather::Stanza::Status] the new status
# File lib/blather/roster_item.rb, line 94 def status=(presence) @statuses.delete_if { |s| s.from == presence.from || s.state == :unavailable } @statuses << presence @statuses.sort! end
Get the current subscription
@return [:both, :from, :none, :remove, :to]
# File lib/blather/roster_item.rb, line 77 def subscription @subscription || :none end
Set the subscription Ensures it is one of VALID_SUBSCRIPTION_TYPES
@param [#to_sym] sub the new subscription
# File lib/blather/roster_item.rb, line 67 def subscription=(sub) if sub && !VALID_SUBSCRIPTION_TYPES.include?(sub = sub.to_sym) raise ArgumentError, "Invalid Type (#{sub}), use: #{VALID_SUBSCRIPTION_TYPES*' '}" end @subscription = sub ? sub : :none end
# File lib/blather/roster_item.rb, line 119 def to_node Stanza::Iq::Roster::RosterItem.new jid, name, subscription, ask, groups end
Translate the RosterItem
into a proper stanza that can be sent over the stream
@return [Blather::Stanza::Iq::Roster]
# File lib/blather/roster_item.rb, line 115 def to_stanza(type = nil) Stanza::Iq::Roster.new type, to_node end