From 1e41b5d4dde8678f5968b57dafe9da63b092646c Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Tue, 24 Dec 2013 18:05:48 -0500 Subject: [PATCH] Default to maximum number of tweets per request --- lib/twitter/rest/api/search.rb | 2 ++ spec/twitter/rest/api/search_spec.rb | 43 ++++++++++++++++------------ spec/twitter/search_results_spec.rb | 4 +-- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/lib/twitter/rest/api/search.rb b/lib/twitter/rest/api/search.rb index a798b66d6..64bd98fbc 100644 --- a/lib/twitter/rest/api/search.rb +++ b/lib/twitter/rest/api/search.rb @@ -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. # @@ -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 diff --git a/spec/twitter/rest/api/search_spec.rb b/spec/twitter/rest/api/search_spec.rb index 6e95ed44e..120f26733 100644 --- a/spec/twitter/rest/api/search_spec.rb +++ b/spec/twitter/rest/api/search_spec.rb @@ -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 diff --git a/spec/twitter/search_results_spec.rb b/spec/twitter/search_results_spec.rb index abf50bac9..3c506b190 100644 --- a/spec/twitter/search_results_spec.rb +++ b/spec/twitter/search_results_spec.rb @@ -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