Class: Blather::RosterItem

Inherits:
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]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item) ⇒ RosterItem

item

can be a JID, String (a@b) or a Stanza



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/blather/roster_item.rb', line 23

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.



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

def ask
  @ask
end

#groupsObject

Returns the value of attribute groups.



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

def groups
  @groups
end

#jidObject

Returns the value of attribute jid.



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

def jid
  @jid
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#statusesObject (readonly)

Returns the value of attribute statuses.



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

def statuses
  @statuses
end

Class Method Details

.new(item) ⇒ Object



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

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

Instance Method Details

#status(resource = nil) ⇒ Object

Return the status with the highest priority if resource is set find the status of that specific resource



85
86
87
# File 'lib/blather/roster_item.rb', line 85

def status(resource = nil)
  top = resource ? @statuses.detect { |s| s.from.resource == resource } : @statuses.first
end

#status=(presence) ⇒ Object

Set the status then sorts them according to priority

presence

Status



76
77
78
79
80
# File 'lib/blather/roster_item.rb', line 76

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

#subscriptionObject

Get the current subscription

returns

:both, :from, :none, :remove, :to or :none



61
62
63
# File 'lib/blather/roster_item.rb', line 61

def subscription
  @subscription || :none
end

#subscription=(sub) ⇒ Object

Set the subscription Ensures it is one of VALID_SUBSCRIPTION_TYPES

Raises:



52
53
54
55
56
# File 'lib/blather/roster_item.rb', line 52

def subscription=(sub)
  raise ArgumentError, "Invalid Type (#{sub}), use: #{VALID_SUBSCRIPTION_TYPES*' '}" if
    sub && !VALID_SUBSCRIPTION_TYPES.include?(sub = sub.to_sym)
  @subscription = sub ? sub : :none
end

#to_stanza(type = nil) ⇒ Object

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



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

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