Class: Jabber::Roster::XRosterItem

Inherits:
REXML::Element show all
Defined in:
lib/xmpp4r/roster/x/roster.rb

Overview

Class containing an <item/> element

The 'name' attribute has been renamed to 'iname' here as 'name' is already used by REXML::Element for the element's name. It's still name='…' in XML.

This is all a bit analoguous to Jabber::RosterItem, used by Jabber::IqQueryRoster. But this class lacks the subscription and ask attributes.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from REXML::Element

#delete_elements, #first_element, #first_element_text, #import, #replace_element_text, #typed_add

Constructor Details

#initialize(jid = nil, iname = nil) ⇒ XRosterItem

Construct a new roster item

jid
JID

Jabber ID

iname
String

Name in the roster



58
59
60
61
62
# File 'lib/xmpp4r/roster/x/roster.rb', line 58

def initialize(jid=nil, iname=nil)
  super('item')
  self.jid = jid
  self.iname = iname
end

Class Method Details

.import(item) ⇒ Object

Create new XRosterItem from REXML::Element

item
REXML::Element

source element to copy attributes and children from



67
68
69
# File 'lib/xmpp4r/roster/x/roster.rb', line 67

def XRosterItem.import(item)
  XRosterItem::new.import(item)
end

Instance Method Details

#actionObject

Get action for this roster item

  • :add

  • :modify

  • :delete

result
Symbol

(defaults to :add according to JEP-0144)



108
109
110
111
112
113
114
# File 'lib/xmpp4r/roster/x/roster.rb', line 108

def action
  case attributes['action']
    when 'modify' then :modify
    when 'delete' then :delete
    else :add
  end
end

#action=(a) ⇒ Object

Set action for this roster item (see action)



119
120
121
122
123
124
125
# File 'lib/xmpp4r/roster/x/roster.rb', line 119

def action=(a)
  case a
    when :modify then attributes['action'] = 'modify'
    when :delete then attributes['action'] = 'delete'
    else attributes['action'] = 'add'
  end
end

#groupsObject

Get groups the item belongs to

result
Array

of [String] The groups



130
131
132
133
134
135
136
# File 'lib/xmpp4r/roster/x/roster.rb', line 130

def groups
  result = []
  each_element('group') { |group|
    result.push(group.text)
  }
  result
end

#groups=(ary) ⇒ Object

Set groups the item belongs to, deletes old groups first.

See JEP 0083 for nested groups

ary
Array

New groups, duplicate values will be removed



144
145
146
147
148
149
150
151
152
# File 'lib/xmpp4r/roster/x/roster.rb', line 144

def groups=(ary)
  # Delete old group elements
  delete_elements('group')
  
  # Add new group elements
  ary.uniq.each { |group|
    add_element('group').text = group
  }
end

#inameObject

Get name of roster item

names can be set by the roster's owner himself

return
String


76
77
78
# File 'lib/xmpp4r/roster/x/roster.rb', line 76

def iname
  attributes['name']
end

#iname=(val) ⇒ Object

Set name of roster item

val
String

Name for this item



83
84
85
# File 'lib/xmpp4r/roster/x/roster.rb', line 83

def iname=(val)
  attributes['name'] = val
end

#jidObject

Get JID of roster item Resource of the JID will not be stripped

return
JID


91
92
93
# File 'lib/xmpp4r/roster/x/roster.rb', line 91

def jid
  JID::new(attributes['jid'])
end

#jid=(val) ⇒ Object

Set JID of roster item

val
JID

or nil



98
99
100
# File 'lib/xmpp4r/roster/x/roster.rb', line 98

def jid=(val)
  attributes['jid'] = val.nil? ? nil : val.to_s
end