Skip to content

Commit

Permalink
Default to maximum number of tweets per request
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Dec 24, 2013
1 parent 9750ac5 commit 1e41b5d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
2 changes: 2 additions & 0 deletions lib/twitter/rest/api/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module REST
module API
module Search
include Twitter::REST::API::Utils
MAX_TWEETS_PER_REQUEST = 100

# Returns tweets that match a specified query.
#
Expand All @@ -28,6 +29,7 @@ module Search
# @option options [Boolean, String, Integer] :include_entities The tweet entities node will be disincluded when set to false.
# @return [Twitter::SearchResults] Return tweets that match a specified query with search metadata
def search(q, options = {})
options[:count] ||= MAX_TWEETS_PER_REQUEST
search_results_from_response(:get, '/1.1/search/tweets.json', options.merge(:q => q))
end

Expand Down
43 changes: 25 additions & 18 deletions spec/twitter/rest/api/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,34 @@
end

describe '#search' do
before do
stub_get('/1.1/search/tweets.json').with(:query => {:q => '#freebandnames'}).to_return(:body => fixture('search.json'), :headers => {:content_type => 'application/json; charset=utf-8'})
end
it 'requests the correct resource' do
@client.search('#freebandnames')
expect(a_get('/1.1/search/tweets.json').with(:query => {:q => '#freebandnames'})).to have_been_made
end
it 'returns recent Tweets related to a query with images and videos embedded' do
search = @client.search('#freebandnames')
expect(search).to be_a Twitter::SearchResults
expect(search.first).to be_a Twitter::Tweet
expect(search.first.text).to eq('@Just_Reboot #FreeBandNames mono surround')
end
context 'when search API responds a malformed result' do
context 'without count specified' do
before do
stub_get('/1.1/search/tweets.json').with(:query => {:q => '#freebandnames'}).to_return(:body => fixture('search_malformed.json'), :headers => {:content_type => 'application/json; charset=utf-8'})
stub_get('/1.1/search/tweets.json').with(:query => {:q => '#freebandnames', :count => '100'}).to_return(:body => fixture('search.json'), :headers => {:content_type => 'application/json; charset=utf-8'})
end
it 'returns an empty array' do
it 'requests the correct resource' do
@client.search('#freebandnames')
expect(a_get('/1.1/search/tweets.json').with(:query => {:q => '#freebandnames', :count => '100'})).to have_been_made
end
it 'returns recent Tweets related to a query with images and videos embedded' do
search = @client.search('#freebandnames')
expect(search.to_a).to be_an Array
expect(search.to_a).to be_empty
expect(search).to be_a Twitter::SearchResults
expect(search.first).to be_a Twitter::Tweet
expect(search.first.text).to eq('@Just_Reboot #FreeBandNames mono surround')
end
end
context 'with count specified' do
before do
stub_get('/1.1/search/tweets.json').with(:query => {:q => '#freebandnames', :count => '3'}).to_return(:body => fixture('search.json'), :headers => {:content_type => 'application/json; charset=utf-8'})
end
it 'requests the correct resource' do
@client.search('#freebandnames', :count => 3)
expect(a_get('/1.1/search/tweets.json').with(:query => {:q => '#freebandnames', :count => '3'})).to have_been_made
end
it 'returns recent Tweets related to a query with images and videos embedded' do
search = @client.search('#freebandnames', :count => 3)
expect(search).to be_a Twitter::SearchResults
expect(search.first).to be_a Twitter::Tweet
expect(search.first.text).to eq('@Just_Reboot #FreeBandNames mono surround')
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/twitter/search_results_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
describe '#each' do
before do
@client = Twitter::REST::Client.new(:consumer_key => 'CK', :consumer_secret => 'CS', :access_token => 'AT', :access_token_secret => 'AS')
stub_get('/1.1/search/tweets.json').with(:query => {:q => '#freebandnames'}).to_return(:body => fixture('search.json'), :headers => {:content_type => 'application/json; charset=utf-8'})
stub_get('/1.1/search/tweets.json').with(:query => {:q => '#freebandnames', :count => '100'}).to_return(:body => fixture('search.json'), :headers => {:content_type => 'application/json; charset=utf-8'})
stub_get('/1.1/search/tweets.json').with(:query => {:q => '#freebandnames', :count => '3', :include_entities => '1', :max_id => '414071361066532863'}).to_return(:body => fixture('search2.json'), :headers => {:content_type => 'application/json; charset=utf-8'})
end
it 'requests the correct resources' do
@client.search('#freebandnames').each {}
expect(a_get('/1.1/search/tweets.json').with(:query => {:q => '#freebandnames'})).to have_been_made
expect(a_get('/1.1/search/tweets.json').with(:query => {:q => '#freebandnames', :count => '100'})).to have_been_made
expect(a_get('/1.1/search/tweets.json').with(:query => {:q => '#freebandnames', :count => '3', :include_entities => '1', :max_id => '414071361066532863'})).to have_been_made
end
it 'iterates' do
Expand Down

0 comments on commit 1e41b5d

Please sign in to comment.