Class: Blather::Stanza::PubSub::Items

Inherits:
Blather::Stanza::PubSub show all
Includes:
Enumerable
Defined in:
lib/blather/stanza/pubsub/items.rb

Overview

# PubSub Items Stanza

[XEP-0060 Section 6.5 - Retrieve Items from a Node](xmpp.org/extensions/xep-0060.html#subscriber-retrieve)

Constant Summary

Constants inherited from Iq

Iq::VALID_TYPES

Constants inherited from XMPPNode

XMPPNode::BASE_NAMES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Blather::Stanza::PubSub

import, #inherit, #pubsub

Methods inherited from Iq

#error?, #get?, import, #reply!, #result?, #set?, #type=

Methods inherited from Blather::Stanza

#as_error, #error?, #from, #from=, handler_list, #id, #id=, next_id, register, #reply, #reply!, #to, #to=, #type, #type=

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, #to_stanza, #write_attr

Methods inherited from Nokogiri::XML::Node

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

Class Method Details

.new(type = nil, host = nil) ⇒ Object

Overrides the parent to ensure an items node is created



39
40
41
42
43
# File 'lib/blather/stanza/pubsub/items.rb', line 39

def self.new(type = nil, host = nil)
  new_node = super
  new_node.items
  new_node
end

.request(host, path, list = [], max = nil) ⇒ Blather::Stanza::PubSub::Items

Create a new Items request

Parameters:

  • host (String)

    the pubsub host to send the request to

  • path (String)

    the path of the node

  • list (Array<String>) (defaults to: [])

    an array of IDs to request

  • max (#to_s) (defaults to: nil)

    the maximum number of items to return

Returns:



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/blather/stanza/pubsub/items.rb', line 24

def self.request(host, path, list = [], max = nil)
  node = self.new :get, host

  node.node = path
  node.max_items = max

  (list || []).each do |id|
    node.items_node << PubSubItem.new(id, nil, node.document)
  end

  node
end

Instance Method Details

#each {|item| ... } ⇒ Object

Iterate over the list of items

Yield Parameters:



76
77
78
# File 'lib/blather/stanza/pubsub/items.rb', line 76

def each(&block)
  items.each &block
end

#itemsArray<Blather::Stanza::PubSub::PubSubItem>

Get the list of items on this stanza



83
84
85
86
87
# File 'lib/blather/stanza/pubsub/items.rb', line 83

def items
  items_node.find('ns:item', :ns => self.class.registered_ns).map do |i|
    PubSubItem.new(nil,nil,self.document).inherit i
  end
end

#items_nodeBlather::XMPPNode

Get or create the actual items node

Returns:



92
93
94
95
96
97
98
# File 'lib/blather/stanza/pubsub/items.rb', line 92

def items_node
  unless node = self.pubsub.find_first('ns:items', :ns => self.class.registered_ns)
    (self.pubsub << (node = XMPPNode.new('items', self.document)))
    node.namespace = self.pubsub.namespace
  end
  node
end

#max_itemsFixnum?

Get the max number of items requested

Returns:

  • (Fixnum, nil)


62
63
64
# File 'lib/blather/stanza/pubsub/items.rb', line 62

def max_items
  items_node[:max_items].to_i if items_node[:max_items]
end

#max_items=(max_items) ⇒ Object

Set the max number of items requested

Parameters:

  • max_items (Fixnum, nil)


69
70
71
# File 'lib/blather/stanza/pubsub/items.rb', line 69

def max_items=(max_items)
  items_node[:max_items] = max_items
end

#nodeString

Get the node name

Returns:

  • (String)


48
49
50
# File 'lib/blather/stanza/pubsub/items.rb', line 48

def node
  items_node[:node]
end

#node=(node) ⇒ Object

Set the node name

Parameters:

  • node (String, nil)


55
56
57
# File 'lib/blather/stanza/pubsub/items.rb', line 55

def node=(node)
  items_node[:node] = node
end