A Helper representing a PubSub Service
Creates a new representation of a pubsub service
or [Jabber::JID]
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 61 def initialize(stream, pubsubjid) @stream = stream @pubsubjid = pubsubjid @event_cbs = CallbackList.new @stream.add_message_callback(200,self) { |message| handle_message(message) } end
Create a new collection node on the pubsub service
the node name - otherwise you get an automatically generated one (in most cases)
if you want to configure your node (default nil)
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 257 def create_collection_node(node = nil, configure = Jabber::PubSub::NodeConfig.new) if configure.options['pubsub#node_type'] && configure.options['pubsub#node_type'] != 'collection' raise Jabber::ArgumentError, "Invalid node_type specified in node configuration. Either do not specify one, or use 'collection'" end configure.options = configure.options.merge({'pubsub#node_type' => 'collection'}) create_node(node, configure) end
Create a new node on the pubsub service
the node name - otherwise you get a automatically generated one (in most cases)
if you want to configure your node (default nil)
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 233 def create_node(node = nil, configure = Jabber::PubSub::NodeConfig.new) rnode = nil iq = basic_pubsub_query(:set) iq.pubsub.add(REXML::Element.new('create')).attributes['node'] = node if configure if configure.kind_of?(Jabber::PubSub::NodeConfig) iq.pubsub.add(configure) end end @stream.send_with_id(iq) do |reply| if reply.kind_of?(Jabber::Iq) and reply.type == :result rnode = node end end rnode end
deletes an item from a persistent node
or [Array] of [String]
true
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 195 def delete_item_from(node, item_id) iq = basic_pubsub_query(:set) retract = iq.pubsub.add(Jabber::PubSub::Retract.new) retract.node = node if item_id.kind_of? Array item_id.each { |id| xmlitem = Jabber::PubSub::Item.new xmlitem.id = id retract.add(xmlitem) } else xmlitem = Jabber::PubSub::Item.new xmlitem.id = item_id retract.add(xmlitem) end @stream.send_with_id(iq) end
Delete a pubsub node
true
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 294 def delete_node(node) iq = basic_pubsub_query(:set,true) iq.pubsub.add(REXML::Element.new('delete')).attributes['node'] = node @stream.send_with_id(iq) end
shows the affiliations on a pubsub service
of { node => symbol }
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 318 def get_affiliations(node = nil) iq = basic_pubsub_query(:get) affiliations = iq.pubsub.add(REXML::Element.new('affiliations')) affiliations.attributes['node'] = node res = nil @stream.send_with_id(iq) { |reply| if reply.pubsub.first_element('affiliations') res = {} reply.pubsub.first_element('affiliations').each_element('affiliation') do |affiliation| # TODO: This should be handled by an affiliation element class aff = case affiliation.attributes['affiliation'] when 'owner' then :owner when 'publisher' then :publisher when 'none' then :none when 'outcast' then :outcast else nil end res[affiliation.attributes['node']] = aff end end true } res end
get configuration from a node
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 269 def get_config_from(node) iq = basic_pubsub_query(:get, true) iq.pubsub.add(Jabber::PubSub::OwnerNodeConfig.new(node)) ret = nil @stream.send_with_id(iq) do |reply| ret = reply.pubsub.first_element('configure') end ret end
gets all items from a pubsub node
{ id => [Jabber::PubSub::Item] }
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 133 def get_items_from(node, count=nil) iq = basic_pubsub_query(:get) items = Jabber::PubSub::Items.new items.max_items = count items.node = node iq.pubsub.add(items) res = nil @stream.send_with_id(iq) { |reply| if reply.kind_of?(Iq) and reply.pubsub and reply.pubsub.first_element('items') res = {} reply.pubsub.first_element('items').each_element('item') do |item| res[item.attributes['id']] = item.children.first if item.children.first end end true } res end
get options from a subscription
or [String]
or nil
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 385 def get_options_from(node, jid, subid = nil) iq = basic_pubsub_query(:get) iq.pubsub.add(Jabber::PubSub::SubscriptionConfig.new(node, jid.kind_of?(String) ? Jabber::JID.new(jid).strip: jid.strip, subid)) ret = nil @stream.send_with_id(iq) do |reply| ret = reply.pubsub.first_element('options') end ret end
shows all jids of subscribers of a node
of [String]
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 370 def get_subscribers_from(node) res = [] get_subscriptions_from(node).each { |sub| res << sub.jid } res end
shows all subscriptions on the given node
of [Jabber::Pubsub::Subscription]
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 347 def get_subscriptions_from(node) iq = basic_pubsub_query(:get) entities = iq.pubsub.add(REXML::Element.new('subscriptions')) entities.attributes['node'] = node res = nil @stream.send_with_id(iq) { |reply| if reply.pubsub.first_element('subscriptions') res = [] if reply.pubsub.first_element('subscriptions').attributes['node'] == node reply.pubsub.first_element('subscriptions').each_element('subscription') { |subscription| res << PubSub::Subscription.import(subscription) } end end true } res end
get all subscriptions on a pubsub component
of [PubSub::Subscription]
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 73 def get_subscriptions_from_all_nodes iq = basic_pubsub_query(:get) entities = iq.pubsub.add(REXML::Element.new('subscriptions')) res = nil @stream.send_with_id(iq) { |reply| if reply.pubsub.first_element('subscriptions') res = [] reply.pubsub.first_element('subscriptions').each_element('subscription') { |subscription| res << Jabber::PubSub::Subscription.import(subscription) } end } res 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?
true
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 158 def publish_item_to(node,item) iq = basic_pubsub_query(:set) publish = iq.pubsub.add(REXML::Element.new('publish')) publish.attributes['node'] = node if item.kind_of?(Jabber::PubSub::Item) publish.add(item) @stream.send_with_id(iq) end end
true
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 174 def publish_item_with_id_to(node,item,id) iq = basic_pubsub_query(:set) publish = iq.pubsub.add(REXML::Element.new('publish')) publish.attributes['node'] = node if item.kind_of?(REXML::Element) xmlitem = Jabber::PubSub::Item.new xmlitem.id = id xmlitem.import(item) publish.add(xmlitem) else raise "given item is not a proper xml document or Jabber::PubSub::Item" end @stream.send_with_id(iq) end
purges all items on a persistent node
true
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 220 def purge_items_from(node) iq = basic_pubsub_query(:set) purge = REXML::Element.new('purge') purge.attributes['node'] = node iq.pubsub.add(purge) @stream.send_with_id(iq) end
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 300 def set_affiliations(node, jid, role = 'publisher') iq = basic_pubsub_query(:set, true) affiliations = iq.pubsub.add(REXML::Element.new('affiliations')) affiliations.attributes['node'] = node affiliation = affiliations.add(REXML::Element.new('affiliation')) affiliation.attributes['jid'] = jid.to_s affiliation.attributes['affiliation'] = role.to_s res = nil @stream.send_with_id(iq) { |reply| true } res end
set configuration for a node
true on success
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 284 def set_config_for(node, config) iq = basic_pubsub_query(:set, true) iq.pubsub.add(config) @stream.send_with_id(iq) end
set options for a subscription
or [String]
[Jabber::PubSub::SubscriptionConfig} specifying configuration options
or nil
true
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 402 def set_options_for(node, jid, options, subid = nil) iq = basic_pubsub_query(:set) iq.pubsub.add(Jabber::PubSub::SubscriptionConfig.new(node, jid.kind_of?(String) ? Jabber::JID.new(jid).strip: jid.strip, options, subid)) ret = false @stream.send_with_id(iq) do |reply| ret = ( reply.type == :result ) end ret end
subscribe to a node
of { attributename => value }
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 92 def subscribe_to(node) iq = basic_pubsub_query(:set) sub = REXML::Element.new('subscribe') sub.attributes['node'] = node sub.attributes['jid'] = @stream.jid.strip.to_s iq.pubsub.add(sub) res = nil @stream.send_with_id(iq) do |reply| pubsubanswer = reply.pubsub if pubsubanswer.first_element('subscription') res = PubSub::Subscription.import(pubsubanswer.first_element('subscription')) end end # @stream.send_with_id(iq) res end
Unsubscribe from a node with an optional subscription id
May raise ServerError
or nil (not supported)
true
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 115 def unsubscribe_from(node, subid=nil) iq = basic_pubsub_query(:set) unsub = PubSub::Unsubscribe.new unsub.node = node unsub.jid = @stream.jid.strip iq.pubsub.add(unsub) ret = false @stream.send_with_id(iq) { |reply| ret = reply.kind_of?(Jabber::Iq) and reply.type == :result } # @stream.send_with_id(iq) ret end
creates a basic pubsub iq #basic_pubsub_query(type)
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 433 def basic_pubsub_query(type,ownerusecase = false) iq = Jabber::Iq.new(type,@pubsubjid) if ownerusecase iq.add(IqPubSubOwner.new) else iq.add(IqPubSub.new) end iq.from = @stream.jid #.strip iq end
handling incoming events #handle_message(message)
# File lib/xmpp4r/pubsub/helper/servicehelper.rb, line 448 def handle_message(message) if message.from == @pubsubjid and message.first_element('event').kind_of?(Jabber::PubSub::Event) event = message.first_element('event') @event_cbs.process(event) end end