| Class | Jabber::PubSub::ServiceHelper |
| In: |
lib/xmpp4r/pubsub/helper/servicehelper.rb
|
| Parent: | Object |
A Helper representing a PubSub Service
Creates a new representation of a pubsub service
| client: | [Jabber::Stream] |
| pubsubjid: | [String] or [Jabber::JID] |
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 24
24: def initialize(client, pubsubjid)
25: @client = client
26: @pubsubjid = pubsubjid
27: @event_cbs = CallbackList.new
28: @client.add_message_callback(200,self) { |message|
29: handle_message(message)
30: }
31: end
shows the affiliations on a pubsub service
| return: | [Hash] of { node => symbol } |
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 134
134: def affiliations
135: iq = basic_pubsub_query(:get)
136: iq.pubsub.add(REXML::Element.new('affiliations'))
137: res = nil
138: @client.send_with_id(iq) { |reply|
139: if reply.pubsub.first_element('affiliations')
140: res = {}
141: reply.pubsub.first_element('affiliations').each_element('affiliation') do |affiliation|
142: # TODO: This should be handled by an affiliation element class
143: aff = case affiliation.attributes['affiliation']
144: when 'owner' then :owner
145: when 'publisher' then :publisher
146: when 'none' then :none
147: when 'outcast' then :outcast
148: else nil
149: end
150: res[affiliation.attributes['node']] = aff
151: end
152: end
153: true
154: }
155: res
156: 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] |
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 38
38: def create(node=nil, configure=nil)
39: rnode = nil
40: iq = basic_pubsub_query(:set)
41: iq.pubsub.add(REXML::Element.new('create')).attributes['node'] = node
42: if configure
43: confele = REXML::Element.new('configure')
44:
45: if configure.type_of?(XMLStanza)
46: confele << configure
47: end
48: iq.pubsub.add(confele)
49: end
50:
51: @client.send_with_id(iq) do |reply|
52: if (create = reply.first_element('pubsub/create'))
53: rnode = create.attributes['node']
54: end
55: true
56: end
57:
58: rnode
59: end
Delete a pubsub node
| node: | [String] |
| return: | true |
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 65
65: def delete(node)
66: iq = basic_pubsub_query(:set)
67: iq.pubsub.add(REXML::Element.new('delete')).attributes['node'] = node
68:
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] |
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 237
237: def get_options(node,subid=nil)
238: iq = basic_pubsub_query(:get)
239: opt = REXML::Element.new('options')
240: opt.attributes['node'] = node
241: opt.attributes['jid'] = @client.jid.strip
242: opt.attributes['subid'] = subid
243: iq.pubsub.add(opt)
244: ret = nil
245: @client.send_with_id(iq) { |reply|
246: reply.pubsub.options.first_element('x') { |xdata|
247: ret = xdata if xdata.kind_of?(Jabber::XData)
248: }
249: true
250: }
251: end
gets all items from a pubsub node
| node: | [String] |
| count: | [Fixnum] |
| return: | [Hash] { id => [Jabber::PubSub::Item] } |
# 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:
118: res = nil
119: @client.send_with_id(iq) { |reply|
120: if reply.kind_of?(Iq) and reply.pubsub and reply.pubsub.first_element('items')
121: res = {}
122: reply.pubsub.first_element('items').each_element('item') do |item|
123: res[item.attributes['id']] = item.children.first if item.children.first
124: end
125: end
126: true
127: }
128: res
129: 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 |
# 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 |
# 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
set options for a node
| node: | [String] |
| options: | [Jabber::XData] |
| subid: | [String] or nil |
| return: | true |
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 259
259: def set_options(node,options,subid=nil)
260: iq = basic_pubsub_query(:set)
261: opt = REXML::Element.new('options')
262: opt.attributes['node'] = node
263: opt.attributes['jid'] = @client.jid.strip
264: opt.attributes['subid'] = subid
265: iq.pubsub.add(opt)
266: iq.pubsub.options.add(options)
267: @client.send_with_id(iq) { |reply| true }
268: end
subscribe to a node
| node: | [String] |
| return: | [Hash] of { attributename => value } |
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 196
196: def subscribe(node)
197: iq = basic_pubsub_query(:set)
198: sub = REXML::Element.new('subscribe')
199: sub.attributes['node'] = node
200: sub.attributes['jid'] = @client.jid.strip
201: iq.pubsub.add(sub)
202: res = {}
203: @client.send_with_id(iq) do |reply|
204: pubsubanswer = reply.pubsub
205: if pubsubanswer.first_element('subscription')
206: pubsubanswer.each_element('subscription') { |element|
207: element.attributes.each { |name,value| res[name] = value }
208: }
209: end
210: true
211: end # @client.send_with_id(iq)
212: res
213: end
shows all jids of subscribers of a node
| node: | [String] |
| return: | [Array] of [String] |
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 184
184: def subscribers(node)
185: res = []
186: subscriptions(node).each { |sub|
187: res << sub.attributes['jid']
188: }
189: res
190: end
shows all subscriptions on the given node
| node: | [String], or nil for all |
| return: | [Array] of [REXML::Element] |
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 162
162: def subscriptions(node=nil)
163: iq = basic_pubsub_query(:get)
164: entities = iq.pubsub.add(REXML::Element.new('subscriptions'))
165: entities.attributes['node'] = node
166:
167: res = nil
168: @client.send_with_id(iq) { |reply|
169: if reply.pubsub.first_element('subscriptions')
170: res = []
171: reply.pubsub.first_element('subscriptions').each_element('subscription') { |subscription|
172: res << REXML::Element.new(subscription)
173: }
174: end
175: true
176: }
177: res
178: end
Unsubscibe from a node with an optional subscription id
May raise ErrorException
| node: | [String] |
| subid: | [String] or nil |
| return: | true |
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 222
222: def unsubscribe(node,subid=nil)
223: iq = basic_pubsub_query(:set)
224: unsub = REXML::Element.new('unsubscribe')
225: unsub.attributes['node'] = node
226: unsub.attributes['jid'] = @client.jid.strip
227: unsub.attributes['subid'] = subid
228: iq.pubsub.add(unsub)
229: @client.send_with_id(iq) { |reply| true } # @client.send_with_id(iq)
230: end
creates a basic pubsub iq basic_pubsub_query(type)
| type: | [Symbol] |
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 290
290: def basic_pubsub_query(type)
291: iq = Jabber::Iq::new(type,@pubsubjid)
292: iq.add(IqPubSub.new)
293: iq
294: end
handling incoming events handle_message(message)
| message: | [Jabber::Message] |
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 300
300: def handle_message(message)
301: if message.from == @pubsubjid and message.first_element('event').kind_of?(Jabber::PubSub::Event)
302: event = message.first_element('event')
303: @event_cbs.process(event)
304: end
305: end