Class: Jabber::Observable::PubSub

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

Overview

Jabber::Observable::PubSub - Convenience subclass to deal with PubSub

Defined Under Namespace

Classes: AlreadySet, NoService

Instance Method Summary collapse

Constructor Details

#initialize(observable) ⇒ PubSub

Creates a new PubSub object

observable

points a Jabber::Observable object



23
24
25
26
27
28
29
# File 'lib/xmpp4r/observable/pubsub.rb', line 23

def initialize(observable)
  @observable = observable

  @helper = @service_jid = nil
  @disco = Jabber::Discovery::Helper.new(@observable.client)
  attach!
end

Instance Method Details

#attach!Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/xmpp4r/observable/pubsub.rb', line 31

def attach!
  begin
    domain = Jabber::JID.new(@observable.jid).domain
    @service_jid = "pubsub." + domain
    set_service(@service_jid)
  rescue
    @helper = @service_jid = nil
  end
  return has_service?
end

#create_node(node) ⇒ Object

Create a PubSub node (Lots of options still have to be encoded!)



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/xmpp4r/observable/pubsub.rb', line 91

def create_node(node)
  raise_noservice if ! has_service?
  begin
    @helper.create_node(node)
  rescue => e
    raise e
    return nil
  end
  @my_nodes << node if defined? @my_nodes
  node
end

#delete_node(node) ⇒ Object

Delete a PubSub node (Lots of options still have to be encoded!)



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/xmpp4r/observable/pubsub.rb', line 149

def delete_node(node)
  raise_noservice if ! has_service?
  begin
    @helper.delete_node(node)
  rescue => e
    raise e
    return nil
  end
  @my_nodes.delete(node) if defined? @my_nodes
  node
end

#get_items_from(node, count = nil) ⇒ Object

Get items from a node



199
200
201
202
# File 'lib/xmpp4r/observable/pubsub.rb', line 199

def get_items_from(node, count = nil)
  raise_noservice if ! has_service?
  @helper.get_items_from(node, count)
end

#has_service?Boolean

Checks if the PubSub service is set

Returns:

  • (Boolean)


51
52
53
# File 'lib/xmpp4r/observable/pubsub.rb', line 51

def has_service?
  ! @helper.nil?
end

#inspectObject

:nodoc:



42
43
44
45
46
47
48
# File 'lib/xmpp4r/observable/pubsub.rb', line 42

def inspect  #:nodoc:
  if has_service?
    sprintf("#<%s:0x%x @service_jid=%s>", self.class.name, __id__, @service_jid)
  else
    sprintf("#<%s:0x%x @has_service?=false>", self.class.name, __id__)
  end
end

#is_subscribed_to?(node) ⇒ Boolean

Return true if we’re subscribed to that node

Returns:

  • (Boolean)


140
141
142
143
144
145
146
# File 'lib/xmpp4r/observable/pubsub.rb', line 140

def is_subscribed_to?(node)
  ret = false
  subscriptions.each do |sub|
    ret = true if sub.node == node and sub.attributes['subscription'] == 'subscribed'
  end
  return ret
end

#my_nodesObject

Return an array of nodes I own



104
105
106
107
108
109
110
111
112
113
# File 'lib/xmpp4r/observable/pubsub.rb', line 104

def my_nodes
  if ! defined? @my_nodes
    ret = []
    subscriptions.each do |sub|
       ret << sub.node if sub.attributes['affiliation'] == 'owner'
    end
    @my_nodes = ret
  end
  return @my_nodes
end

#node_exists?(node) ⇒ Boolean

Return true if a given node exists

Returns:

  • (Boolean)


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

def node_exists?(node)
  ret = []
  if ! defined? @existing_nodes or ! @existing_nodes.include?(node)
    # We'll renew @existing_nodes if we haven't got it the first time
    reply = @disco.get_items_for(@service_jid)
    reply.items.each do |item|
      ret << item.node
    end
    @existing_nodes = ret
  end
  return @existing_nodes.include?(node)
end

#publish_atom_item(node, title, body, time = Time.now) ⇒ Object

Publish atom Item. This is an item with one atom entry with title, body and time.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/xmpp4r/observable/pubsub.rb', line 179

def publish_atom_item(node, title, body, time = Time.now)
  raise_noservice if ! has_service?

  item = Jabber::PubSub::Item.new
  entry = REXML::Element.new('entry')
  entry.add_namespace("http://www.w3.org/2005/Atom")
  mytitle = REXML::Element.new('title')
  mytitle.text = title
  entry.add(mytitle)
  mybody = REXML::Element.new('body')
  mybody.text = body
  entry.add(mybody)
  published = REXML::Element.new("published")
  published.text = time.utc.iso8601
  entry.add(published)
  item.add(entry)
  publish_item(node, item)
end

#publish_item(node, item) ⇒ Object

Publish an Item. This infers an item of Jabber::PubSub::Item kind is passed



162
163
164
165
# File 'lib/xmpp4r/observable/pubsub.rb', line 162

def publish_item(node, item)
  raise_noservice if ! has_service?
  @helper.publish_item_to(node, item)
end

#publish_simple_item(node, text) ⇒ Object

Publish Simple Item. This is an item with one element and some text to it.



168
169
170
171
172
173
174
175
176
# File 'lib/xmpp4r/observable/pubsub.rb', line 168

def publish_simple_item(node, text)
  raise_noservice if ! has_service?

  item = Jabber::PubSub::Item.new
  xml = REXML::Element.new('value')
  xml.text = text
  item.add(xml)
  publish_item(node, item)
end

#set_service(service) ⇒ Object

Sets the PubSub service. Just one service is allowed. If nil, reset.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/xmpp4r/observable/pubsub.rb', line 56

def set_service(service)
  if service.nil?
    @helper = @service_jid = nil
  else
    raise NotConnected, "You are not connected" if ! @observable.connected?
    raise AlreadySet, "You already have a PubSub service (#{@service_jid})." if has_service?
    @helper = Jabber::PubSub::ServiceHelper.new(@observable.client, service)
    @service_jid = service

    @helper.add_event_callback do |event|
      @observable.changed(:event)
      @observable.notify_observers(:event, event)
    end
  end
end

#subscribe_to(node) ⇒ Object

Subscribe to a node.



73
74
75
76
# File 'lib/xmpp4r/observable/pubsub.rb', line 73

def subscribe_to(node)
  raise_noservice if ! has_service?
  @helper.subscribe_to(node) unless is_subscribed_to?(node)
end

#subscribed_nodesObject

Returns an array of nodes I am subscribed to



130
131
132
133
134
135
136
137
# File 'lib/xmpp4r/observable/pubsub.rb', line 130

def subscribed_nodes
  ret = []
  subscriptions.each do |sub|
    next if sub.node.nil?
    ret << sub.node if sub.attributes['subscription'] == 'subscribed' and ! my_nodes.include?(sub.node)
  end
  return ret
end

#subscriptionsObject

Return the subscriptions we have in the configured PubSub service.



85
86
87
88
# File 'lib/xmpp4r/observable/pubsub.rb', line 85

def subscriptions
  raise_noservice if ! has_service?
  @helper.get_subscriptions_from_all_nodes()
end

#unsubscribe_from(node) ⇒ Object

Unsubscribe from a node.



79
80
81
82
# File 'lib/xmpp4r/observable/pubsub.rb', line 79

def unsubscribe_from(node)
  raise_noservice if ! has_service?
  @helper.unsubscribe_from(node)
end