superfeedr-superfeedr-ruby 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +1 -1
- data/lib/stanzas/subscribe_query_stanza.rb +19 -11
- data/lib/stanzas/unsubscribe_query_stanza.rb +18 -9
- data/lib/superfeedr.rb +14 -15
- data/spec/stanzas/subscribe_stanza_spec.rb +2 -2
- data/spec/stanzas/unsubscribe_stanza_spec.rb +2 -2
- data/spec/superfeedr_ruby_spec.rb +24 -26
- metadata +2 -2
data/VERSION.yml
CHANGED
@@ -1,18 +1,26 @@
|
|
1
|
-
class SubscribeQueryStanza < IqQueryStanza
|
1
|
+
class SubscribeQueryStanza < IqQueryStanza
|
2
2
|
|
3
|
-
def initialize(params)
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
def initialize(params)
|
4
|
+
raise NoFeedToSubscribe if params[:nodes].nil? or params[:nodes].empty?
|
5
|
+
raise TooManyFeeds if params[:nodes].size > 30
|
6
|
+
super(params.merge({:type => :set}))
|
7
|
+
@pubsub = Nokogiri::XML::Node.new("pubsub", @doc)
|
8
|
+
@pubsub["xmlns"] = "http://jabber.org/protocol/pubsub"
|
9
|
+
params[:nodes].each do |node|
|
10
|
+
add_node(node)
|
11
|
+
end
|
12
|
+
@iq.add_child(@pubsub)
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_node(node)
|
7
16
|
subscribe = Nokogiri::XML::Node.new("subscribe", @doc)
|
8
|
-
subscribe["node"] =
|
17
|
+
subscribe["node"] = node
|
9
18
|
subscribe["jid"] = from.split("/").first
|
10
|
-
pubsub.add_child(subscribe)
|
11
|
-
|
12
|
-
end
|
19
|
+
@pubsub.add_child(subscribe)
|
20
|
+
end
|
13
21
|
|
14
|
-
def
|
15
|
-
@
|
22
|
+
def nodes
|
23
|
+
@pubsub.children.map {|c| c["node"]}
|
16
24
|
end
|
17
25
|
|
18
26
|
end
|
@@ -1,18 +1,27 @@
|
|
1
1
|
class UnsubscribeQueryStanza < IqQueryStanza
|
2
2
|
|
3
3
|
def initialize(params)
|
4
|
+
raise NoFeedToSubscribe if params[:nodes].nil? or params[:nodes].empty?
|
5
|
+
raise TooManyFeeds if params[:nodes].size > 30
|
4
6
|
super(params.merge({:type => :set}))
|
5
|
-
|
6
|
-
pubsub
|
7
|
-
|
7
|
+
|
8
|
+
@pubsub = Nokogiri::XML::Node.new("pubsub", @doc)
|
9
|
+
params[:nodes].each do |node|
|
10
|
+
add_node(node)
|
11
|
+
end
|
12
|
+
@pubsub["xmlns"] = "http://jabber.org/protocol/pubsub"
|
13
|
+
@iq.add_child(@pubsub)
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_node(node)
|
8
17
|
unsubscribe = Nokogiri::XML::Node.new("unsubscribe", @doc)
|
9
|
-
unsubscribe["node"] =
|
18
|
+
unsubscribe["node"] = node.to_s
|
10
19
|
unsubscribe["jid"] = from.split("/").first
|
11
|
-
pubsub.add_child(unsubscribe)
|
12
|
-
end
|
20
|
+
@pubsub.add_child(unsubscribe)
|
21
|
+
end
|
13
22
|
|
14
|
-
def
|
15
|
-
@
|
23
|
+
def nodes
|
24
|
+
@pubsub.children.map {|c| c["node"]}
|
16
25
|
end
|
17
26
|
|
18
|
-
end
|
27
|
+
end
|
data/lib/superfeedr.rb
CHANGED
@@ -6,7 +6,6 @@ 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
9
|
##
|
11
10
|
# By default, the log level is at error. You can change that at anytime in your app
|
12
11
|
Babylon.logger.level = Log4r::ERROR
|
@@ -58,13 +57,13 @@ module Superfeedr
|
|
58
57
|
end
|
59
58
|
|
60
59
|
##
|
61
|
-
# Subscribes to the multiple feeds,
|
60
|
+
# Subscribes to the multiple feeds, 30 by 30. Calls the block after each feed.
|
62
61
|
def self.subscribe(*feeds, &block)
|
63
62
|
return if feeds.flatten! == []
|
64
|
-
|
65
|
-
Superfeedr.
|
63
|
+
subset = feeds.slice!(0..29)
|
64
|
+
Superfeedr.add_feeds(subset) do |result|
|
66
65
|
subscribe(feeds, &block)
|
67
|
-
block.call(
|
66
|
+
block.call(subset)
|
68
67
|
end
|
69
68
|
end
|
70
69
|
|
@@ -72,10 +71,10 @@ module Superfeedr
|
|
72
71
|
# Ubsubscribe to multiple feeds, one by one. Calls the block after each feed.
|
73
72
|
def self.unsubscribe(*feeds, &block)
|
74
73
|
return if feeds.flatten! == []
|
75
|
-
|
76
|
-
Superfeedr.
|
74
|
+
subset = feeds.slice!(0..29)
|
75
|
+
Superfeedr.remove_feeds(subset) do |result|
|
77
76
|
unsubscribe(feeds, &block)
|
78
|
-
block.call(
|
77
|
+
block.call(subset)
|
79
78
|
end
|
80
79
|
end
|
81
80
|
|
@@ -85,8 +84,8 @@ module Superfeedr
|
|
85
84
|
Superfeedr.subscriptions_by_page(start_page) do |page, result|
|
86
85
|
if !result.empty?
|
87
86
|
subscriptions(start_page + 1, &block)
|
88
|
-
block.call(page, result)
|
89
87
|
end
|
88
|
+
block.call(page, result)
|
90
89
|
end
|
91
90
|
end
|
92
91
|
|
@@ -94,9 +93,9 @@ module Superfeedr
|
|
94
93
|
# Adds the url to the list of feeds you're monitoring. The block passed in argument will be called upon success.
|
95
94
|
# The block will take one boolen argument : true means everything went right... false means something failed!
|
96
95
|
# (Please set Babylon's log to Log4r::INFO for more info)
|
97
|
-
def self.
|
96
|
+
def self.add_feeds(feeds_url, &block)
|
98
97
|
raise NotConnected unless connection
|
99
|
-
stanza = SubscribeQueryStanza.new({:
|
98
|
+
stanza = SubscribeQueryStanza.new({:nodes => feeds_url, :from => connection.jid})
|
100
99
|
@@callbacks[stanza.id] = Hash.new
|
101
100
|
@@callbacks[stanza.id][:method] = method(:on_subscribe)
|
102
101
|
@@callbacks[stanza.id][:param] = block
|
@@ -107,9 +106,9 @@ module Superfeedr
|
|
107
106
|
# Unsubscribe from a feed. The block passed in argument will be called upon success.
|
108
107
|
# The block will take one boolen argument : true means everything went right... false means something failed!
|
109
108
|
# (Please set Babylon's log to Log4r::INFO for more info)
|
110
|
-
def self.
|
109
|
+
def self.remove_feeds(feeds_url, &block)
|
111
110
|
raise NotConnected unless connection
|
112
|
-
stanza = UnsubscribeQueryStanza.new({:
|
111
|
+
stanza = UnsubscribeQueryStanza.new({:nodes => feeds_url, :from => connection.jid})
|
113
112
|
@@callbacks[stanza.id] = Hash.new
|
114
113
|
@@callbacks[stanza.id][:method] = method(:on_unsubscribe)
|
115
114
|
@@callbacks[stanza.id][:param] = block
|
@@ -140,7 +139,7 @@ module Superfeedr
|
|
140
139
|
# Called with a response to a subscriptions listing
|
141
140
|
def self.on_subscriptions(stanza, &block)
|
142
141
|
page = stanza.xpath('//subscriptions').first["page"].to_i
|
143
|
-
feeds = stanza.xpath('//subscription').map { |s| s["node"] }
|
142
|
+
feeds = stanza.xpath('//subscription').map { |s| CGI.unescapeHTML(s["node"]) }
|
144
143
|
block.call(page, feeds)
|
145
144
|
end
|
146
145
|
|
@@ -195,7 +194,7 @@ module Superfeedr
|
|
195
194
|
@@callbacks.delete(stanza["id"])
|
196
195
|
else
|
197
196
|
if stanza.name == "message" and stanza.at("event")
|
198
|
-
@@notication_callback.call(NotificationStanza.new(stanza))
|
197
|
+
@@notication_callback.call(NotificationStanza.new(stanza)) if @@notification_callback
|
199
198
|
# Here we need to call the main notification callback!
|
200
199
|
end
|
201
200
|
end
|
@@ -5,11 +5,11 @@ describe SubscribeQueryStanza do
|
|
5
5
|
it_should_behave_like "Iq Query Stanzas"
|
6
6
|
|
7
7
|
before(:each) do
|
8
|
-
@params = { :type => "set", :from => "me@server.com/resource", :
|
8
|
+
@params = { :type => "set", :from => "me@server.com/resource", :nodes => ["http//domain.tld/feed.xml"], :type => "set", :from => "me@server.com/resource"}
|
9
9
|
end
|
10
10
|
|
11
11
|
it "should have the right node value" do
|
12
|
-
SubscribeQueryStanza.new(@params).
|
12
|
+
SubscribeQueryStanza.new(@params).nodes.should == @params[:nodes]
|
13
13
|
end
|
14
14
|
|
15
15
|
end
|
@@ -5,11 +5,11 @@ describe UnsubscribeQueryStanza do
|
|
5
5
|
it_should_behave_like "Iq Query Stanzas"
|
6
6
|
|
7
7
|
before(:each) do
|
8
|
-
@params = { :type => "set", :from => "me@server.com/resource", :
|
8
|
+
@params = { :type => "set", :from => "me@server.com/resource", :nodes => ["http//domain.tld/feed.xml", "http//domain.tld/feed2.xml"], :type => "set", :from => "me@server.com/resource"}
|
9
9
|
end
|
10
10
|
|
11
11
|
it "should have the right node value" do
|
12
|
-
UnsubscribeQueryStanza.new(@params).
|
12
|
+
UnsubscribeQueryStanza.new(@params).nodes.should == @params[:nodes]
|
13
13
|
end
|
14
14
|
|
15
15
|
end
|
@@ -14,11 +14,10 @@ describe Superfeedr do
|
|
14
14
|
|
15
15
|
|
16
16
|
describe "subscribe" do
|
17
|
-
it "should call
|
18
|
-
feeds = "a"
|
19
|
-
|
20
|
-
|
21
|
-
end
|
17
|
+
it "should call add_feeds with feeds supplied" do
|
18
|
+
feeds = ["a"] * 50
|
19
|
+
Superfeedr.should_receive(:add_feeds).with(["a"] * 30).and_yield(true)
|
20
|
+
Superfeedr.should_receive(:add_feeds).with(["a"] * 20).and_yield(true)
|
22
21
|
Superfeedr.subscribe(feeds) do |result|
|
23
22
|
result.should be_true
|
24
23
|
end
|
@@ -26,11 +25,10 @@ describe Superfeedr do
|
|
26
25
|
end
|
27
26
|
|
28
27
|
describe "unsubscribe" do
|
29
|
-
it "should call
|
30
|
-
feeds = "a"
|
31
|
-
|
32
|
-
|
33
|
-
end
|
28
|
+
it "should call remove_feeds with feeds supplied" do
|
29
|
+
feeds = ["a"] * 40
|
30
|
+
Superfeedr.should_receive(:remove_feeds).with(["a"] * 30).and_yield(true)
|
31
|
+
Superfeedr.should_receive(:remove_feeds).with(["a"] * 10).and_yield(true)
|
34
32
|
Superfeedr.unsubscribe(feeds) do |result|
|
35
33
|
result.should be_true
|
36
34
|
end
|
@@ -41,7 +39,7 @@ describe Superfeedr do
|
|
41
39
|
it "should call subscriptions_by_page for each page as long as they're not empty" do
|
42
40
|
def method_called_upon_page
|
43
41
|
end
|
44
|
-
self.should_receive(:method_called_upon_page).exactly(
|
42
|
+
self.should_receive(:method_called_upon_page).exactly(4).times
|
45
43
|
3.times do |t|
|
46
44
|
Superfeedr.should_receive(:subscriptions_by_page).with(t+1).and_yield( t+1 , ["a", "b", "c"])
|
47
45
|
end
|
@@ -53,7 +51,7 @@ describe Superfeedr do
|
|
53
51
|
end
|
54
52
|
|
55
53
|
|
56
|
-
describe "
|
54
|
+
describe "add_feeds" do
|
57
55
|
before(:each) do
|
58
56
|
Superfeedr.stub!(:connection).and_return(@mock_connection)
|
59
57
|
Superfeedr.stub!(:send).and_return(true)
|
@@ -68,35 +66,35 @@ describe Superfeedr do
|
|
68
66
|
it "should raise an error if not connected" do
|
69
67
|
Superfeedr.should_receive(:connection).and_return(nil)
|
70
68
|
lambda {
|
71
|
-
Superfeedr.
|
69
|
+
Superfeedr.add_feeds(@node, &@block)
|
72
70
|
}.should raise_error(Superfeedr::NotConnected)
|
73
71
|
end
|
74
72
|
|
75
73
|
it "should create a new SubscribeQueryStanza with the right url" do
|
76
|
-
SubscribeQueryStanza.should_receive(:new).with({:
|
77
|
-
Superfeedr.
|
74
|
+
SubscribeQueryStanza.should_receive(:new).with({:nodes => @node, :from => @mock_connection.jid}).and_return(@mock_stanza)
|
75
|
+
Superfeedr.add_feeds(@node, &@block)
|
78
76
|
end
|
79
77
|
|
80
78
|
it "should add a Proc that just calls the block in params to the @@callbacks" do
|
81
|
-
Superfeedr.
|
79
|
+
Superfeedr.add_feeds(@node, &@block)
|
82
80
|
Superfeedr.callbacks[@mock_stanza.id][:method].should == Superfeedr.method(:on_subscribe)
|
83
81
|
Superfeedr.callbacks[@mock_stanza.id][:param].should == @block
|
84
82
|
end
|
85
83
|
|
86
84
|
it "should send the stanza" do
|
87
85
|
Superfeedr.should_receive(:send).with(@mock_stanza).and_return(true)
|
88
|
-
Superfeedr.
|
86
|
+
Superfeedr.add_feeds(@node, &@block)
|
89
87
|
end
|
90
88
|
end
|
91
89
|
|
92
|
-
describe "
|
90
|
+
describe "remove_feeds" do
|
93
91
|
before(:each) do
|
94
92
|
Superfeedr.stub!(:connection).and_return(@mock_connection)
|
95
93
|
Superfeedr.stub!(:send).and_return(true)
|
96
94
|
@block = Proc.new {
|
97
95
|
|
98
96
|
}
|
99
|
-
@
|
97
|
+
@nodes = ["http://domain.com/feed.xml"]
|
100
98
|
@mock_stanza = mock(UnsubscribeQueryStanza, {:id => "123"})
|
101
99
|
UnsubscribeQueryStanza.stub!(:new).and_return(@mock_stanza)
|
102
100
|
end
|
@@ -104,24 +102,24 @@ describe Superfeedr do
|
|
104
102
|
it "should raise an error if not connected" do
|
105
103
|
Superfeedr.should_receive(:connection).and_return(nil)
|
106
104
|
lambda {
|
107
|
-
Superfeedr.
|
105
|
+
Superfeedr.remove_feeds(@nodes, &@block)
|
108
106
|
}.should raise_error(Superfeedr::NotConnected)
|
109
107
|
end
|
110
108
|
|
111
109
|
it "should create a new SubscribeQueryStanza with the right url" do
|
112
|
-
UnsubscribeQueryStanza.should_receive(:new).with({:
|
113
|
-
Superfeedr.
|
110
|
+
UnsubscribeQueryStanza.should_receive(:new).with({:nodes => @nodes, :from => @mock_connection.jid}).and_return(@mock_stanza)
|
111
|
+
Superfeedr.remove_feeds(@nodes, &@block)
|
114
112
|
end
|
115
113
|
|
116
114
|
it "should add a Proc that just calls the block in params to the @@callbacks" do
|
117
|
-
Superfeedr.
|
115
|
+
Superfeedr.remove_feeds(@nodes, &@block)
|
118
116
|
Superfeedr.callbacks[@mock_stanza.id][:method].should == Superfeedr.method(:on_unsubscribe)
|
119
117
|
Superfeedr.callbacks[@mock_stanza.id][:param].should == @block
|
120
118
|
end
|
121
119
|
|
122
120
|
it "should send the stanza" do
|
123
121
|
Superfeedr.should_receive(:send).with(@mock_stanza).and_return(true)
|
124
|
-
Superfeedr.
|
122
|
+
Superfeedr.remove_feeds(@nodes, &@block)
|
125
123
|
end
|
126
124
|
end
|
127
125
|
|
@@ -221,7 +219,7 @@ EOXML
|
|
221
219
|
<iq type="result" to="you@superfeedr.com/home" id="subman1" from="firehoser.superfeedr.com">
|
222
220
|
<pubsub>
|
223
221
|
<subscriptions page="3">
|
224
|
-
<subscription node="http://domain.tld/path/to/a/feed/atom.xml" subscription="subscribed" jid="you@superfeedr.com" />
|
222
|
+
<subscription node="http://domain.tld/path/to/a/feed/atom.xml&toto=tutu" subscription="subscribed" jid="you@superfeedr.com" />
|
225
223
|
<subscription node="http://domain2.tld/path/to/feed.rss" subscription="subscribed" jid="you@superfeedr.com" />
|
226
224
|
</subscriptions>
|
227
225
|
</pubsub>
|
@@ -230,7 +228,7 @@ EOXML
|
|
230
228
|
stanza = Nokogiri::XML(xml)
|
231
229
|
Superfeedr.on_subscriptions(stanza.root) do |page, subscriptions|
|
232
230
|
page.should == 3
|
233
|
-
subscriptions.should == ["http://domain.tld/path/to/a/feed/atom.xml", "http://domain2.tld/path/to/feed.rss"]
|
231
|
+
subscriptions.should == ["http://domain.tld/path/to/a/feed/atom.xml&toto=tutu", "http://domain2.tld/path/to/feed.rss"]
|
234
232
|
end
|
235
233
|
end
|
236
234
|
|
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.3.
|
4
|
+
version: 0.3.2
|
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-11 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|