Module: Superfeedr

Defined in:
lib/superfeedr.rb

Overview

Based on the API documented there : superfeedr.com/documentation

Defined Under Namespace

Classes: NotConnected

Constant Summary

@@connection =
nil
@@callbacks =
{}
@@connection_callback =
nil
@@notification_callback =
nil

Class Method Summary collapse

Class Method Details

.add_feeds(feeds_url, &block) ⇒ Object

Adds the url to the list of feeds you're monitoring. The block passed in argument will be called upon success. The block will take one boolen argument : true means everything went right... false means something failed! (Please set Skates's log to Log4r::INFO for more info)

Raises:



96
97
98
99
100
101
102
103
# File 'lib/superfeedr.rb', line 96

def self.add_feeds(feeds_url, &block)
  raise NotConnected unless connection
  stanza = SubscribeQueryStanza.new({:nodes => feeds_url, :from => connection.jid})
  @@callbacks[stanza.id] = Hash.new
  @@callbacks[stanza.id][:method] = method(:on_subscribe)
  @@callbacks[stanza.id][:param] = block
  send(stanza)
end

.callbacksObject

::nodoc



164
165
166
# File 'lib/superfeedr.rb', line 164

def self.callbacks
  @@callbacks
end

.confObject

Config loaded from config.yaml



209
210
211
# File 'lib/superfeedr.rb', line 209

def self.conf
  @@conf ||= YAML::load(File.read(File.dirname(__FILE__) + '/config.yaml'))
end

.connect(jid, password, host = nil, port = nil, app_type = "client", &block) ⇒ Object

Connects your client to the Superfeedr.com XMPP server. You need to pass the following arguments : "jid" : login@superfeedr.com "password" : your superfeedr.com password

"host" : host for your jid or component : only useful if you use an external jid
"port" : port for your jid or component : only useful if you use an external jid
"app_type" : (client | component) only useful if you use an external jid

The optional block will be called upon connection.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/superfeedr.rb', line 32

def self.connect(jid, password, host = nil, port = nil, app_type = "client", &block)

  params = {
    "jid" => jid,
    "password" => password,
    "host" => host,
    "port" => port
  }
  @@connection_callback = block
  
  run = Proc.new {
    if app_type == "client"
      Skates::ClientConnection.connect(params, self) 
    else 
      Skates::ComponentConnection.connect(params, self) 
    end          
  }
  
  if EventMachine.reactor_running?
    run.call
  else
  EventMachine.run {
    run.call
  }
  end
end

.connectionObject

::nodoc



170
171
172
# File 'lib/superfeedr.rb', line 170

def self.connection
  @@connection
end

.on_connected(connection) ⇒ Object

::nodoc



182
183
184
185
# File 'lib/superfeedr.rb', line 182

def self.on_connected(connection) 
  @@connection = connection
  @@connection_callback.call
end

.on_disconnectedObject

::nodoc



189
190
191
# File 'lib/superfeedr.rb', line 189

def self.on_disconnected()
  @@connection = false
end

.on_notification(&block) ⇒ Object

Specifies the block that will be called upon notification. Your block should take a NotificationStanza instance argument.



134
135
136
# File 'lib/superfeedr.rb', line 134

def self.on_notification(&block)
  @@notification_callback = block
end

.on_stanza(stanza) ⇒ Object

This shall not be called by your application. It is called upon stanza recetion. If it is a reply to a stanza we sent earlier, then, we just call it's associated callback. If it is a notification stanza, then, we call the notification callback (that you should have given when calling Superfeedr.connect) with a NotificationStanza instance.



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/superfeedr.rb', line 195

def self.on_stanza(stanza)
  if stanza["id"] && @@callbacks[stanza["id"]]
    @@callbacks[stanza["id"]][:method].call(stanza, &@@callbacks[stanza["id"]][:param])
    @@callbacks.delete(stanza["id"])
  else
    if stanza.name == "message" and !stanza.xpath("./ps:event", {"ps" => "http://jabber.org/protocol/pubsub#event"}).empty?
      @@notification_callback.call(NotificationStanza.new(stanza)) if @@notification_callback
      # Here we need to call the main notification callback!
    end
  end
end

.on_subscribe(stanza, &block) ⇒ Object

Called with a response to a subscribe



152
153
154
# File 'lib/superfeedr.rb', line 152

def self.on_subscribe(stanza, &block)
  block.call(stanza["type"] == "result")
end

.on_subscriptions(stanza, &block) ⇒ Object

Called with a response to a subscriptions listing



140
141
142
143
144
145
146
147
148
# File 'lib/superfeedr.rb', line 140

def self.on_subscriptions(stanza, &block)
  xmlns = {
    'pubsub' => 'http://jabber.org/protocol/pubsub',
    'superfeedr' => 'http://superfeedr.com/xmpp-pubsub-ext'
  }
  page = stanza.xpath('//pubsub:subscriptions/@superfeedr:page', xmlns).to_s.to_i
  feeds = stanza.xpath('//pubsub:subscription', xmlns).map { |s| CGI.unescapeHTML(s["node"]) }
  block.call(page, feeds)
end

.on_unsubscribe(stanza, &block) ⇒ Object

Called with a response to an unsubscribe.



158
159
160
# File 'lib/superfeedr.rb', line 158

def self.on_unsubscribe(stanza, &block)
  block.call(stanza["type"] == "result")
end

.remove_feeds(feeds_url, &block) ⇒ Object

Unsubscribe from a feed. The block passed in argument will be called upon success. The block will take one boolen argument : true means everything went right... false means something failed! (Please set Skates's log to Log4r::INFO for more info)

Raises:



109
110
111
112
113
114
115
116
# File 'lib/superfeedr.rb', line 109

def self.remove_feeds(feeds_url, &block)
  raise NotConnected unless connection
  stanza = UnsubscribeQueryStanza.new({:nodes => feeds_url, :from => connection.jid})
  @@callbacks[stanza.id] = Hash.new
  @@callbacks[stanza.id][:method] = method(:on_unsubscribe)
  @@callbacks[stanza.id][:param] = block
  send(stanza)
end

.send(xml) ⇒ Object

::nodoc



176
177
178
# File 'lib/superfeedr.rb', line 176

def self.send(xml)
  connection.send_xml(xml)
end

.subscribe(*feeds, &block) ⇒ Object

Subscribes to the multiple feeds, 30 by 30. Calls the block after each set of 30 feeds.



61
62
63
64
65
66
67
68
# File 'lib/superfeedr.rb', line 61

def self.subscribe(*feeds, &block)
  return if feeds.flatten! == []
  subset = feeds.slice!(0..29)
  Superfeedr.add_feeds(subset) do |result|
    subscribe(feeds, &block)
    block.call(subset) 
  end
end

.subscriptions(start_page = 1, &block) ⇒ Object

List all subscriptions, by sending them by blocks (page), starting at page specified in argument



83
84
85
86
87
88
89
90
# File 'lib/superfeedr.rb', line 83

def self.subscriptions(start_page = 1, &block)
  Superfeedr.subscriptions_by_page(start_page) do |page, result|
    if !result.empty?
      subscriptions(start_page + 1, &block)
    end
    block.call(page, result)
  end
end

.subscriptions_by_page(page = 1, &block) ⇒ Object

Lists the subscriptions by page. The block passed in argument will be called with 2 arguments : the page, and an array of the feed's url in the page you requested. (Currently the Superfeedr API only supports 30 feeds per page.)

Raises:



122
123
124
125
126
127
128
129
# File 'lib/superfeedr.rb', line 122

def self.subscriptions_by_page(page = 1, &block)
  raise NotConnected unless connection
  stanza = SubscriptionsQueryStanza.new({:page => page, :from => connection.jid})
  @@callbacks[stanza.id] = Hash.new
  @@callbacks[stanza.id][:method] = method(:on_subscriptions)
  @@callbacks[stanza.id][:param] = block
  send(stanza)
end

.unsubscribe(*feeds, &block) ⇒ Object

Ubsubscribe to multiple feeds, one by one. Calls the block after each set of 30 feeds.



72
73
74
75
76
77
78
79
# File 'lib/superfeedr.rb', line 72

def self.unsubscribe(*feeds, &block)
  return if feeds.flatten! == []
  subset = feeds.slice!(0..29)
  Superfeedr.remove_feeds(subset) do |result|
    unsubscribe(feeds, &block)
    block.call(subset) 
  end
end