superfeedr-superfeedr-ruby 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION.yml +2 -2
- data/lib/config.yaml +2 -0
- data/lib/stanzas/iq_query_stanza.rb +1 -1
- data/lib/superfeedr.rb +55 -7
- data/spec/superfeedr_ruby_spec.rb +55 -15
- metadata +3 -2
data/VERSION.yml
CHANGED
data/lib/config.yaml
ADDED
@@ -4,7 +4,7 @@ class IqQueryStanza
|
|
4
4
|
@doc = Nokogiri::XML::Document.new
|
5
5
|
@iq = Nokogiri::XML::Node.new("iq", @doc)
|
6
6
|
@iq["type"] = params[:type].to_s
|
7
|
-
@iq["to"] =
|
7
|
+
@iq["to"] = Superfeedr.conf[:superfeedr_jid]
|
8
8
|
@iq["id"] = "#{random_iq_id}"
|
9
9
|
@iq["from"] = params[:from] if params[:from]
|
10
10
|
end
|
data/lib/superfeedr.rb
CHANGED
@@ -6,6 +6,12 @@ require "stanzas/subscribe_query_stanza.rb"
|
|
6
6
|
require "stanzas/unsubscribe_query_stanza.rb"
|
7
7
|
require "stanzas/subscriptions_query_stanza.rb"
|
8
8
|
|
9
|
+
|
10
|
+
##
|
11
|
+
# By default, the log level is at error. You can change that at anytime in your app
|
12
|
+
Babylon.logger.level = Log4r::ERROR
|
13
|
+
|
14
|
+
|
9
15
|
##
|
10
16
|
# Based on the API documented there : http://superfeedr.com/documentation
|
11
17
|
module Superfeedr
|
@@ -52,10 +58,43 @@ module Superfeedr
|
|
52
58
|
end
|
53
59
|
|
54
60
|
##
|
55
|
-
#
|
61
|
+
# Subscribes to the multiple feeds, one by one. Calls the block after each feed.
|
62
|
+
def self.subscribe(*feeds, &block)
|
63
|
+
return if feeds.flatten! == []
|
64
|
+
feed = feeds.shift
|
65
|
+
Superfeedr.add_feed(feed) do |result|
|
66
|
+
subscribe(feeds, &block)
|
67
|
+
block.call(feed)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
##
|
72
|
+
# Ubsubscribe to multiple feeds, one by one. Calls the block after each feed.
|
73
|
+
def self.unsubscribe(*feeds, &block)
|
74
|
+
return if feeds.flatten! == []
|
75
|
+
feed = feeds.shift
|
76
|
+
Superfeedr.remove_feed(feed) do |result|
|
77
|
+
unsubscribe(feeds, &block)
|
78
|
+
block.call(feed)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
##
|
83
|
+
# List all subscriptions, by sending them by blocks (page), starting at page specified in argument
|
84
|
+
def self.subscriptions(start_page = 1, &block)
|
85
|
+
Superfeedr.subscriptions_by_page(start_page) do |result|
|
86
|
+
if !result.empty?
|
87
|
+
subscriptions(start_page + 1, &block)
|
88
|
+
end
|
89
|
+
block.call(result)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
##
|
94
|
+
# Adds the url to the list of feeds you're monitoring. The block passed in argument will be called upon success.
|
56
95
|
# The block will take one boolen argument : true means everything went right... false means something failed!
|
57
96
|
# (Please set Babylon's log to Log4r::INFO for more info)
|
58
|
-
def self.
|
97
|
+
def self.add_feed(feed_url, &block)
|
59
98
|
raise NotConnected unless connection
|
60
99
|
stanza = SubscribeQueryStanza.new({:node => feed_url, :from => connection.jid})
|
61
100
|
@@callbacks[stanza.id] = Hash.new
|
@@ -68,7 +107,7 @@ module Superfeedr
|
|
68
107
|
# Unsubscribe from a feed. The block passed in argument will be called upon success.
|
69
108
|
# The block will take one boolen argument : true means everything went right... false means something failed!
|
70
109
|
# (Please set Babylon's log to Log4r::INFO for more info)
|
71
|
-
def self.
|
110
|
+
def self.remove_feed(feed_url, &block)
|
72
111
|
raise NotConnected unless connection
|
73
112
|
stanza = UnsubscribeQueryStanza.new({:node => feed_url, :from => connection.jid})
|
74
113
|
@@callbacks[stanza.id] = Hash.new
|
@@ -81,7 +120,7 @@ module Superfeedr
|
|
81
120
|
# Lists the subscriptions by page. The block passed in argument will be called with 2 arguments : the page,
|
82
121
|
# and an array of the feed's url in the page you requested.
|
83
122
|
# (Currently the Superfeedr API only supports 30 feeds per page.)
|
84
|
-
def self.
|
123
|
+
def self.subscriptions_by_page(page = 1, &block)
|
85
124
|
raise NotConnected unless connection
|
86
125
|
stanza = SubscriptionsQueryStanza.new({:page => page, :from => connection.jid})
|
87
126
|
@@callbacks[stanza.id] = Hash.new
|
@@ -154,10 +193,19 @@ module Superfeedr
|
|
154
193
|
if stanza["id"] && @@callbacks[stanza["id"]]
|
155
194
|
@@callbacks[stanza["id"]][:method].call(stanza, &@@callbacks[stanza["id"]][:param])
|
156
195
|
@@callbacks.delete(stanza["id"])
|
157
|
-
|
158
|
-
|
159
|
-
|
196
|
+
else
|
197
|
+
if stanza.name == "message" and stanza.at("event")
|
198
|
+
@@notication_callback.call(NotificationStanza.new(stanza))
|
199
|
+
# Here we need to call the main notification callback!
|
200
|
+
end
|
160
201
|
end
|
161
202
|
end
|
162
203
|
|
204
|
+
##
|
205
|
+
# Config loaded from config.yaml
|
206
|
+
def self.conf
|
207
|
+
@@conf ||= YAML::load(File.read(File.dirname(__FILE__) + '/config.yaml'))
|
208
|
+
end
|
209
|
+
|
210
|
+
|
163
211
|
end
|
@@ -12,7 +12,48 @@ describe Superfeedr do
|
|
12
12
|
describe "on_stanza" do
|
13
13
|
end
|
14
14
|
|
15
|
+
|
15
16
|
describe "subscribe" do
|
17
|
+
it "should call add_feed with feeds supplied" do
|
18
|
+
feeds = "a", "b", "c", "d"
|
19
|
+
feeds.each do |feed|
|
20
|
+
Superfeedr.should_receive(:add_feed).with(feed).and_yield(true)
|
21
|
+
end
|
22
|
+
Superfeedr.subscribe(feeds) do |result|
|
23
|
+
result.should be_true
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "unsubscribe" do
|
29
|
+
it "should call remove_feed with feeds supplied" do
|
30
|
+
feeds = "a", "b", "c", "d"
|
31
|
+
feeds.each do |feed|
|
32
|
+
Superfeedr.should_receive(:remove_feed).with(feed).and_yield(true)
|
33
|
+
end
|
34
|
+
Superfeedr.unsubscribe(feeds) do |result|
|
35
|
+
result.should be_true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "subscriptions" do
|
41
|
+
it "should call subscriptions_by_page for each page as long as they're not empty" do
|
42
|
+
def method_called_upon_page
|
43
|
+
end
|
44
|
+
self.should_receive(:method_called_upon_page).exactly(4).times
|
45
|
+
3.times do |t|
|
46
|
+
Superfeedr.should_receive(:subscriptions_by_page).with(t+1).and_yield(["a", "b", "c"])
|
47
|
+
end
|
48
|
+
Superfeedr.should_receive(:subscriptions_by_page).with(4).and_yield([])
|
49
|
+
Superfeedr.subscriptions do |result|
|
50
|
+
method_called_upon_page
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
describe "add_feed" do
|
16
57
|
before(:each) do
|
17
58
|
Superfeedr.stub!(:connection).and_return(@mock_connection)
|
18
59
|
Superfeedr.stub!(:send).and_return(true)
|
@@ -27,28 +68,28 @@ describe Superfeedr do
|
|
27
68
|
it "should raise an error if not connected" do
|
28
69
|
Superfeedr.should_receive(:connection).and_return(nil)
|
29
70
|
lambda {
|
30
|
-
Superfeedr.
|
71
|
+
Superfeedr.add_feed(@node, &@block)
|
31
72
|
}.should raise_error(Superfeedr::NotConnected)
|
32
73
|
end
|
33
74
|
|
34
75
|
it "should create a new SubscribeQueryStanza with the right url" do
|
35
76
|
SubscribeQueryStanza.should_receive(:new).with({:node => @node, :from => @mock_connection.jid}).and_return(@mock_stanza)
|
36
|
-
Superfeedr.
|
77
|
+
Superfeedr.add_feed(@node, &@block)
|
37
78
|
end
|
38
79
|
|
39
80
|
it "should add a Proc that just calls the block in params to the @@callbacks" do
|
40
|
-
Superfeedr.
|
81
|
+
Superfeedr.add_feed(@node, &@block)
|
41
82
|
Superfeedr.callbacks[@mock_stanza.id][:method].should == Superfeedr.method(:on_subscribe)
|
42
83
|
Superfeedr.callbacks[@mock_stanza.id][:param].should == @block
|
43
84
|
end
|
44
85
|
|
45
86
|
it "should send the stanza" do
|
46
87
|
Superfeedr.should_receive(:send).with(@mock_stanza).and_return(true)
|
47
|
-
Superfeedr.
|
88
|
+
Superfeedr.add_feed(@node, &@block)
|
48
89
|
end
|
49
90
|
end
|
50
91
|
|
51
|
-
describe "
|
92
|
+
describe "remove_feed" do
|
52
93
|
before(:each) do
|
53
94
|
Superfeedr.stub!(:connection).and_return(@mock_connection)
|
54
95
|
Superfeedr.stub!(:send).and_return(true)
|
@@ -63,28 +104,28 @@ describe Superfeedr do
|
|
63
104
|
it "should raise an error if not connected" do
|
64
105
|
Superfeedr.should_receive(:connection).and_return(nil)
|
65
106
|
lambda {
|
66
|
-
Superfeedr.
|
107
|
+
Superfeedr.remove_feed(@node, &@block)
|
67
108
|
}.should raise_error(Superfeedr::NotConnected)
|
68
109
|
end
|
69
110
|
|
70
111
|
it "should create a new SubscribeQueryStanza with the right url" do
|
71
112
|
UnsubscribeQueryStanza.should_receive(:new).with({:node => @node, :from => @mock_connection.jid}).and_return(@mock_stanza)
|
72
|
-
Superfeedr.
|
113
|
+
Superfeedr.remove_feed(@node, &@block)
|
73
114
|
end
|
74
115
|
|
75
116
|
it "should add a Proc that just calls the block in params to the @@callbacks" do
|
76
|
-
Superfeedr.
|
117
|
+
Superfeedr.remove_feed(@node, &@block)
|
77
118
|
Superfeedr.callbacks[@mock_stanza.id][:method].should == Superfeedr.method(:on_unsubscribe)
|
78
119
|
Superfeedr.callbacks[@mock_stanza.id][:param].should == @block
|
79
120
|
end
|
80
121
|
|
81
122
|
it "should send the stanza" do
|
82
123
|
Superfeedr.should_receive(:send).with(@mock_stanza).and_return(true)
|
83
|
-
Superfeedr.
|
124
|
+
Superfeedr.remove_feed(@node, &@block)
|
84
125
|
end
|
85
126
|
end
|
86
127
|
|
87
|
-
describe "
|
128
|
+
describe "subscriptions_by_page" do
|
88
129
|
before(:each) do
|
89
130
|
Superfeedr.stub!(:connection).and_return(@mock_connection)
|
90
131
|
Superfeedr.stub!(:send).and_return(true)
|
@@ -99,17 +140,17 @@ describe Superfeedr do
|
|
99
140
|
it "should raise an error if not connected" do
|
100
141
|
Superfeedr.should_receive(:connection).and_return(nil)
|
101
142
|
lambda {
|
102
|
-
Superfeedr.
|
143
|
+
Superfeedr.subscriptions_by_page(@page, &@block)
|
103
144
|
}.should raise_error(Superfeedr::NotConnected)
|
104
145
|
end
|
105
146
|
|
106
147
|
it "should create a new SubscribeQueryStanza with the right url" do
|
107
148
|
SubscriptionsQueryStanza.should_receive(:new).with({:page => @page, :from => @mock_connection.jid}).and_return(@mock_stanza)
|
108
|
-
Superfeedr.
|
149
|
+
Superfeedr.subscriptions_by_page(@page, &@block)
|
109
150
|
end
|
110
151
|
|
111
152
|
it "should add a Proc that just calls the block in params to the @@callbacks" do
|
112
|
-
Superfeedr.
|
153
|
+
Superfeedr.subscriptions_by_page(@page, &@block)
|
113
154
|
Superfeedr.callbacks[@mock_stanza.id][:method].should == Superfeedr.method(:on_subscriptions)
|
114
155
|
Superfeedr.callbacks[@mock_stanza.id][:param].should == @block
|
115
156
|
|
@@ -117,11 +158,10 @@ describe Superfeedr do
|
|
117
158
|
|
118
159
|
it "should send the stanza" do
|
119
160
|
Superfeedr.should_receive(:send).with(@mock_stanza).and_return(true)
|
120
|
-
Superfeedr.
|
161
|
+
Superfeedr.subscriptions_by_page(@page, &@block)
|
121
162
|
end
|
122
163
|
end
|
123
164
|
|
124
|
-
|
125
165
|
describe "on_subscribe" do
|
126
166
|
it "should call the block with true if the stanza type is 'result'" do
|
127
167
|
xml = <<-EOXML
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: superfeedr-superfeedr-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- julien Genestoux
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-06-
|
12
|
+
date: 2009-06-06 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -46,6 +46,7 @@ files:
|
|
46
46
|
- README.rdoc
|
47
47
|
- Rakefile
|
48
48
|
- VERSION.yml
|
49
|
+
- lib/config.yaml
|
49
50
|
- lib/stanzas/iq_query_stanza.rb
|
50
51
|
- lib/stanzas/notification_stanza.rb
|
51
52
|
- lib/stanzas/subscribe_query_stanza.rb
|