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
@@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:



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

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


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

def self.callbacks
  @@callbacks
end

.confObject

Config loaded from config.yaml



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

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.



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

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


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

def self.connection
  @@connection
end

.on_connected(connection) ⇒ Object

::nodoc


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

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

.on_disconnectedObject

::nodoc


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

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.



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

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.



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

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")
      @@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



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

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



139
140
141
142
143
# File 'lib/superfeedr.rb', line 139

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.



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

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:



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

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


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

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.



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

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



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

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:



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

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.



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

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