Class: Blather::Stanza::Iq::Roster::RosterItem

Inherits:
XMPPNode
  • Object
show all
Defined in:
lib/blather/stanza/iq/roster.rb

Overview

# RosterItem Fragment

Individual roster items. This is a convenience class to attach methods to the node

Constant Summary

Constants inherited from XMPPNode

XMPPNode::BASE_NAMES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from XMPPNode

class_from_registration, #decorate, decorator_modules, import, parse, register

Class Method Details

.new(XML: :Node) ⇒ Object .new(opts) ⇒ Object .new(jid = nil, name = nil, subscription = nil, ask = nil) ⇒ Object

Create a new RosterItem

Overloads:

  • .new(XML: :Node) ⇒ Object

    Create a RosterItem by inheriting a node

    Parameters:

    • node (XML::Node)

      an xml node to inherit

  • .new(opts) ⇒ Object

    Create a RosterItem through a hash of options the RosterItem must be one of Blather::RosterItem::VALID_SUBSCRIPTION_TYPES

    Parameters:

    • opts (Hash)

      the options

    Options Hash (opts):

    • :jid (Blather::JID, String, nil)

      the JID of the item

    • :name (String, nil)

      the alias to give the JID

    • :subscription (Symbol, nil)

      the subscription status of

    • :ask (:subscribe, nil)

      the ask value of the RosterItem

    • :groups (Array<#to_s>)

      the group names the RosterItem is a member of

  • .new(jid = nil, name = nil, subscription = nil, ask = nil) ⇒ Object

    RosterItem must be one of Blather::RosterItem::VALID_SUBSCRIPTION_TYPES

    Parameters:

    • jid (Blather::JID, String, nil) (defaults to: nil)

      the JID of the item

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

      the alias to give the JID

    • subscription (Symbol, nil) (defaults to: nil)

      the subscription status of the

    • ask (:subscribe, nil) (defaults to: nil)

      the ask value of the RosterItem

    • groups (Array<#to_s>)

      the group names the RosterItem is a member of



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/blather/stanza/iq/roster.rb', line 75

def self.new(jid = nil, name = nil, subscription = nil, ask = nil, groups = nil)
  new_node = super :item

  case jid
  when Nokogiri::XML::Node
    new_node.inherit jid
  when Hash
    new_node.jid = jid[:jid]
    new_node.name = jid[:name]
    new_node.subscription = jid[:subscription]
    new_node.ask = jid[:ask]
    new_node.groups = jid[:groups]
  else
    new_node.jid = jid
    new_node.name = name
    new_node.subscription = subscription
    new_node.ask = ask
    new_node.groups = groups
  end
  new_node
end

Instance Method Details

#ask<:subscribe, nil>

Get the ask value of the item

Returns:

  • (<:subscribe, nil>)


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

def ask
  read_attr :ask, :to_sym
end

#ask=(ask) ⇒ Object

Set the ask value of the item

Parameters:

  • ask (<:subscribe, nil>)


149
150
151
# File 'lib/blather/stanza/iq/roster.rb', line 149

def ask=(ask)
  write_attr :ask, ask
end

#groupsArray<String>

The groups roster item belongs to

Returns:

  • (Array<String>)


156
157
158
# File 'lib/blather/stanza/iq/roster.rb', line 156

def groups
  find('child::*[local-name()="group"]').map { |g| g.content }
end

#groups=(new_groups) ⇒ Object

Set the roster item’s groups

Parameters:

  • new_groups (Array<#to_s>)

    an array of group names



163
164
165
166
167
168
169
170
171
# File 'lib/blather/stanza/iq/roster.rb', line 163

def groups=(new_groups)
  remove_children :group
  if new_groups
    new_groups.uniq.each do |g|
      self << (group = XMPPNode.new(:group, self.document))
      group.content = g
    end
  end
end

#jidBlather::JID?

Get the JID attached to the item

Returns:



100
101
102
# File 'lib/blather/stanza/iq/roster.rb', line 100

def jid
  (j = self[:jid]) ? JID.new(j) : nil
end

#jid=(jid) ⇒ Object

Set the JID of the item

Parameters:



107
108
109
# File 'lib/blather/stanza/iq/roster.rb', line 107

def jid=(jid)
  write_attr :jid, (jid.nil?) ? nil : JID.new(jid).stripped
end

#nameString?

Get the item name

Returns:

  • (String, nil)


114
115
116
# File 'lib/blather/stanza/iq/roster.rb', line 114

def name
  read_attr :name
end

#name=(name) ⇒ Object

Set the item name

Parameters:

  • name (#to_s)

    the name of the item



121
122
123
# File 'lib/blather/stanza/iq/roster.rb', line 121

def name=(name)
  write_attr :name, name
end

#subscription<:both, :from, :none, :remove, :to>

Get the subscription value of the item

Returns:

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


128
129
130
# File 'lib/blather/stanza/iq/roster.rb', line 128

def subscription
  read_attr :subscription, :to_sym
end

#subscription=(subscription) ⇒ Object

Set the subscription value of the item

Parameters:

  • subscription (<:both, :from, :none, :remove, :to>)


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

def subscription=(subscription)
  write_attr :subscription, subscription
end

#to_stanzaBlather::Stanza::Iq::Roster

Convert the roster item to a proper stanza all wrapped up This facilitates new subscriptions



177
178
179
# File 'lib/blather/stanza/iq/roster.rb', line 177

def to_stanza
  Roster.new(:set, self)
end