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

Inherits:
XMPPNode 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, #content_from, import, #inherit, #inherit_attrs, #inspect, #namespace=, #namespace_href, #nokogiri_namespace=, #read_attr, #read_content, register, #remove_child, #remove_children, #set_content_for, #write_attr

Methods inherited from Nokogiri::XML::Node

#[]=, #attr_set, #find_first, #nokogiri_xpath, #xpath

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

  • .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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/blather/stanza/iq/roster.rb', line 71

def self.new(jid = nil, name = nil, subscription = nil, ask = 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]
  else
    new_node.jid = jid
    new_node.name = name
    new_node.subscription = subscription
    new_node.ask = ask
  end
  new_node
end

Instance Method Details

#ask<:subscribe, nil>

Get the ask value of the item

Returns:

  • (<:subscribe, nil>)


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

def ask
  read_attr :ask, :to_sym
end

#ask=(ask) ⇒ Object

Set the ask value of the item

Parameters:

  • ask (<:subscribe, nil>)


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

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

#groupsArray<String>

The groups roster item belongs to

Returns:

  • (Array<String>)


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

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



157
158
159
160
161
162
163
164
165
# File 'lib/blather/stanza/iq/roster.rb', line 157

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:



94
95
96
# File 'lib/blather/stanza/iq/roster.rb', line 94

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

#jid=(jid) ⇒ Object

Set the JID of the item

Parameters:



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

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

#nameString?

Get the item name

Returns:

  • (String, nil)


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

def name
  read_attr :name
end

#name=(name) ⇒ Object

Set the item name

Parameters:

  • name (#to_s)

    the name of the item



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

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


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

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


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

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



171
172
173
# File 'lib/blather/stanza/iq/roster.rb', line 171

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