Module: Superfeedr

Defined in:
lib/superfeedr.rb

Overview

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

Defined Under Namespace

Classes: NotConnected

Constant Summary collapse

@@connection =
nil
@@callbacks =
{}
@@connection_callback =
nil
@@notication_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 Babylon’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


160
161
162
# File 'lib/superfeedr.rb', line 160

def self.callbacks
  @@callbacks
end

.confObject

Config loaded from config.yaml



205
206
207
# File 'lib/superfeedr.rb', line 205

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” : [email protected] “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.



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 33

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"
      Babylon::ClientConnection.connect(params, self) 
    else 
      Babylon::ComponentConnection.connect(params, self) 
    end          
  }
  
  if EventMachine.reactor_running?
    run.call
  else
  EventMachine.run {
    run.call
  }
  end
end

.connectionObject

::nodoc


166
167
168
# File 'lib/superfeedr.rb', line 166

def self.connection
  @@connection
end

.on_connected(connection) ⇒ Object

::nodoc


178
179
180
181
# File 'lib/superfeedr.rb', line 178

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

.on_disconnectedObject

::nodoc


185
186
187
# File 'lib/superfeedr.rb', line 185

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)
  @@notication_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.



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/superfeedr.rb', line 191

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.at("event")
      @@notication_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



148
149
150
# File 'lib/superfeedr.rb', line 148

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
# File 'lib/superfeedr.rb', line 140

def self.on_subscriptions(stanza, &block)
  page = stanza.xpath('//subscriptions').first["page"].to_i
  feeds = stanza.xpath('//subscription').map { |s| CGI.unescapeHTML(s["node"]) }
  block.call(page, feeds)
end

.on_unsubscribe(stanza, &block) ⇒ Object

Called with a response to an unsubscribe.



154
155
156
# File 'lib/superfeedr.rb', line 154

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 Babylon’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


172
173
174
# File 'lib/superfeedr.rb', line 172

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 feed.



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 feed.



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