Class: Jabber::Roster::RosterItem

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

Overview

Class containing the <item/> elements of the roster

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.

Direct Known Subclasses

Helper::RosterItem

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, subscription = nil, ask = nil) ⇒ RosterItem

Construct a new roster item

jid
JID

Jabber ID

iname
String

Name in the roster

subscription
Symbol

Type of subscription (see RosterItem#subscription=)

ask
Symbol

or [Nil] Can be :subscribe



115
116
117
118
119
120
121
# File 'lib/xmpp4r/roster/iq/roster.rb', line 115

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

Class Method Details

.import(item) ⇒ Object

Create new RosterItem from REXML::Element

item
REXML::Element

source element to copy attributes and children from



126
127
128
# File 'lib/xmpp4r/roster/iq/roster.rb', line 126

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

Instance Method Details

#askObject

Get if asking for subscription

result
Symbol

nil or :subscribe



197
198
199
200
201
202
# File 'lib/xmpp4r/roster/iq/roster.rb', line 197

def ask
  case attributes['ask']
    when 'subscribe' then :subscribe
    else nil
  end
end

#ask=(val) ⇒ Object

Set if asking for subscription

val
Symbol

nil or :subscribe



207
208
209
210
211
212
# File 'lib/xmpp4r/roster/iq/roster.rb', line 207

def ask=(val)
  case val
    when :subscribe then attributes['ask'] = 'subscribe'
    else attributes['ask'] = nil
  end
end

#groupsObject

Get groups the item belongs to

result
Array

of [String] The groups



217
218
219
220
221
222
223
# File 'lib/xmpp4r/roster/iq/roster.rb', line 217

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



231
232
233
234
235
236
237
238
239
# File 'lib/xmpp4r/roster/iq/roster.rb', line 231

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


135
136
137
# File 'lib/xmpp4r/roster/iq/roster.rb', line 135

def iname
  attributes['name']
end

#iname=(val) ⇒ Object

Set name of roster item

val
String

Name for this item



142
143
144
# File 'lib/xmpp4r/roster/iq/roster.rb', line 142

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

#jidObject

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

return
JID


150
151
152
# File 'lib/xmpp4r/roster/iq/roster.rb', line 150

def jid
  (a = attributes['jid']) ? JID::new(a) : nil
end

#jid=(val) ⇒ Object

Set JID of roster item

val
JID

or nil



157
158
159
# File 'lib/xmpp4r/roster/iq/roster.rb', line 157

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

#subscriptionObject

Get subscription type of roster item

result
Symbol

or [Nil] The following values are valid according to RFC3921:

  • :both

  • :from

  • :none

  • :remove

  • :to



169
170
171
172
173
174
175
176
177
178
# File 'lib/xmpp4r/roster/iq/roster.rb', line 169

def subscription
  case attributes['subscription']
    when 'both' then :both
    when 'from' then :from
    when 'none' then :none
    when 'remove' then :remove
    when 'to' then :to
    else nil
  end
end

#subscription=(val) ⇒ Object

Set subscription type of roster item

val
Symbol

or [Nil] See subscription for possible Symbols



183
184
185
186
187
188
189
190
191
192
# File 'lib/xmpp4r/roster/iq/roster.rb', line 183

def subscription=(val)
  case val
    when :both then attributes['subscription'] = 'both'
    when :from then attributes['subscription'] = 'from'
    when :none then attributes['subscription'] = 'none'
    when :remove then attributes['subscription'] = 'remove'
    when :to then attributes['subscription'] = 'to'
    else attributes['subscription'] = nil
  end
end