Class: C3s::Subscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/pubsub/subscriber.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, service_name) ⇒ Subscriber

Initializes the publisher

client
Jabber::Client

the client publishing

service
String

pubsub service name (eg: pubsub.jabber)



10
11
12
13
14
15
# File 'lib/pubsub/subscriber.rb', line 10

def initialize(client, service_name)
  @client = client
  @service = service_name
  @pubsub = Jabber::PubSub::ServiceHelper.new(@client, @service)
  @browser = Jabber::PubSub::NodeBrowser.new(@client)
end

Instance Method Details

#get_last_item(nodename) ⇒ Object

Returns the last item from a node. This node must be a leaf node.

nodename
String

the node name



49
50
51
# File 'lib/pubsub/subscriber.rb', line 49

def get_last_item(nodename)
  @pubsub.get_items_from(nodename, 1)
end

#get_leaf_nodes(firstnode, jid) ⇒ Object

TODO - THIS NEEDED A @disco OBJECT.. AND I DON’T REMEMBER THE CLASS OF THE OBJECT Gets the leaf nodes for a given provider node. If the provider node is ‘location’ then the leaf nodes will be in the form ‘location:[email protected]/resource’



73
74
75
76
77
78
79
80
81
# File 'lib/pubsub/subscriber.rb', line 73

def get_leaf_nodes(firstnode, jid)
  return if firstnode.nil? or @disco.nil?
  firstnode.items.each do |item|
    node = @disco.get_items_for(config['pubsub'], item.node)
    c3snode = C3s::Node.new(item.node)
    @items << item if c3snode.leaf? and jid==c3snode.jid
    get_leaf_nodes(node, jid) unless node.items.size<1 or item.node.nil?
  end
end

#get_subscriptionsObject

Returns all the subscriptions



55
56
57
# File 'lib/pubsub/subscriber.rb', line 55

def get_subscriptions
  @pubsub.get_subscriptions_from_all_nodes
end

#is_collection?(nodename) ⇒ Boolean

Checks if a node is a collection node

nodename
String

the node name

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/pubsub/subscriber.rb', line 41

def is_collection?(nodename)
  info = @browser.get_info(@service, nodename)
  info['type'].eql?('collection')
end

#subscribe_collection(nodename, options = {}) ⇒ Object

Subscribes to collection node.

nodename
String

the node name



20
21
22
23
24
25
26
27
28
# File 'lib/pubsub/subscriber.rb', line 20

def subscribe_collection(nodename, options={})
  return if !is_collection?(nodename)
  subscription = @pubsub.subscribe_to(nodename)
  options = {
    'pubsub#subscription_type' => options[:subscription_type] || 'items',
    'pubsub#subscription_depth' => options[:subscription_depth] || 'all'
  }
  @pubsub.set_options_for(nodename, @client.jid, options, subscription.subid)
end

#unsubscribe_allObject

Unsubscribes all subscribed nodes



61
62
63
64
65
# File 'lib/pubsub/subscriber.rb', line 61

def unsubscribe_all
  get_subscriptions().each do |sub|
    unsubscribe_from(sub.node, sub.subid)
  end
end

#unsubscribe_from(nodename, subid) ⇒ Object

Unsubscribe node

nodename
String

the node name

subid
String

the subscription ID



34
35
36
# File 'lib/pubsub/subscriber.rb', line 34

def unsubscribe_from(nodename, subid)
  @pubsub.unsubscribe_from(nodename, subid)
end