Permalink
Browse files

SearchResults object contains metadata and results array

  • Loading branch information...
1 parent 847c018 commit 1b88acd997360a3688e3158e5e36fed81f5a529c @wjlroe committed May 23, 2012
Showing with 91 additions and 15 deletions.
  1. +10 −2 README.md
  2. +5 −8 lib/twitter/client.rb
  3. +66 −0 lib/twitter/search_results.rb
  4. +10 −5 spec/twitter/client/search_spec.rb
View
12 README.md
@@ -138,13 +138,21 @@ Return the text of the Tweet at https://twitter.com/sferik/statuses/27558893223
Twitter.status(27558893223).text
Find the 3 most recent marriage proposals to [@justinbieber][justinbieber]
- Twitter.search("to:justinbieber marry me", :rpp => 3, :result_type => "recent").map do |status|
+ Twitter.search("to:justinbieber marry me", :rpp => 3, :result_type => "recent").results.map do |status|
"#{status.from_user}: #{status.text}"
end
Let's find a Japanese-language Tweet tagged #ruby (no retweets)
- Twitter.search("#ruby -rt", :lang => "ja", :rpp => 1).first.text
+ Twitter.search("#ruby -rt", :lang => "ja", :rpp => 1).results.first.text
+
+The search result object returned by `Twitter::Client#search` includes some metadata about the search
+results:
+
+ Twitter.search("to:justinbieber marry me", :rpp => 3, :result_type => "recent").max_id => 28857935752
+
+The `max_id` attribute can be used in your next search query as the `:since_id` parameter to only return newer
+tweets.
Certain methods require authentication. To get your Twitter OAuth credentials,
register an app at http://dev.twitter.com/apps
View
13 lib/twitter/client.rb
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
require 'twitter/action_factory'
require 'twitter/authenticatable'
require 'twitter/config'
@@ -25,6 +26,7 @@
require 'twitter/request'
require 'twitter/retweet'
require 'twitter/saved_search'
+require 'twitter/search_results'
require 'twitter/settings'
require 'twitter/size'
require 'twitter/status'
@@ -1787,17 +1789,12 @@ def saved_search_destroy(id, options={})
# @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] :with_twitter_user_id When set to either true, t or 1, the from_user_id and to_user_id values in the response will map to "official" user IDs which will match those returned by the REST API.
- # @return [Array<Twitter::Status>] Return tweets that match a specified query
+ # @return <Twitter::SearchResults> Return tweets that match a specified query with search metadata
# @example Returns tweets related to twitter
# Twitter.search('twitter')
def search(q, options={})
- if results = get("/search.json", options.merge(:q => q), :endpoint => search_endpoint)['results']
- results.map do |status|
- Twitter::Status.new(status)
- end
- else
- []
- end
+ response = get("/search.json", options.merge(:q => q), :endpoint => search_endpoint)
+ Twitter::SearchResults.new(response)
end
# Returns recent statuses related to a query with images and videos embedded
View
66 lib/twitter/search_results.rb
@@ -0,0 +1,66 @@
+require 'twitter/base'
+
+module Twitter
+ class SearchResults < Twitter::Base
+
+ lazy_attr_reader :results, :completed_in, :max_id, :max_id_str, :next_path, :page,
+ :query, :refresh_url, :results_per_page, :since_id, :since_id_str
+
+
+ # @return [Array<Twitter::Status>]
+ def results
+ @results ||= (@attrs['results'] || []).map{ |status| Twitter::Status.new(status) }
+ end
+
+ # @return [Float]
+ def completed_in
+ @attrs['completed_in']
+ end
+
+ # @return [Fixnum]
+ def max_id
+ @attrs['max_id']
+ end
+
+ # @return [String]
+ def max_id_str
+ @attrs['max_id_str']
+ end
+
+ # @return [String]
+ def next_page
+ @attrs['next_page']
+ end
+
+ # @return [Fixnum]
+ def page
+ @attrs['page']
+ end
+
+ # @return [String]
+ def query
+ @attrs['query']
+ end
+
+ # @return [String]
+ def refresh_url
+ @attrs['refresh_url']
+ end
+
+ # @return [Fixnum]
+ def results_per_page
+ @attrs['results_per_page']
+ end
+
+ # @return [Fixnum]
+ def since_id
+ @attrs['since_id']
+ end
+
+ # @return [String]
+ def since_id_str
+ @attrs['since_id_str']
+ end
+ end
+end
+
View
15 spec/twitter/client/search_spec.rb
@@ -20,9 +20,14 @@
end
it "should return recent statuses related to a query with images and videos embedded" do
search = @client.search('twitter')
- search.should be_an Array
- search.first.should be_a Twitter::Status
- search.first.text.should == "@KaiserKuo from not too far away your new twitter icon looks like Vader."
+ search.results.should be_an Array
+ search.results.first.should be_a Twitter::Status
+ search.results.first.text.should == "@KaiserKuo from not too far away your new twitter icon looks like Vader."
+ end
+
+ it "should return the max_id value for a search result" do
+ search = @client.search('twitter')
+ search.max_id.should eq(28857935752)
end
context "when search API responds a malformed result" do
@@ -34,8 +39,8 @@
it "should not fail and return blank Array" do
search = @client.search('twitter')
- search.should be_an Array
- search.should have(0).items
+ search.results.should be_an Array
+ search.results.should have(0).items
end
end
end

0 comments on commit 1b88acd

Please sign in to comment.