class Blather::RosterItem

RosterItems hold internal representations of the user's roster including each JID's status.

Constants

VALID_SUBSCRIPTION_TYPES

@private

Attributes

ask[R]
groups[RW]
jid[R]
name[RW]
statuses[R]

Public Class Methods

new(item) click to toggle source

@private

Calls superclass method
# File lib/blather/roster_item.rb, line 17
def self.new(item)
  return item if item.is_a?(self)
  super
end
new(item) click to toggle source

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

<=>(other) click to toggle source

Compare two RosterItems by their JID

@param [Blather::JID] other the JID to compare against @return [Fixnum<-1, 0, 1>]

# File lib/blather/roster_item.rb, line 127
def <=>(other)
  JID.new(self.jid) <=> JID.new(other.jid)
end
==(o) click to toggle source

@private

# File lib/blather/roster_item.rb, line 141
def ==(o)
  eql?(o)
end
ask=(ask) click to toggle source

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
eql?(o, *fields) click to toggle source

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
jid=(jid) click to toggle source

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
status(resource = nil) click to toggle source

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
status=(presence) click to toggle source

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
subscription() click to toggle source

Get the current subscription

@return [:both, :from, :none, :remove, :to]

# File lib/blather/roster_item.rb, line 77
def subscription
  @subscription || :none
end
subscription=(sub) click to toggle source

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
to_node() click to toggle source
# File lib/blather/roster_item.rb, line 119
def to_node
  Stanza::Iq::Roster::RosterItem.new jid, name, subscription, ask, groups
end
to_stanza(type = nil) click to toggle source

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