Class: Blather::Stanza::PubSub::Publish

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

Overview

# PubSub Publish Stanza

[XEP-0060 Section 7.1 - Publish an Item to a Node](xmpp.org/extensions/xep-0060.html#publisher-publish)

Constant Summary

Constants inherited from Iq

Iq::VALID_TYPES

Constants inherited from XMPPNode

XMPPNode::BASE_NAMES

Instance Attribute Summary

Attributes inherited from Blather::Stanza

#handler_hierarchy

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=, #initialize, next_id, register, #reply, #reply!, #to, #to=, #type, #type=

Methods inherited from XMPPNode

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

Constructor Details

This class inherits a constructor from Blather::Stanza

Class Method Details

.new(host = nil, node = nil, type = :set, payload = nil) ⇒ Object

Create a new publish node

Parameters:

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

    the host to pushlish the node to

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

    the name of the node to publish to

  • type (Blather::Stanza::Iq::VALID_TYPES) (defaults to: :set)

    the node type

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

    the payload to publish see #payload=



22
23
24
25
26
27
# File 'lib/blather/stanza/pubsub/publish.rb', line 22

def self.new(host = nil, node = nil, type = :set, payload = nil)
  new_node = super(type, host)
  new_node.node = node
  new_node.payload = payload if payload
  new_node
end

Instance Method Details

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

Iterate over the list of items

Yields:

  • (item)

    a block to accept each item

Yield Parameters:



89
90
91
# File 'lib/blather/stanza/pubsub/publish.rb', line 89

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

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

Get the list of items



79
80
81
82
83
# File 'lib/blather/stanza/pubsub/publish.rb', line 79

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

#nodeString?

Get the name of the node to publish to

Returns:

  • (String, nil)


54
55
56
# File 'lib/blather/stanza/pubsub/publish.rb', line 54

def node
  publish[:node]
end

#node=(node) ⇒ Object

Set the name of the node to publish to

Parameters:

  • node (String, nil)


61
62
63
# File 'lib/blather/stanza/pubsub/publish.rb', line 61

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

#payload=(hash) ⇒ Object #payload=(array) ⇒ Object #payload=(string) ⇒ Object

Set the payload to publish

Overloads:

  • #payload=(hash) ⇒ Object

    Set the payload as a set of ID => payload entries

    Parameters:

    • hash (Hash<id => payload>)
  • #payload=(array) ⇒ Object

    Set the list of payloads all at once

    Parameters:

    • array (Array<#to_s>)
  • #payload=(string) ⇒ Object

    Set the payload as a string

    Parameters:

    • string (#to_s)


40
41
42
43
44
45
46
47
48
49
# File 'lib/blather/stanza/pubsub/publish.rb', line 40

def payload=(payload)
  payload = case payload
  when Hash   then  payload.to_a
  when Array  then  payload.map { |v| [nil, v] }
  else              [[nil, payload]]
  end
  payload.each do |id, value|
    self.publish << PubSubItem.new(id, value, self.document)
  end
end

#publishBlather::XMPPNode

Get or create the actual publish node

Returns:



68
69
70
71
72
73
74
# File 'lib/blather/stanza/pubsub/publish.rb', line 68

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

#sizeFixnum

Get the size of the items list

Returns:

  • (Fixnum)


96
97
98
# File 'lib/blather/stanza/pubsub/publish.rb', line 96

def size
  items.size
end