Class Jabber::PubSub::ServiceHelper
In: lib/xmpp4r/pubsub/helper/servicehelper.rb
Parent: Object

A Helper representing a PubSub Service

Methods

Public Class methods

Creates a new representation of a pubsub service

client:[Jabber::Stream]
pubsubjid:[String] or [Jabber::JID]

[Source]

    # File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 25
25:       def initialize(client, pubsubjid)
26:         @client = client
27:         @pubsubjid = pubsubjid
28:         @event_cbs = CallbackList.new
29:         @client.add_message_callback(200,self) { |message|
30:           handle_message(message)
31:         }
32:       end

Public Instance methods

Register callbacks for incoming events (i.e. Message stanzas containing) PubSub notifications

[Source]

     # File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 293
293:       def add_event_callback(prio = 200, ref = nil, &block)
294:         @event_cbs.add(prio, ref, block)
295:       end

shows the affiliations on a pubsub service

return:[Hash] of { node => symbol }

[Source]

     # File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 133
133:       def affiliations
134:         iq = basic_pubsub_query(:get)
135:         iq.pubsub.add(REXML::Element.new('affiliations'))
136:         res = nil
137:         @client.send_with_id(iq) { |reply|
138:           if reply.pubsub.first_element('affiliations')
139:             res = {}
140:             reply.pubsub.first_element('affiliations').each_element('affiliation') do |affiliation|
141:               # TODO: This should be handled by an affiliation element class
142:               aff = case affiliation.attributes['affiliation']
143:                       when 'owner' then :owner
144:                       when 'publisher' then :publisher
145:                       when 'none' then :none
146:                       when 'outcast' then :outcast
147:                       else nil
148:                     end
149:               res[affiliation.attributes['node']] = aff
150:             end
151:           end
152:           true
153:         }
154:         res
155:       end

Create a new node on the pubsub service

node:[String] you node name - otherwise you get a automaticly generated one (in most cases)
configure:[Jabber::XMLStanza] if you want to configure you node (default nil)
return:[String]

[Source]

    # File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 39
39:       def create(node=nil, configure=nil)
40:         rnode = nil
41:         iq = basic_pubsub_query(:set)
42:         iq.pubsub.add(REXML::Element.new('create')).attributes['node'] = node
43:         if configure
44:           confele =  REXML::Element.new('configure')
45: 
46:           if configure.type_of?(XMLStanza)
47:             confele << configure
48:           end
49:           iq.pubsub.add(confele)
50:         end
51: 
52:         @client.send_with_id(iq) do |reply|
53:           if (create = reply.first_element('pubsub/create'))
54:             rnode = create.attributes['node']
55:           end
56:           true
57:         end
58: 
59:         rnode
60:       end

Delete a pubsub node

node:[String]
return:true

[Source]

    # File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 66
66:       def delete(node)
67:         iq = basic_pubsub_query(:set,true)
68:         iq.pubsub.add(REXML::Element.new('delete')).attributes['node'] = node
69:         @client.send_with_id(iq) { |reply|
70:           true
71:         }
72:       end

get options of a node

node:[String]
subid:[String] or nil
return:[Jabber::XData]

[Source]

     # File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 235
235:       def get_options(node,subid=nil)
236:         iq = basic_pubsub_query(:get)
237:         opt = REXML::Element.new('options')
238:         opt.attributes['node'] = node
239:         opt.attributes['jid'] = @client.jid.strip
240:         opt.attributes['subid'] = subid
241:         iq.pubsub.add(opt)
242:         ret = nil
243:         @client.send_with_id(iq) { |reply|
244:           reply.pubsub.options.first_element('x') { |xdata|
245:     
246:             ret = xdata if xdata.kind_of?(Jabber::XData)
247:     
248:           }
249:         true
250:         }
251:         return ret
252:       end

gets all items from a pubsub node

node:[String]
count:[Fixnum]
return:[Hash] { id => [Jabber::PubSub::Item] }

[Source]

     # File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 112
112:       def items(node,count=nil)
113:         iq = basic_pubsub_query(:get)
114:         items = Jabber::PubSub::Items.new
115:         items.node = node
116:         iq.pubsub.add(items)
117:         res = nil
118:         @client.send_with_id(iq) { |reply|
119:           if reply.kind_of?(Iq) and reply.pubsub and reply.pubsub.first_element('items')
120:             res = {}
121:             reply.pubsub.first_element('items').each_element('item') do |item|
122:               res[item.attributes['id']] = item.children.first if item.children.first
123:             end
124:           end
125:           true
126:         }
127:         res
128:       end

NOTE: this method sends only one item per publish request because some services may not allow batch processing maybe this will changed in the future

node:[String]
item:[Jabber::PubSub::Item]
return:true

[Source]

    # File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 81
81:       def publish(node,item)
82:         iq = basic_pubsub_query(:set)
83:         publish = iq.pubsub.add(REXML::Element.new('publish'))
84:         publish.attributes['node'] = node
85:         if item.kind_of?(Jabber::PubSub::Item)
86:           publish.add(item)
87:           @client.send_with_id(iq) { |reply| true }
88:         end
89:       end
node:[String]
item:[REXML::Element]
id:[String]
return:true

[Source]

     # File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 96
 96:       def publish_with_id(node,item,id)
 97:         if item.kind_of?(REXML::Element)
 98:           xmlitem = Jabber::PubSub::Item.new
 99:           xmlitem.id = id
100:           xmlitem.add(item)
101:           publish(node,xmlitem)
102:         else
103:           raise "given item is not a proper xml document or Jabber::PubSub::Item"
104:         end
105:       end

purges all items on a persist node

node:[String]
return:true

[Source]

     # File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 275
275:       def purge(node)
276:         iq = basic_pubsub_query(:set)
277:         purge = REXML::Element.new('purge')
278:         purge.attributes['node'] = node
279:         iq.pubsub.add(purge)
280:         @client.send_with_id(iq) { |reply| true }
281:       end

set options for a node

node:[String]
options:[Jabber::XData]
subid:[String] or nil
return:true

[Source]

     # File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 260
260:       def set_options(node,options,subid=nil)
261:         iq = basic_pubsub_query(:set)
262:         opt = REXML::Element.new('options')
263:         opt.attributes['node'] = node
264:         opt.attributes['jid'] = @client.jid.strip
265:         opt.attributes['subid'] = subid
266:         iq.pubsub.add(opt)
267:         iq.pubsub.options.add(options)
268:         @client.send_with_id(iq) { |reply| true }
269:       end

subscribe to a node

node:[String]
return:[Hash] of { attributename => value }

[Source]

     # File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 194
194:       def subscribe(node)
195:         iq = basic_pubsub_query(:set)
196:         sub = REXML::Element.new('subscribe')
197:         sub.attributes['node'] = node
198:         sub.attributes['jid'] = @client.jid.strip
199:         iq.pubsub.add(sub)
200:         res = {}
201:         @client.send_with_id(iq) do |reply|
202:           pubsubanswer = reply.pubsub
203:           if pubsubanswer.first_element('subscription')
204:             pubsubanswer.each_element('subscription') { |element|
205:               element.attributes.each { |name,value| res[name] = value }
206:             }
207:           end
208:           true
209:         end # @client.send_with_id(iq)
210:         res
211:       end

shows all jids of subscribers of a node

node:[String]
return:[Array] of [String]

[Source]

     # File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 182
182:       def subscribers(node)
183:         res = []
184:         subscriptions(node).each { |sub|
185:           res << sub.attributes['jid']
186:         }
187:         res
188:       end

shows all subscriptions on the given node

node:[String], or nil for all
return:[Array] of [REXML::Element]

[Source]

     # File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 161
161:       def subscriptions(node=nil)
162:         iq = basic_pubsub_query(:get)
163:         entities = iq.pubsub.add(REXML::Element.new('subscriptions'))
164:         entities.attributes['node'] = node
165:         res = nil
166:         @client.send_with_id(iq) { |reply|
167:           if reply.pubsub.first_element('subscriptions')
168:             res = []
169:             reply.pubsub.first_element('subscriptions').each_element('subscription') { |subscription|
170:               res << REXML::Element.new(subscription)
171:             }
172:           end
173:           true
174:         }
175:         res
176:       end

String representation

result:[String] The PubSub service‘s JID

[Source]

     # File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 286
286:       def to_s
287:         @pubsubjid.to_s
288:       end

Unsubscibe from a node with an optional subscription id

May raise ErrorException

node:[String]
subid:[String] or nil
return:true

[Source]

     # File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 220
220:       def unsubscribe(node,subid=nil)
221:         iq = basic_pubsub_query(:set)
222:         unsub = REXML::Element.new('unsubscribe')
223:         unsub.attributes['node'] = node
224:         unsub.attributes['jid'] = @client.jid.strip
225:         unsub.attributes['subid'] = subid
226:         iq.pubsub.add(unsub)
227:         @client.send_with_id(iq) { |reply| true        } # @client.send_with_id(iq)
228:       end

Private Instance methods

creates a basic pubsub iq basic_pubsub_query(type)

type:[Symbol]

[Source]

     # File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 303
303:       def basic_pubsub_query(type,ownerusecase = false)
304:         iq = Jabber::Iq::new(type,@pubsubjid)
305:         if ownerusecase 
306:           iq.add(IqPubSubOwner.new)
307:         else
308:           iq.add(IqPubSub.new)
309:         end
310:         iq
311:       end

handling incoming events handle_message(message)

message:[Jabber::Message]

[Source]

     # File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 317
317:       def handle_message(message)
318:         if message.from == @pubsubjid and message.first_element('event').kind_of?(Jabber::PubSub::Event)
319:           event = message.first_element('event')
320:           @event_cbs.process(event)
321:         end
322:       end

[Validate]