From 141d23765e0b18be45b9e7e741b5a5a7eb1feb04 Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Tue, 24 Dec 2013 17:25:09 -0500 Subject: [PATCH] Revert "Allow search query to be passed in options hash" This reverts commit 8e846e1d2d0db40fc24db3247b4e239a30bf0760. --- .yardopts | 1 - examples/Search.md | 22 -------------- lib/twitter/rest/api/search.rb | 43 +++++++++------------------- spec/twitter/rest/api/search_spec.rb | 40 +++++++++----------------- 4 files changed, 27 insertions(+), 79 deletions(-) delete mode 100644 examples/Search.md diff --git a/.yardopts b/.yardopts index 9c509a68b..d6ecd3f14 100644 --- a/.yardopts +++ b/.yardopts @@ -11,5 +11,4 @@ README.md examples/AllTweets.md examples/Configuration.md examples/RateLimiting.md -examples/Search.md examples/Update.md diff --git a/examples/Search.md b/examples/Search.md deleted file mode 100644 index 22f07ef9b..000000000 --- a/examples/Search.md +++ /dev/null @@ -1,22 +0,0 @@ -# Search - -You can get search results, up to 100 at a time. - -Here is an example of recursively geting pages of 100 Tweets until you receive -an empty response. - -**Note: This may result in [rate limiting][].** - -[rate limiting]: https://github.com/sferik/twitter/blob/master/examples/RateLimiting.md - -```ruby -require 'twitter' - -def get_all_results(options, collection = []) - results = @client.search(options) - collection += results.to_a - results.next_results? ? get_all_results(results.next_results, collection) : collection -end - -get_all_results(:q => "@sferik since:#{Date.today}", :count => 100) -``` diff --git a/lib/twitter/rest/api/search.rb b/lib/twitter/rest/api/search.rb index 55ba5dd35..230043102 100644 --- a/lib/twitter/rest/api/search.rb +++ b/lib/twitter/rest/api/search.rb @@ -1,4 +1,3 @@ -require 'twitter/arguments' require 'twitter/rest/api/utils' require 'twitter/search_results' @@ -16,36 +15,20 @@ module Search # @rate_limited Yes # @authentication Requires user context # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @param q [String] A search term. + # @param options [Hash] A customizable set of options. + # @option options [String] :geocode Returns tweets by users located within a given radius of the given latitude/longitude. The location is preferentially taking from the Geotagging API, but will fall back to their Twitter profile. The parameter value is specified by "latitude,longitude,radius", where radius units must be specified as either "mi" (miles) or "km" (kilometers). Note that you cannot use the near operator via the API to geocode arbitrary locations; however you can use this geocode parameter to search near geocodes directly. + # @option options [String] :lang Restricts tweets to the given language, given by an ISO 639-1 code. + # @option options [String] :locale Specify the language of the query you are sending (only ja is currently effective). This is intended for language-specific clients and the default should work in the majority of cases. + # @option options [String] :result_type Specifies what type of search results you would prefer to receive. Options are "mixed", "recent", and "popular". The current default is "mixed." + # @option options [Integer] :count The number of tweets to return per page, up to a maximum of 100. + # @option options [String] :until Optional. Returns tweets generated before the given date. Date should be formatted as YYYY-MM-DD. + # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. There are limits to the number of Tweets which can be accessed through the API. If the limit of Tweets has occured since the since_id, the since_id will be forced to the oldest ID available. + # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. + # @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 - # @overload search(q, options = {}) - # @param q [String] A search term. - # @param options [Hash] A customizable set of options. - # @option options [String] :geocode Returns tweets by users located within a given radius of the given latitude/longitude. The location is preferentially taking from the Geotagging API, but will fall back to their Twitter profile. The parameter value is specified by "latitude,longitude,radius", where radius units must be specified as either "mi" (miles) or "km" (kilometers). Note that you cannot use the near operator via the API to geocode arbitrary locations; however you can use this geocode parameter to search near geocodes directly. - # @option options [String] :lang Restricts tweets to the given language, given by an ISO 639-1 code. - # @option options [String] :locale Specify the language of the query you are sending (only ja is currently effective). This is intended for language-specific clients and the default should work in the majority of cases. - # @option options [String] :result_type Specifies what type of search results you would prefer to receive. Options are "mixed", "recent", and "popular". The current default is "mixed." - # @option options [Integer] :count The number of tweets to return per page, up to a maximum of 100. - # @option options [String] :until Optional. Returns tweets generated before the given date. Date should be formatted as YYYY-MM-DD. - # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. There are limits to the number of Tweets which can be accessed through the API. If the limit of Tweets has occured since the since_id, the since_id will be forced to the oldest ID available. - # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. - # @option options [Boolean, String, Integer] :include_entities The tweet entities node will be disincluded when set to false. - # @overload search(options) - # @param options [Hash] A customizable set of options. - # @option options [String] :q A search term. - # @option options [String] :geocode Returns tweets by users located within a given radius of the given latitude/longitude. The location is preferentially taking from the Geotagging API, but will fall back to their Twitter profile. The parameter value is specified by "latitude,longitude,radius", where radius units must be specified as either "mi" (miles) or "km" (kilometers). Note that you cannot use the near operator via the API to geocode arbitrary locations; however you can use this geocode parameter to search near geocodes directly. - # @option options [String] :lang Restricts tweets to the given language, given by an ISO 639-1 code. - # @option options [String] :locale Specify the language of the query you are sending (only ja is currently effective). This is intended for language-specific clients and the default should work in the majority of cases. - # @option options [String] :result_type Specifies what type of search results you would prefer to receive. Options are "mixed", "recent", and "popular". The current default is "mixed." - # @option options [Integer] :count The number of tweets to return per page, up to a maximum of 100. - # @option options [String] :until Optional. Returns tweets generated before the given date. Date should be formatted as YYYY-MM-DD. - # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. There are limits to the number of Tweets which can be accessed through the API. If the limit of Tweets has occured since the since_id, the since_id will be forced to the oldest ID available. - # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. - # @option options [Boolean, String, Integer] :include_entities The tweet entities node will be disincluded when set to false. - def search(*args) - arguments = Twitter::Arguments.new(args) - q = arguments.pop || arguments.options[:q] - fail(ArgumentError, "wrong number of arguments (#{args.size} for #{args.size + 1})") if q.nil? - object_from_response(Twitter::SearchResults, :get, '/1.1/search/tweets.json', arguments.options.merge(:q => q)) + def search(q, options = {}) + object_from_response(Twitter::SearchResults, :get, '/1.1/search/tweets.json', options.merge(:q => q)) end end end diff --git a/spec/twitter/rest/api/search_spec.rb b/spec/twitter/rest/api/search_spec.rb index aee8788db..c3df6bc10 100644 --- a/spec/twitter/rest/api/search_spec.rb +++ b/spec/twitter/rest/api/search_spec.rb @@ -10,38 +10,26 @@ before do stub_get('/1.1/search/tweets.json').with(:query => {:q => 'twitter'}).to_return(:body => fixture('search.json'), :headers => {:content_type => 'application/json; charset=utf-8'}) end - context 'with query passed as the first argument' do - it 'requests the correct resource' do - @client.search('twitter') - expect(a_get('/1.1/search/tweets.json').with(:query => {:q => 'twitter'})).to have_been_made - end - it 'returns recent Tweets related to a query with images and videos embedded' do - search = @client.search('twitter') - expect(search).to be_a Twitter::SearchResults - expect(search.first).to be_a Twitter::Tweet - expect(search.first.text).to eq('Bubble Mailer #freebandnames') - end - it 'returns the max_id value for a search result' do - search = @client.search('twitter') - expect(search.max_id).to eq(250_126_199_840_518_145) - end + it 'requests the correct resource' do + @client.search('twitter') + expect(a_get('/1.1/search/tweets.json').with(:query => {:q => 'twitter'})).to have_been_made end - context 'with query passed in options hash' do - it 'requests the correct resource' do - @client.search(:q => 'twitter') - expect(a_get('/1.1/search/tweets.json').with(:query => {:q => 'twitter'})).to have_been_made - end - it 'returns recent Tweets related to a query with images and videos embedded' do - search = @client.search(:q => 'twitter') - expect(search).to be_a Twitter::SearchResults - expect(search.first).to be_a Twitter::Tweet - expect(search.first.text).to eq('Bubble Mailer #freebandnames') - end + it 'returns recent Tweets related to a query with images and videos embedded' do + search = @client.search('twitter') + expect(search).to be_a Twitter::SearchResults + expect(search.first).to be_a Twitter::Tweet + expect(search.first.text).to eq('Bubble Mailer #freebandnames') + end + it 'returns the max_id value for a search result' do + search = @client.search('twitter') + expect(search.max_id).to eq(250_126_199_840_518_145) end + context 'when search API responds a malformed result' do before do stub_get('/1.1/search/tweets.json').with(:query => {:q => 'twitter'}).to_return(:body => fixture('search_malformed.json'), :headers => {:content_type => 'application/json; charset=utf-8'}) end + it 'returns an empty array' do search = @client.search('twitter') expect(search.to_a).to be_an Array