devfu-twitter-search-watcher 0.1.1 → 0.1.2
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 +1 -1
- data/lib/twitter-search-watcher.rb +68 -1
- data/spec/search_spec.rb +131 -0
- data/spec/search_url_generation_spec.rb +72 -0
- data/spec/spec_helper.rb +15 -14
- metadata +6 -4
- data/spec/twitter_search_watcher_spec.rb +0 -140
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
@@ -1,5 +1,10 @@
|
|
1
1
|
%w( rubygems cgi json open-uri ostruct ).each {|lib| require lib }
|
2
2
|
|
3
|
+
#
|
4
|
+
# = Usage
|
5
|
+
#
|
6
|
+
# coming soon
|
7
|
+
#
|
3
8
|
class TwitterSearchWatcher
|
4
9
|
|
5
10
|
TWITTER_SEARCH_URL = 'http://search.twitter.com/search.json'
|
@@ -87,7 +92,7 @@ class TwitterSearchWatcher
|
|
87
92
|
|
88
93
|
# Performs a search. Accepts the same parameters as #search_url
|
89
94
|
def search! additional_parameters = nil
|
90
|
-
|
95
|
+
json search_url(additional_parameters)
|
91
96
|
end
|
92
97
|
|
93
98
|
# Performs a search, given the response from another search.
|
@@ -114,6 +119,68 @@ class TwitterSearchWatcher
|
|
114
119
|
search!( (additional_parameters || {}).merge( :page => (response['page'] + 1), :max_id => response['max_id'] ) ) if response['next_page']
|
115
120
|
end
|
116
121
|
|
122
|
+
# Performs a search! and search_more! as needed to return a response with *all* pages of tweets.
|
123
|
+
#
|
124
|
+
# This respects max_pages and will only make max_pages number of additional requests to get paginated tweets.
|
125
|
+
#
|
126
|
+
# The response object returned is similar to the responses returned by all other methods, but we only
|
127
|
+
# currently have a 'results' key on the Hash returned. If you're used to getting some of the other keys
|
128
|
+
# returned by the other methods (which Twitter returns), be warned!
|
129
|
+
#
|
130
|
+
# To get the tweets off of the response:
|
131
|
+
#
|
132
|
+
# tweets = watcher.search_with_pagination!['results']
|
133
|
+
def search_with_pagination! additional_parameters = nil
|
134
|
+
response = search! additional_parameters
|
135
|
+
|
136
|
+
max_requests = max_pages
|
137
|
+
max_requests = additional_parameters[:max_pages] if additional_parameters && additional_parameters[:max_pages]
|
138
|
+
|
139
|
+
tweets = { 'results' => [] }
|
140
|
+
pages_requested_so_far = 0
|
141
|
+
|
142
|
+
if response['next_page']
|
143
|
+
while response = search_more!(response)
|
144
|
+
tweets['results'] << response['results']
|
145
|
+
pages_requested_so_far += 1
|
146
|
+
|
147
|
+
break if max_requests && pages_requested_so_far >= max_requests
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
tweets
|
152
|
+
end
|
153
|
+
|
154
|
+
# Helper to do an HTTP GET request and return the response body
|
155
|
+
def get url
|
156
|
+
TwitterSearchWatcher.get url, 'User-Agent' => user_agent
|
157
|
+
end
|
158
|
+
|
159
|
+
# Helper to do an HTTP GET request and return the response body
|
160
|
+
def self.get url, options = {}
|
161
|
+
open( url, { 'User-Agent' => DEFAULT_USER_AGENT }.merge(options) ).read
|
162
|
+
end
|
163
|
+
|
164
|
+
# Helper to #get a url and return the response body parsed as JSON
|
165
|
+
def json url
|
166
|
+
JSON.parse get(url)
|
167
|
+
end
|
168
|
+
|
169
|
+
# Helper to #get a url and return the response body parsed as JSON
|
170
|
+
def self.json url
|
171
|
+
JSON.parse get(url)
|
172
|
+
end
|
173
|
+
|
174
|
+
# Instantiates a new TwitterSearchWatcher given the search_string and options and then
|
175
|
+
# calls search_with_pagination! on the instance, returning the response.
|
176
|
+
#
|
177
|
+
# tweets_json = TwitterSearchWatcher.search_with_pagination!('foo')['results']
|
178
|
+
#
|
179
|
+
def self.search_with_pagination! search_string, options = nil
|
180
|
+
watcher = TwitterSearchWatcher.new search_string, options
|
181
|
+
watcher.search_with_pagination!
|
182
|
+
end
|
183
|
+
|
117
184
|
# Instantiates a new TwitterSearchWatcher given the search_string and options and then
|
118
185
|
# calls #watch on the instance using the block given.
|
119
186
|
def self.watch! search_string, options = nil, &block
|
data/spec/search_spec.rb
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe TwitterSearchWatcher, 'searching' do
|
4
|
+
|
5
|
+
it 'should be able to execute search' do
|
6
|
+
watcher = TSW.new 'chunky bacon'
|
7
|
+
TSW.should_receive(:get).with(watcher.search_url, 'User-Agent' => watcher.user_agent).and_return(fake_response)
|
8
|
+
response = watcher.search!
|
9
|
+
response.should be_a_kind_of(Hash)
|
10
|
+
response['query'].should == 'remitaylor' # make sure it's getting our fake date
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should be able to search!(:rpp => 15) ... inotherwords, should be able to pass additional parameters to search!' do
|
14
|
+
watcher = TSW.new 'foo'
|
15
|
+
|
16
|
+
TSW.should_receive(:get).with(/99/, 'User-Agent' => watcher.user_agent).and_return(fake_response)
|
17
|
+
watcher.search! :rpp => 99
|
18
|
+
|
19
|
+
TSW.should_receive(:get).with(/foo/, 'User-Agent' => watcher.user_agent).and_return(fake_response)
|
20
|
+
watcher.search! :rpp => 99
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should be able to search!(:rpp => nil) to override an existing parameter' do
|
24
|
+
watcher = TSW.new 'foo', :rpp => 50
|
25
|
+
watcher.search_url.should include('rpp=50')
|
26
|
+
watcher.search_url.should include('q=foo')
|
27
|
+
|
28
|
+
# double check that normal overrides are working properly
|
29
|
+
watcher.search_url( :rpp => 99 ).should_not include('rpp=50')
|
30
|
+
watcher.search_url( :rpp => 99 ).should include('rpp=99')
|
31
|
+
|
32
|
+
watcher.search_url( :rpp => nil ).should_not include('rpp=50')
|
33
|
+
watcher.search_url( :rpp => nil ).should_not include('rpp')
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should be able to search_newer!(result_set) to get results newer than a given result set' do
|
37
|
+
watcher = TSW.new 'chunky bacon'
|
38
|
+
TSW.should_receive(:get).with(watcher.search_url, 'User-Agent' => watcher.user_agent).and_return(fake_response(:max_id => 5555))
|
39
|
+
response = watcher.search!
|
40
|
+
|
41
|
+
TSW.should_receive(:get).with(/since_id=5555/, 'User-Agent' => watcher.user_agent).and_return(fake_response)
|
42
|
+
watcher.search_newer! response
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should be able to search_more!(result_set) to get paginated results' do
|
46
|
+
with_next_page = fake_response :page => 1, :max_id => 1, :next_page => '?page=2&max_id=444&q=chunky+bacon'
|
47
|
+
without_next_page = fake_response :page => 1, :max_id => 2, :next_page => nil
|
48
|
+
|
49
|
+
watcher = TSW.new 'chunky bacon'
|
50
|
+
|
51
|
+
TSW.should_receive(:get).once.with(watcher.search_url, 'User-Agent' => watcher.user_agent).and_return(with_next_page)
|
52
|
+
response = watcher.search!
|
53
|
+
|
54
|
+
TSW.should_receive(:get).once.with(/page=2/, 'User-Agent' => watcher.user_agent).and_return(without_next_page)
|
55
|
+
response2 = watcher.search_more! response
|
56
|
+
|
57
|
+
watcher.should_not_receive(:open)
|
58
|
+
watcher.search_more!( response2 ).should be_nil # because no next_page
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should be able to watch! on a watcher' do
|
62
|
+
# it's a PITA to test the loop, so i'm not currently testing that check_every and max_pages are actually used correctly
|
63
|
+
watcher = TSW.new 'chunky bacon', :check_every => 120, :max_pages => 5
|
64
|
+
watcher.check_every.should == 120
|
65
|
+
watcher.max_pages.should == 5
|
66
|
+
watcher.should respond_to(:watch!)
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'should be able to TSW.search_with_pagination!' do
|
70
|
+
_2_pages_left = fake_response :page => 1, :max_id => 1, :next_page => '?page=2&max_id=1&q=foo'
|
71
|
+
_1_page_left = fake_response :page => 2, :max_id => 2, :next_page => '?page=3&max_id=1&q=foo'
|
72
|
+
_0_pages_left = fake_response :page => 3, :max_id => 3, :next_page => nil
|
73
|
+
|
74
|
+
TSW.should_receive(:get).once.with(/q=foo/, anything).and_return(_2_pages_left)
|
75
|
+
TSW.should_receive(:get).once.with(/page=2/, anything).and_return(_1_page_left)
|
76
|
+
TSW.should_receive(:get).once.with(/page=3/, anything).and_return(_0_pages_left)
|
77
|
+
|
78
|
+
TSW.search_with_pagination! 'foo'
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'should be able to search_with_pagination!' do
|
82
|
+
_2_pages_left = fake_response :page => 1, :max_id => 1, :next_page => '?page=2&max_id=1&q=foo'
|
83
|
+
_1_page_left = fake_response :page => 2, :max_id => 2, :next_page => '?page=3&max_id=1&q=foo'
|
84
|
+
_0_pages_left = fake_response :page => 3, :max_id => 3, :next_page => nil
|
85
|
+
|
86
|
+
watcher = TSW.new 'foo'
|
87
|
+
|
88
|
+
TSW.should_receive(:get).once.with(/q=foo/, anything).and_return(_2_pages_left)
|
89
|
+
TSW.should_receive(:get).once.with(/page=2/, anything).and_return(_1_page_left)
|
90
|
+
TSW.should_receive(:get).once.with(/page=3/, anything).and_return(_0_pages_left)
|
91
|
+
|
92
|
+
watcher.search_with_pagination!
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'search_with_pagination! should respect max_pages (instance)' do
|
96
|
+
_2_pages_left = fake_response :page => 1, :max_id => 1, :next_page => '?page=2&max_id=1&q=foo'
|
97
|
+
_1_page_left = fake_response :page => 2, :max_id => 2, :next_page => '?page=3&max_id=1&q=foo'
|
98
|
+
_0_pages_left = fake_response :page => 3, :max_id => 3, :next_page => nil
|
99
|
+
|
100
|
+
watcher = TSW.new 'foo', :max_pages => 1 # it'll only get 1 page *after* doing the original search! request
|
101
|
+
|
102
|
+
TSW.should_receive(:get).once.with(/q=foo/, 'User-Agent' => watcher.user_agent).and_return(_2_pages_left)
|
103
|
+
TSW.should_receive(:get).once.with(/page=2/, 'User-Agent' => watcher.user_agent).and_return(_1_page_left)
|
104
|
+
watcher.should_not_receive(:open)
|
105
|
+
|
106
|
+
watcher.search_with_pagination!
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'search_with_pagination! should respect max_pages (override)' do
|
110
|
+
_2_pages_left = fake_response :page => 1, :max_id => 1, :next_page => '?page=2&max_id=1&q=foo'
|
111
|
+
_1_page_left = fake_response :page => 2, :max_id => 2, :next_page => '?page=3&max_id=1&q=foo'
|
112
|
+
_0_pages_left = fake_response :page => 3, :max_id => 3, :next_page => nil
|
113
|
+
|
114
|
+
watcher = TSW.new 'foo', :max_pages => 99
|
115
|
+
|
116
|
+
TSW.should_receive(:get).once.with(watcher.search_url, 'User-Agent' => watcher.user_agent).and_return(_2_pages_left)
|
117
|
+
TSW.should_receive(:get).once.with(/page=2/, 'User-Agent' => watcher.user_agent).and_return(_1_page_left)
|
118
|
+
watcher.should_not_receive(:open)
|
119
|
+
|
120
|
+
watcher.search_with_pagination! :max_pages => 1
|
121
|
+
end
|
122
|
+
|
123
|
+
# Ideas ...
|
124
|
+
|
125
|
+
#it 'should be able to search! (with pagination)'
|
126
|
+
#it 'should default to getting additional pages when doing a search!'
|
127
|
+
#it 'default rpp should be 100 (the max)'
|
128
|
+
#it 'should have a nice method to call that will make a new watcher and start watching'
|
129
|
+
#it 'should be able to watch! with pagination (will search! while there is a :next_page to request)'
|
130
|
+
|
131
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe TwitterSearchWatcher, '#search_url generation' do
|
4
|
+
|
5
|
+
it 'should be able to initialize with a search string' do
|
6
|
+
TwitterSearchWatcher.new('foo').search_url.should == 'http://search.twitter.com/search.json?q=foo'
|
7
|
+
TwitterSearchWatcher.new('#foo').search_url.should == 'http://search.twitter.com/search.json?q=%23foo'
|
8
|
+
TwitterSearchWatcher.new('@foo').search_url.should == 'http://search.twitter.com/search.json?q=%40foo'
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should be able to search for replies :to someone' do
|
12
|
+
TwitterSearchWatcher.new( :to => 'remitaylor' ).search_url.should end_with('search.json?to=remitaylor')
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should be able to search for tweets :from someone' do
|
16
|
+
TwitterSearchWatcher.new( :from => 'remitaylor' ).search_url.should end_with('search.json?from=remitaylor')
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should be able to search for tweets :from someone that are :to someone' do
|
20
|
+
watcher = TwitterSearchWatcher.new :from => 'remitaylor', :to => 'wickd_wanda'
|
21
|
+
watcher.search_url.should include('from=remitaylor')
|
22
|
+
watcher.search_url.should include('to=wickd_wanda')
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should have a good default User-Agent' do
|
26
|
+
watcher = TwitterSearchWatcher.new
|
27
|
+
watcher.user_agent.should == 'TwitterSearchWatcher RubyGem http://github.com/devfu/twitter-search-watcher'
|
28
|
+
watcher.user_agent = 'foo'
|
29
|
+
watcher.user_agent.should == 'foo'
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should be able to override User-Agent' do
|
33
|
+
watcher = TwitterSearchWatcher.new 'x', :user_agent => 'bar'
|
34
|
+
watcher.search_url.should == 'http://search.twitter.com/search.json?q=x'
|
35
|
+
watcher.user_agent.should == 'bar'
|
36
|
+
|
37
|
+
watcher = TwitterSearchWatcher.new :user_agent => 'bar'
|
38
|
+
watcher.search_url.should == 'http://search.twitter.com/search.json?'
|
39
|
+
watcher.user_agent.should == 'bar'
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should allow quotes in query string' do
|
43
|
+
TwitterSearchWatcher.new('"hello there"').search_url.should end_with('q="hello+there"')
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should replace all spaces in query string with + signs' do
|
47
|
+
TwitterSearchWatcher.new('hello there').search_url.should end_with('q=hello+there')
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should be able to request a particular :page (pagination)' do
|
51
|
+
TwitterSearchWatcher.new( 'foo', :page => 2 ).search_url.should include("page=2")
|
52
|
+
TwitterSearchWatcher.new( 'foo', :page => 2 ).search_url.should include("q=foo")
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should be able to request since a particular twitter ID (:since_id)' do
|
56
|
+
TwitterSearchWatcher.new( 'foo', :since_id => 1234 ).search_url.should include("since_id=1234")
|
57
|
+
TwitterSearchWatcher.new( 'foo', :since_id => 1234 ).search_url.should include("q=foo")
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should be able to request with a particular :max_id (used with :page)' do
|
61
|
+
TwitterSearchWatcher.new( 'foo', :max_id => 4321 ).search_url.should include("max_id=4321")
|
62
|
+
TwitterSearchWatcher.new( 'foo', :max_id => 4321 ).search_url.should include("q=foo")
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should be able to request with a particular :rpp (results_per_page)' do
|
66
|
+
TwitterSearchWatcher.new( 'foo', :rpp => 90 ).search_url.should include("rpp=90")
|
67
|
+
TwitterSearchWatcher.new( 'foo', :rpp => 90 ).search_url.should include("q=foo")
|
68
|
+
|
69
|
+
lambda { TwitterSearchWatcher.new( 'foo', :rpp => 101 ) }.should raise_error(/100/)
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,6 +2,9 @@ require File.dirname(__FILE__) + '/../lib/twitter-search-watcher'
|
|
2
2
|
require 'rubygems'
|
3
3
|
require 'spec'
|
4
4
|
require 'ostruct'
|
5
|
+
require 'activesupport' # for stringify_keys
|
6
|
+
|
7
|
+
TSW = TwitterSearchWatcher
|
5
8
|
|
6
9
|
def readable value
|
7
10
|
OpenStruct.new({ :read => value })
|
@@ -12,20 +15,18 @@ def fake_response_for watcher
|
|
12
15
|
end
|
13
16
|
|
14
17
|
def fake_response options = {}
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
}.merge(options).to_json
|
28
|
-
)
|
18
|
+
{
|
19
|
+
'max_id' => 1234,
|
20
|
+
'since_id' => 0,
|
21
|
+
'total' => 15, # doesn't appear if next_page present?
|
22
|
+
'next_page' => '?page=2&max_id=1234&q=remitaylor', # only appears if there are additional pages
|
23
|
+
'refresh_url' => '?since_id=1234&q=remitaylor',
|
24
|
+
'page' => 1,
|
25
|
+
'results_per_page' => 15,
|
26
|
+
'completed_in' => 0.1234,
|
27
|
+
'query' => 'remitaylor',
|
28
|
+
'results' => fake_tweets
|
29
|
+
}.merge(options.stringify_keys).to_json
|
29
30
|
end
|
30
31
|
|
31
32
|
def fake_tweets
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: devfu-twitter-search-watcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- remi
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-09-04 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -26,9 +26,10 @@ files:
|
|
26
26
|
- Rakefile
|
27
27
|
- VERSION
|
28
28
|
- lib/twitter-search-watcher.rb
|
29
|
+
- spec/search_spec.rb
|
30
|
+
- spec/search_url_generation_spec.rb
|
29
31
|
- spec/spec.opts
|
30
32
|
- spec/spec_helper.rb
|
31
|
-
- spec/twitter_search_watcher_spec.rb
|
32
33
|
has_rdoc: false
|
33
34
|
homepage: http://github.com/devfu/twitter-search-watcher
|
34
35
|
licenses:
|
@@ -57,5 +58,6 @@ signing_key:
|
|
57
58
|
specification_version: 3
|
58
59
|
summary: for watching a Twitter search
|
59
60
|
test_files:
|
60
|
-
- spec/
|
61
|
+
- spec/search_spec.rb
|
61
62
|
- spec/spec_helper.rb
|
63
|
+
- spec/search_url_generation_spec.rb
|
@@ -1,140 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
|
3
|
-
describe TwitterSearchWatcher do
|
4
|
-
|
5
|
-
it 'should be able to initialize with a search string' do
|
6
|
-
TwitterSearchWatcher.new('foo').search_url.should == 'http://search.twitter.com/search.json?q=foo'
|
7
|
-
TwitterSearchWatcher.new('#foo').search_url.should == 'http://search.twitter.com/search.json?q=%23foo'
|
8
|
-
TwitterSearchWatcher.new('@foo').search_url.should == 'http://search.twitter.com/search.json?q=%40foo'
|
9
|
-
end
|
10
|
-
|
11
|
-
it 'should be able to search for replies :to someone' do
|
12
|
-
TwitterSearchWatcher.new( :to => 'remitaylor' ).search_url.should end_with('search.json?to=remitaylor')
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'should be able to search for tweets :from someone' do
|
16
|
-
TwitterSearchWatcher.new( :from => 'remitaylor' ).search_url.should end_with('search.json?from=remitaylor')
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'should be able to search for tweets :from someone that are :to someone' do
|
20
|
-
watcher = TwitterSearchWatcher.new :from => 'remitaylor', :to => 'wickd_wanda'
|
21
|
-
watcher.search_url.should include('from=remitaylor')
|
22
|
-
watcher.search_url.should include('to=wickd_wanda')
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'should have a good default User-Agent' do
|
26
|
-
watcher = TwitterSearchWatcher.new
|
27
|
-
watcher.user_agent.should == 'TwitterSearchWatcher RubyGem http://github.com/devfu/twitter-search-watcher'
|
28
|
-
watcher.user_agent = 'foo'
|
29
|
-
watcher.user_agent.should == 'foo'
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'should be able to override User-Agent' do
|
33
|
-
watcher = TwitterSearchWatcher.new 'x', :user_agent => 'bar'
|
34
|
-
watcher.search_url.should == 'http://search.twitter.com/search.json?q=x'
|
35
|
-
watcher.user_agent.should == 'bar'
|
36
|
-
|
37
|
-
watcher = TwitterSearchWatcher.new :user_agent => 'bar'
|
38
|
-
watcher.search_url.should == 'http://search.twitter.com/search.json?'
|
39
|
-
watcher.user_agent.should == 'bar'
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'should allow quotes in query string' do
|
43
|
-
TwitterSearchWatcher.new('"hello there"').search_url.should end_with('q="hello+there"')
|
44
|
-
end
|
45
|
-
|
46
|
-
it 'should replace all spaces in query string with + signs' do
|
47
|
-
TwitterSearchWatcher.new('hello there').search_url.should end_with('q=hello+there')
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'should be able to execute search' do
|
51
|
-
watcher = TwitterSearchWatcher.new 'chunky bacon'
|
52
|
-
watcher.should_receive(:open).with(watcher.search_url, 'User-Agent' => watcher.user_agent).and_return(fake_response)
|
53
|
-
response = watcher.search!
|
54
|
-
response.should be_a_kind_of(Hash)
|
55
|
-
response['query'].should == 'remitaylor' # make sure it's getting our fake date
|
56
|
-
end
|
57
|
-
|
58
|
-
it 'should be able to request a particular :page (pagination)' do
|
59
|
-
TwitterSearchWatcher.new( 'foo', :page => 2 ).search_url.should include("page=2")
|
60
|
-
TwitterSearchWatcher.new( 'foo', :page => 2 ).search_url.should include("q=foo")
|
61
|
-
end
|
62
|
-
|
63
|
-
it 'should be able to request since a particular twitter ID (:since_id)' do
|
64
|
-
TwitterSearchWatcher.new( 'foo', :since_id => 1234 ).search_url.should include("since_id=1234")
|
65
|
-
TwitterSearchWatcher.new( 'foo', :since_id => 1234 ).search_url.should include("q=foo")
|
66
|
-
end
|
67
|
-
|
68
|
-
it 'should be able to request with a particular :max_id (used with :page)' do
|
69
|
-
TwitterSearchWatcher.new( 'foo', :max_id => 4321 ).search_url.should include("max_id=4321")
|
70
|
-
TwitterSearchWatcher.new( 'foo', :max_id => 4321 ).search_url.should include("q=foo")
|
71
|
-
end
|
72
|
-
|
73
|
-
it 'should be able to request with a particular :rpp (results_per_page)' do
|
74
|
-
TwitterSearchWatcher.new( 'foo', :rpp => 90 ).search_url.should include("rpp=90")
|
75
|
-
TwitterSearchWatcher.new( 'foo', :rpp => 90 ).search_url.should include("q=foo")
|
76
|
-
|
77
|
-
lambda { TwitterSearchWatcher.new( 'foo', :rpp => 101 ) }.should raise_error(/100/)
|
78
|
-
end
|
79
|
-
|
80
|
-
it 'should be able to search!(:rpp => 15) ... inotherwords, should be able to pass additional parameters to search!' do
|
81
|
-
watcher = TwitterSearchWatcher.new 'foo'
|
82
|
-
|
83
|
-
watcher.should_receive(:open).with(/99/, 'User-Agent' => watcher.user_agent).and_return(fake_response)
|
84
|
-
watcher.search! :rpp => 99
|
85
|
-
|
86
|
-
watcher.should_receive(:open).with(/foo/, 'User-Agent' => watcher.user_agent).and_return(fake_response)
|
87
|
-
watcher.search! :rpp => 99
|
88
|
-
end
|
89
|
-
|
90
|
-
it 'should be able to search!(:rpp => nil) to override an existing parameter' do
|
91
|
-
watcher = TwitterSearchWatcher.new 'foo', :rpp => 50
|
92
|
-
watcher.search_url.should include('rpp=50')
|
93
|
-
watcher.search_url.should include('q=foo')
|
94
|
-
|
95
|
-
# double check that normal overrides are working properly
|
96
|
-
watcher.search_url( :rpp => 99 ).should_not include('rpp=50')
|
97
|
-
watcher.search_url( :rpp => 99 ).should include('rpp=99')
|
98
|
-
|
99
|
-
watcher.search_url( :rpp => nil ).should_not include('rpp=50')
|
100
|
-
watcher.search_url( :rpp => nil ).should_not include('rpp')
|
101
|
-
end
|
102
|
-
|
103
|
-
it 'should be able to search_newer!(result_set) to get results newer than a given result set' do
|
104
|
-
watcher = TwitterSearchWatcher.new 'chunky bacon'
|
105
|
-
watcher.should_receive(:open).with(watcher.search_url, 'User-Agent' => watcher.user_agent).and_return(fake_response(:max_id => 5555))
|
106
|
-
response = watcher.search!
|
107
|
-
|
108
|
-
watcher.should_receive(:open).with(/since_id=5555/, 'User-Agent' => watcher.user_agent).and_return(fake_response)
|
109
|
-
watcher.search_newer! response
|
110
|
-
end
|
111
|
-
|
112
|
-
it 'should be able to search_more!(result_set) to get paginated results' do
|
113
|
-
watcher = TwitterSearchWatcher.new 'chunky bacon'
|
114
|
-
watcher.should_receive(:open).with(watcher.search_url, 'User-Agent' => watcher.user_agent).and_return(fake_response(:page => 1, :max_id => 444, :next_page => '?page=2&max_id=444&q=chunky+bacon'))
|
115
|
-
response = watcher.search!
|
116
|
-
|
117
|
-
watcher.should_receive(:open).with(/page=2/, 'User-Agent' => watcher.user_agent).and_return(fake_response(:page => 2, :max_id => 445, :next_page => nil))
|
118
|
-
response2 = watcher.search_more! response
|
119
|
-
watcher.should_receive(:open).with(/max_id=444/, 'User-Agent' => watcher.user_agent).and_return(fake_response(:page => 2, :max_id => 445, :next_page => nil))
|
120
|
-
response2 = watcher.search_more! response
|
121
|
-
|
122
|
-
watcher.should_not_receive(:open)
|
123
|
-
watcher.search_more!( response2 ).should be_nil # because no next_page
|
124
|
-
end
|
125
|
-
|
126
|
-
it 'should be able to watch! on a watcher' do
|
127
|
-
# it's a PITA to test the loop, so i'm not currently testing that check_every and max_pages are actually used correctly
|
128
|
-
watcher = TwitterSearchWatcher.new 'chunky bacon', :check_every => 120, :max_pages => 5
|
129
|
-
watcher.check_every.should == 120
|
130
|
-
watcher.max_pages.should == 5
|
131
|
-
watcher.should respond_to(:watch!)
|
132
|
-
end
|
133
|
-
|
134
|
-
it 'default rpp should be 100 (the max)'
|
135
|
-
|
136
|
-
it 'should have a nice method to call that will make a new watcher and start watching'
|
137
|
-
|
138
|
-
it 'should be able to watch! with pagination (will search! while there is a :next_page to request)'
|
139
|
-
|
140
|
-
end
|