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


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

def self.callbacks
  @@callbacks
end

.confObject

Config loaded from config.yaml



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

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


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

def self.connection
  @@connection
end

.on_connected(connection) ⇒ Object

::nodoc


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

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

.on_disconnectedObject

::nodoc


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

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.



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

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



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

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
144
145
146
147
# File 'lib/superfeedr.rb', line 139

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.



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

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


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

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