/
search_spec.rb
365 lines (314 loc) · 11.1 KB
/
search_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
require 'helper'
describe Twitter::Search do
it "should connect using the search_endpoint configuration" do
search = Twitter::Search.new
endpoint = URI.parse(search.api_endpoint).to_s.gsub(/\/$/, '')
connection = search.send(:connection).build_url(nil).to_s
puts "URI says: #{endpoint.to_s}"
puts "Connection says: #{connection.to_s}"
connection.should == endpoint
end
context "with module configuration" do
before do
@keys = Twitter::Config::VALID_OPTIONS_KEYS
Twitter.configure do |config|
@keys.each do |key|
config.send("#{key}=", key)
end
end
end
after do
Twitter.reset
end
it "should inherit module configuration" do
client = Twitter::Search.new
@keys.each do |key|
client.send(key).should == key
end
end
context "with class configuration" do
before do
@configuration = {
:connection_options => {:timeout => 10},
:consumer_key => 'CK',
:consumer_secret => 'CS',
:oauth_token => 'OT',
:oauth_token_secret => 'OS',
:adapter => :typhoeus,
:endpoint => 'http://tumblr.com/',
:gateway => 'apigee-32234.apigee.com',
:proxy => 'http://erik:sekret@proxy.example.com:8080',
:search_endpoint => 'http://google.com/',
:media_endpoint => 'https://upload.twitter.com/',
:user_agent => 'Custom User Agent',
}
end
context "during initialization"
it "should override module configuration" do
client = Twitter::Search.new(@configuration)
@keys.each do |key|
client.send(key).should == @configuration[key]
end
end
context "after initilization" do
it "should override module configuration after initialization" do
client = Twitter::Search.new
@configuration.each do |key, value|
client.send("#{key}=", value)
end
@keys.each do |key|
client.send(key).should == @configuration[key]
end
end
end
end
end
context ".new" do
before do
@client = Twitter::Search.new
end
describe ".containing" do
it "should set the query to include term" do
@client.containing('twitter').query[:q].should include 'twitter'
end
end
describe ".not_containing" do
it "should set the query not to include term" do
@client.not_containing('twitter').query[:q].should include '-twitter'
end
end
describe ".language" do
it "should set the language to the query" do
@client.language('en').query[:lang].should == 'en'
end
end
describe ".locale" do
it "should set the locale to the query" do
@client.locale('ja').query[:locale].should == 'ja'
end
end
describe ".from" do
it "should set the query to include statuses from the specified person" do
@client.from('sferik').query[:q].should include 'from:sferik'
end
end
describe ".not_from" do
it "should set the query not to include statuses from the specified person" do
@client.not_from('sferik').query[:q].should include '-from:sferik'
end
it "should exclude multiple users" do
query = @client.not_from('sferik').not_from('pengwynn').query[:q]
query.should include '-from:sferik'
query.should include '-from:pengwynn'
end
end
describe ".to" do
it "should set the query to include statuses to the specified person" do
@client.to('sferik').query[:q].should include 'to:sferik'
end
end
describe ".not_to" do
it "should set the query not to include statuses to the specified person" do
@client.not_to('sferik').query[:q].should include '-to:sferik'
end
end
describe ".mentioning" do
it "should set the query to include mentions" do
@client.mentioning('sferik').query[:q].should include '@sferik'
end
end
describe ".not_mentioning" do
it "should set the query not to include mentions" do
@client.not_mentioning('sferik').query[:q].should include '-@sferik'
end
end
describe ".filter" do
it "should set the query to include filter" do
@client.filter('links').query[:q].should include 'filter:links'
end
end
describe ".retweets" do
it "should set the query to include retweets" do
@client.retweets.query[:q].should include 'rt'
end
end
describe ".no_retweets" do
it "should set the query not to include retweets" do
@client.no_retweets.query[:q].should include '-rt'
end
end
describe ".hashtag" do
it "should set the query to include hashtag" do
@client.hashtag('twitter').query[:q].should include '#twitter'
end
end
describe ".excluding_hashtag" do
it "should set the query not to include hashtag" do
@client.excluding_hashtag('twitter').query[:q].should include '-#twitter'
end
end
describe ".max_id" do
it "should set the max id" do
@client.max_id(1).query[:max_id].should == 1
end
end
describe ".phrase" do
it "should set the phrase" do
@client.phrase('peanut butter').query[:phrase].should == 'peanut butter'
end
end
describe ".result_type" do
it "should set the result type" do
@client.result_type('popular').query[:result_type].should == 'popular'
end
end
describe ".source" do
it "should set the source" do
@client.source("Hibari").query[:q].should include 'source:Hibari'
end
end
describe ".since_id" do
it "should set the since id" do
@client.since_id(1).query[:since_id].should == 1
end
end
describe ".since_date" do
it "should set the since date" do
@client.since_date('2010-10-10').query[:since].should == '2010-10-10'
end
end
describe ".until_date" do
it "should set the until date" do
@client.until_date('2010-10-10').query[:until].should == '2010-10-10'
end
end
describe ".positive" do
it "should set the query to include ':)'" do
@client.positive.query[:tude].should include ':)'
end
end
describe ".negative" do
it "should set the query to include ':('" do
@client.negative.query[:tude].should include ':('
end
end
describe ".question" do
it "should set the query to include '?'" do
@client.question.query[:tude].should include '?'
end
end
describe ".geocode" do
it "should set the geocode" do
@client.geocode(37.781157, -122.398720, '1mi').query[:geocode].should == '37.781157,-122.39872,1mi'
end
end
describe ".place" do
it "should set the place" do
@client.place("5a110d312052166f").query[:q].should include 'place:5a110d312052166f'
end
end
describe ".per_page" do
it "should set the number of results per page" do
@client.per_page(25).query[:rpp].should == 25
end
end
describe ".include_entities" do
it "should set the query to include entities" do
@client.include_entities().query[:include_entities].should == true
end
end
describe ".with_twitter_user_id" do
it "should set the query to return official user ids" do
@client.with_twitter_user_id().query[:with_twitter_user_id].should == true
end
end
describe ".page" do
it "should set the page number" do
@client.page(20).query[:page].should == 20
end
end
describe ".next_page?" do
before do
stub_request(:get, "https://search.twitter.com/search.json").
with(:query => {:q => "twitter"}).
to_return(:body => fixture("search.json"), :headers => {:content_type => "application/json; charset=utf-8"})
@client.containing('twitter')
end
it "should get the correct resource" do
@client.next_page?
a_request(:get, "https://search.twitter.com/search.json").
with(:query => {:q => "twitter"}).
should have_been_made
end
it "should be true if there's another page" do
next_page = @client.next_page?
next_page.should be_true
end
end
describe ".fetch" do
before do
stub_request(:get, "https://search.twitter.com/search.json").
with(:query => {:q => "twitter"}).
to_return(:body => fixture("search.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "should get the correct resource" do
@client.containing('twitter').fetch
a_request(:get, "https://search.twitter.com/search.json").
with(:query => {:q => "twitter"}).
should have_been_made
end
it "should return an array of search results" do
results = @client.containing('twitter').fetch
results.should be_an Array
results.first.should be_a Twitter::Status
results.first.text.should == "@KaiserKuo from not too far away your new twitter icon looks like Vader."
end
end
describe ".fetch_next_page" do
before do
stub_request(:get, "https://search.twitter.com/search.json").
with(:query => {:q => "twitter"}).
to_return(:body => fixture("search.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_request(:get, "https://search.twitter.com/search.json").
with(:query => {:q => "twitter", :page => "2", :max_id => "28857935752"}).
to_return(:body => fixture("search.json"), :headers => {:content_type => "application/json; charset=utf-8"})
@client.containing('twitter')
end
it "should get the correct resource" do
@client.fetch_next_page
a_request(:get, "https://search.twitter.com/search.json").
with(:query => {:q => "twitter"}).
should have_been_made
a_request(:get, "https://search.twitter.com/search.json").
with(:query => {:q => "twitter", :page => "2", :max_id => "28857935752"}).
should have_been_made
end
end
describe ".each" do
before do
stub_request(:get, "https://search.twitter.com/search.json").
with(:query => {:q => "twitter"}).
to_return(:body => fixture("search.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "should iterate over results" do
@client.containing('twitter').each do |result|
result.should be
end
a_request(:get, "https://search.twitter.com/search.json").
with(:query => {:q => "twitter"}).
should have_been_made
end
it "should iterate over results multiple times in a row" do
@client.containing('twitter').each do |result|
result.should be
end
@client.containing('twitter').each do |result|
result.should be
end
a_request(:get, "https://search.twitter.com/search.json").
with(:query => {:q => "twitter"}).
should have_been_made.once
end
end
end
end