Class: Jabber::PubSub::ServiceHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/xmpp4r/pubsub/helper/servicehelper.rb

Overview

A Helper representing a PubSub Service

Direct Known Subclasses

NodeHelper, UserTune::Helper

Instance Method Summary collapse

Constructor Details

#initialize(stream, pubsubjid) ⇒ ServiceHelper

Creates a new representation of a pubsub service

stream
Jabber::Stream
pubsubjid
String

or [Jabber::JID]



60
61
62
63
64
65
66
67
# File 'lib/xmpp4r/pubsub/helper/servicehelper.rb', line 60

def initialize(stream, pubsubjid)
  @stream = stream
  @pubsubjid = pubsubjid
  @event_cbs = CallbackList.new
  @stream.add_message_callback(200,self) { |message|
    handle_message(message)
  }
end

Instance Method Details

#add_event_callback(prio = 200, ref = nil, &block) ⇒ Object

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



378
379
380
# File 'lib/xmpp4r/pubsub/helper/servicehelper.rb', line 378

def add_event_callback(prio = 200, ref = nil, &block)
  @event_cbs.add(prio, ref, block)
end

#create_node(node = nil, configure = Jabber::PubSub::NodeConfig.new) ⇒ Object

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::PubSub::NodeConfig

if you want to configure your node (default nil)

return
String


210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/xmpp4r/pubsub/helper/servicehelper.rb', line 210

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
    true
  end

  rnode
end

#delete_node(node) ⇒ Object

Delete a pubsub node

node
String
return

true



259
260
261
262
263
264
265
# File 'lib/xmpp4r/pubsub/helper/servicehelper.rb', line 259

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) { |reply|
    true
  }
end

#get_affiliations(node = nil) ⇒ Object

shows the affiliations on a pubsub service

node
String
return
Hash

of { node => symbol }



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/xmpp4r/pubsub/helper/servicehelper.rb', line 272

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_config_from(node) ⇒ Object

get configuration from a node

node
String
return
Jabber::PubSub::Configure


234
235
236
237
238
239
240
241
242
# File 'lib/xmpp4r/pubsub/helper/servicehelper.rb', line 234

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

#get_items_from(node, count = nil) ⇒ Object

gets all items from a pubsub node

node
String
count
Fixnum
return
Hash

{ id => [Jabber::PubSub::Item] }



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/xmpp4r/pubsub/helper/servicehelper.rb', line 135

def get_items_from(node,count=nil)
  iq = basic_pubsub_query(:get)
  items = Jabber::PubSub::Items.new
  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(node, jid, subid = nil) ⇒ Object

get options from a subscription

node
String
jid
Jabber::JID

or [String]

subid
String

or nil

return
Jabber::PubSub::OwnerConfigure


339
340
341
342
343
344
345
346
347
# File 'lib/xmpp4r/pubsub/helper/servicehelper.rb', line 339

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

#get_subscribers_from(node) ⇒ Object

shows all jids of subscribers of a node

node
String
return
Array

of [String]



324
325
326
327
328
329
330
# File 'lib/xmpp4r/pubsub/helper/servicehelper.rb', line 324

def get_subscribers_from(node)
  res = []
  get_subscriptions_from(node).each { |sub|
    res << sub.jid
  }
  res
end

#get_subscriptions_from(node) ⇒ Object

shows all subscriptions on the given node

node
String
return
Array

of [Jabber::Pubsub::Subscription]



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/xmpp4r/pubsub/helper/servicehelper.rb', line 301

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_subscriptions_from_all_nodesObject

get all subscriptions on a pubsub component

return
Hash

of [PubSub::Subscription]



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/xmpp4r/pubsub/helper/servicehelper.rb', line 72

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
    true
  }

  res
end

#publish_item_to(node, item) ⇒ Object

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

it automatically generates an id for the item



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/xmpp4r/pubsub/helper/servicehelper.rb', line 160

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)
    item.id = Jabber::IdGenerator.generate_id
    publish.add(item)
    @stream.send_with_id(iq) { |reply| true }
  end
end

#publish_item_with_id_to(node, item, id) ⇒ Object

node
String
item
REXML::Element
id
String
return

true



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/xmpp4r/pubsub/helper/servicehelper.rb', line 177

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) { |reply| true }
end

#purge_items_from(node) ⇒ Object

purges all items on a persistent node

node
String
return

true



197
198
199
200
201
202
203
# File 'lib/xmpp4r/pubsub/helper/servicehelper.rb', line 197

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) { |reply| true }
end

#set_config_for(node, config) ⇒ Object

set configuration for a node

node
String
options
Jabber::PubSub::NodeConfig
return

true on success



249
250
251
252
253
# File 'lib/xmpp4r/pubsub/helper/servicehelper.rb', line 249

def set_config_for(node, config)
  iq = basic_pubsub_query( :set )
  iq.pubsub.add(Jabber::PubSub::NodeConfig(options).form)
  @stream.send_with_id(iq) { |reply| true }
end

#set_options_for(node, jid, options, subid = nil) ⇒ Object

set options for a subscription

node
String
jid
Jabber::JID

or [String]

options

[Jabber::PubSub::SubscriptionConfig} specifying configuration options

subid
String

or nil

return

true



356
357
358
359
360
361
362
363
364
365
366
# File 'lib/xmpp4r/pubsub/helper/servicehelper.rb', line 356

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 )
    true
  end

  ret
end

#subscribe_to(node) ⇒ Object

subscribe to a node

node
String
return
Hash

of { attributename => value }



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 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
    true
  end # @stream.send_with_id(iq)
  res
end

#to_sObject

String representation

result
String

The PubSub service’s JID



371
372
373
# File 'lib/xmpp4r/pubsub/helper/servicehelper.rb', line 371

def to_s
  @pubsubjid.to_s
end

#unsubscribe_from(node, subid = nil) ⇒ Object

Unsubscribe from a node with an optional subscription id

May raise ServerError

node
String
subid
String

or nil (not supported)

return

true



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/xmpp4r/pubsub/helper/servicehelper.rb', line 116

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
    true
  } # @stream.send_with_id(iq)
  ret
end