Class: Blather::RosterItem

Inherits:
Object
  • Object
show all
Defined in:
lib/blather/roster_item.rb

Overview

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

Constant Summary collapse

VALID_SUBSCRIPTION_TYPES =
[:both, :from, :none, :remove, :to].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jid) ⇒ Blather::RosterItem #initialize(jid) ⇒ Blather::RosterItem #initialize(node) ⇒ Blather::RosterItem

Create a new RosterItem

Overloads:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/blather/roster_item.rb', line 33

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

Instance Attribute Details

#askObject

Returns the value of attribute ask.



8
9
10
# File 'lib/blather/roster_item.rb', line 8

def ask
  @ask
end

#groupsObject

Returns the value of attribute groups.



12
13
14
# File 'lib/blather/roster_item.rb', line 12

def groups
  @groups
end

#jidObject

Returns the value of attribute jid.



8
9
10
# File 'lib/blather/roster_item.rb', line 8

def jid
  @jid
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/blather/roster_item.rb', line 12

def name
  @name
end

#statusesObject (readonly)

Returns the value of attribute statuses.



8
9
10
# File 'lib/blather/roster_item.rb', line 8

def statuses
  @statuses
end

Class Method Details

.new(item) ⇒ Object



15
16
17
18
# File 'lib/blather/roster_item.rb', line 15

def self.new(item)
  return item if item.is_a?(self)
  super
end

Instance Method Details

#<=>(o) ⇒ Object



121
122
123
# File 'lib/blather/roster_item.rb', line 121

def <=>(o)
  self.jid.to_s <=> o.jid.to_s
end

#eql?(o) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


125
126
127
128
129
# File 'lib/blather/roster_item.rb', line 125

def eql?(o)
  o.is_a?(RosterItem) &&
  o.jid == self.jid &&
  o.groups == self.groups
end

#status(resource = nil) ⇒ Object

The status with the highest priority

Parameters:

  • resource (String, nil) (defaults to: nil)

    the resource to get the status of



101
102
103
104
105
106
107
# File 'lib/blather/roster_item.rb', line 101

def status(resource = nil)
  top = if resource
    @statuses.detect { |s| s.from.resource == resource }
  else
    @statuses.last
  end
end

#status=(presence) ⇒ Object

Set the status then sorts them according to priority

Parameters:

  • the (Blather::Stanza::Status)

    new status



92
93
94
95
96
# File 'lib/blather/roster_item.rb', line 92

def status=(presence)
  @statuses.delete_if { |s| s.from == presence.from || s.state == :unavailable }
  @statuses << presence
  @statuses.sort!
end

#subscription:both, ...

Get the current subscription

Returns:

  • (:both, :from, :none, :remove, :to)


75
76
77
# File 'lib/blather/roster_item.rb', line 75

def subscription
  @subscription || :none
end

#subscription=(sub) ⇒ Object

Set the subscription Ensures it is one of VALID_SUBSCRIPTION_TYPES

Parameters:

  • sub (#to_sym)

    the new subscription



65
66
67
68
69
70
# File 'lib/blather/roster_item.rb', line 65

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_stanza(type = nil) ⇒ Blather::Stanza::Iq::Roster

Translate the RosterItem into a proper stanza that can be sent over the stream



113
114
115
116
117
118
119
# File 'lib/blather/roster_item.rb', line 113

def to_stanza(type = nil)
  r = Stanza::Iq::Roster.new type
  n = Stanza::Iq::Roster::RosterItem.new jid, name, subscription, ask
  r.query << n
  n.groups = groups
  r
end