intridea-tweetstream 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +43 -0
- data/VERSION +1 -1
- data/lib/tweetstream/client.rb +53 -2
- data/spec/spec_helper.rb +1 -1
- data/spec/tweetstream/client_spec.rb +36 -0
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -47,6 +47,49 @@ user ids:
|
|
47
47
|
The methods available to TweetStream::Client will be kept in parity
|
48
48
|
with the methods available on the Streaming API wiki page.
|
49
49
|
|
50
|
+
== Handling Deletes and Rate Limitations
|
51
|
+
|
52
|
+
Sometimes the Streaming API will send messages other than statuses.
|
53
|
+
Specifically, it does so when a status is deleted or rate limitations
|
54
|
+
have caused some tweets not to appear in the stream. To handle these,
|
55
|
+
you can use the on_delete and on_limit methods. Example:
|
56
|
+
|
57
|
+
@client = TweetStream::Client.new('user','pass')
|
58
|
+
|
59
|
+
@client.on_delete do |status_id, user_id|
|
60
|
+
Tweet.delete(status_id)
|
61
|
+
end
|
62
|
+
|
63
|
+
@client.on_limit do |skip_count|
|
64
|
+
# do something
|
65
|
+
end
|
66
|
+
|
67
|
+
@client.track('intridea')
|
68
|
+
|
69
|
+
The on_delete and on_limit methods can also be chained, like so:
|
70
|
+
|
71
|
+
TweetStream::Client.new('user','pass').on_delete{ |status_id, user_id|
|
72
|
+
Tweet.delete(status_id)
|
73
|
+
}.on_limit { |skip_count|
|
74
|
+
# do something
|
75
|
+
}.track('intridea') do |status|
|
76
|
+
# do something with the status like normal
|
77
|
+
end
|
78
|
+
|
79
|
+
You can also provide <tt>:delete</tt> and/or <tt>:limit</tt>
|
80
|
+
options when you make your method call:
|
81
|
+
|
82
|
+
TweetStream::Client.new('user','pass').track('intridea',
|
83
|
+
:delete => Proc.new{ |status_id, user_id| # do something },
|
84
|
+
:limit => Proc.new{ |skip_count| # do something }
|
85
|
+
) do |status
|
86
|
+
# do something with the status like normal
|
87
|
+
end
|
88
|
+
|
89
|
+
Twitter recommends honoring deletions as quickly as possible, and
|
90
|
+
you would likely be wise to integrate this functionality into your
|
91
|
+
application.
|
92
|
+
|
50
93
|
== Daemonizing
|
51
94
|
|
52
95
|
It is also possible to create a daemonized script quite easily
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.4
|
data/lib/tweetstream/client.rb
CHANGED
@@ -94,11 +94,62 @@ module TweetStream
|
|
94
94
|
start('statuses/filter', query_params, &block)
|
95
95
|
end
|
96
96
|
|
97
|
+
# Set a Proc to be run when a deletion notice is received
|
98
|
+
# from the Twitter stream. For example:
|
99
|
+
#
|
100
|
+
# @client = TweetStream::Client.new('user','pass')
|
101
|
+
# @client.on_delete do |status_id, user_id|
|
102
|
+
# Tweet.delete(status_id)
|
103
|
+
# end
|
104
|
+
#
|
105
|
+
# Block must take two arguments: the status id and the user id.
|
106
|
+
# If no block is given, it will return the currently set
|
107
|
+
# deletion proc. When a block is given, the TweetStream::Client
|
108
|
+
# object is returned to allow for chaining.
|
109
|
+
def on_delete(&block)
|
110
|
+
if block_given?
|
111
|
+
@on_delete = block
|
112
|
+
self
|
113
|
+
else
|
114
|
+
@on_delete
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
# Set a Proc to be run when a rate limit notice is received
|
119
|
+
# from the Twitter stream. For example:
|
120
|
+
#
|
121
|
+
# @client = TweetStream::Client.new('user','pass')
|
122
|
+
# @client.on_limit do |discarded_count|
|
123
|
+
# # Make note of discarded count
|
124
|
+
# end
|
125
|
+
#
|
126
|
+
# Block must take one argument: the number of discarded tweets.
|
127
|
+
# If no block is given, it will return the currently set
|
128
|
+
# limit proc. When a block is given, the TweetStream::Client
|
129
|
+
# object is returned to allow for chaining.
|
130
|
+
def on_limit(&block)
|
131
|
+
if block_given?
|
132
|
+
@on_limit = block
|
133
|
+
self
|
134
|
+
else
|
135
|
+
@on_limit
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
97
139
|
def start(path, query_parameters = {}, &block) #:nodoc:
|
98
|
-
|
140
|
+
delete_proc = query_parameters.delete(:delete) || self.on_delete
|
141
|
+
limit_proc = query_parameters.delete(:limit) || self.on_limit
|
99
142
|
|
143
|
+
uri = build_uri(path, query_parameters)
|
144
|
+
|
100
145
|
Yajl::HttpStream.get(uri, :symbolize_keys => true) do |hash|
|
101
|
-
|
146
|
+
if hash[:delete] && hash[:delete][:status]
|
147
|
+
delete_proc.call(hash[:delete][:status][:id], hash[:delete][:status][:user_id]) if delete_proc.is_a?(Proc)
|
148
|
+
elsif hash[:limit] && hash[:limit][:track]
|
149
|
+
limit_proc.call(hash[:limit][:track]) if limit_proc.is_a?(Proc)
|
150
|
+
elsif hash[:text] && hash[:user]
|
151
|
+
yield TweetStream::Status.new(hash)
|
152
|
+
end
|
102
153
|
end
|
103
154
|
end
|
104
155
|
|
data/spec/spec_helper.rb
CHANGED
@@ -12,7 +12,7 @@ def sample_tweets
|
|
12
12
|
@tweets
|
13
13
|
else
|
14
14
|
@tweets = []
|
15
|
-
Yajl::Parser.parse(File.open(File.dirname(__FILE__) + '/data/statuses.json', 'r')) do |hash|
|
15
|
+
Yajl::Parser.parse(File.open(File.dirname(__FILE__) + '/data/statuses.json', 'r'), :symbolize_keys => true) do |hash|
|
16
16
|
@tweets << hash
|
17
17
|
end
|
18
18
|
@tweets
|
@@ -74,6 +74,28 @@ describe TweetStream::Client do
|
|
74
74
|
end
|
75
75
|
@yielded.should be_true
|
76
76
|
end
|
77
|
+
|
78
|
+
{
|
79
|
+
:delete => {:delete => {:status => {:id => 1234, :user_id => 3}}},
|
80
|
+
:limit => {:limit => {:track => 1234}}
|
81
|
+
}.each_pair do |special_method, special_object|
|
82
|
+
it "should make a call to the #{special_method} proc if a #{special_method} object is given" do
|
83
|
+
@called = false
|
84
|
+
@proc = Proc.new{|*args| @called = true }
|
85
|
+
@client.send("on_#{special_method}", &@proc)
|
86
|
+
Yajl::HttpStream.should_receive(:get).once.with(URI.parse('http://abc:def@stream.twitter.com/1/statuses/filter.json?track=musicmonday'), :symbolize_keys => true).and_yield(special_object)
|
87
|
+
@client.track('musicmonday')
|
88
|
+
@called.should == true
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should accept a proc on a :#{special_method} option if a #{special_method} object is given" do
|
92
|
+
@called = false
|
93
|
+
@proc = Proc.new{|*args| @called = true }
|
94
|
+
Yajl::HttpStream.should_receive(:get).once.with(URI.parse('http://abc:def@stream.twitter.com/1/statuses/filter.json?track=musicmonday'), :symbolize_keys => true).and_yield(special_object)
|
95
|
+
@client.track('musicmonday', special_method => @proc)
|
96
|
+
@called.should == true
|
97
|
+
end
|
98
|
+
end
|
77
99
|
end
|
78
100
|
|
79
101
|
describe ' API methods' do
|
@@ -113,6 +135,20 @@ describe TweetStream::Client do
|
|
113
135
|
@client.filter(:follow => 123)
|
114
136
|
end
|
115
137
|
end
|
138
|
+
|
139
|
+
%w(on_delete on_limit).each do |proc_setter|
|
140
|
+
describe "##{proc_setter}" do
|
141
|
+
before do
|
142
|
+
@client = TweetStream::Client.new('abc','def')
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'should set when a block is given' do
|
146
|
+
proc = Proc.new{|a,b| puts a }
|
147
|
+
@client.send(proc_setter, &proc)
|
148
|
+
@client.send(proc_setter).should == proc
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
116
152
|
|
117
153
|
describe '#track' do
|
118
154
|
before do
|