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)



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

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

#basic_pubsub_query(type, ownerusecase = false) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/pubsub/subscriber.rb', line 46

def basic_pubsub_query(type,ownerusecase = false)
  iq = Jabber::Iq.new(type, @client.config['pubsub'])
  if ownerusecase
    iq.add(Jabber::PubSub::IqPubSubOwner.new)
  else
    iq.add(Jabber::PubSub::IqPubSub.new)
  end
  iq.from = @client.jid
  iq
end

#get_last_item(nodename) ⇒ Object

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

nodename
String

the node name



76
77
78
# File 'lib/pubsub/subscriber.rb', line 76

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’



100
101
102
103
104
105
106
107
108
# File 'lib/pubsub/subscriber.rb', line 100

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



82
83
84
# File 'lib/pubsub/subscriber.rb', line 82

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)


68
69
70
71
# File 'lib/pubsub/subscriber.rb', line 68

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

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

Subscribes to a collection node.

nodename
String

the node name



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pubsub/subscriber.rb', line 21

def subscribe_collection(nodename, options={})
  $LOG.info "Going to subscribe #{nodename}"
  iq = basic_pubsub_query(:set)
  sub = REXML::Element.new('subscribe')
  sub.attributes['node'] = nodename
  sub.attributes['jid'] = @client.config['jid']
  iq.pubsub.add(sub)
  
  # Set the options to subscribe collection node
  options = {
    'pubsub#subscription_type' => options[:subscription_type] || 'items',
    'pubsub#subscription_depth' => options[:subscription_depth] || 'all'
  }
  iq.pubsub.add(Jabber::PubSub::SubscriptionConfig.new(nodename, @client.jid, options))
  
  res = nil
  @client.send_with_id(iq) do |reply|
    pubsubanswer = reply.pubsub
    if pubsubanswer.first_element('subscription')
      res = PubSub::Subscription.import(pubsubanswer.first_element('subscription'))
    end
  end
  res
end

#unsubscribe_allObject

Unsubscribes all subscribed nodes



88
89
90
91
92
# File 'lib/pubsub/subscriber.rb', line 88

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



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

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