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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/blather/stanza/iq/roster.rb', line 82

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>)


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

def ask
  read_attr :ask, :to_sym
end

#ask=(ask) ⇒ Object

Set the ask value of the item

Parameters:

  • ask (<:subscribe, nil>)


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

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

#groupsArray<String>

The groups roster item belongs to

Returns:

  • (Array<String>)


163
164
165
# File 'lib/blather/stanza/iq/roster.rb', line 163

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



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

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:



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

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

#jid=(jid) ⇒ Object

Set the JID of the item

Parameters:



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

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

#nameString?

Get the item name

Returns:

  • (String, nil)


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

def name
  read_attr :name
end

#name=(name) ⇒ Object

Set the item name

Parameters:

  • name (#to_s)

    the name of the item



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

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>)


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

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>)


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

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



184
185
186
# File 'lib/blather/stanza/iq/roster.rb', line 184

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