Skip to content

Commit

Permalink
Added accessors for search results for convenience
Browse files Browse the repository at this point in the history
Signed-off-by: John Nunemaker <nunemaker@gmail.com>
  • Loading branch information
Wynn Netherland authored and jnunemaker committed Jan 5, 2009
1 parent 81255a4 commit 47d0506
Show file tree
Hide file tree
Showing 7 changed files with 326 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
require 'twitter/status'
require 'twitter/direct_message'
require 'twitter/rate_limit_status'
require 'twitter/search_result_info'
require 'twitter/search_result'

module Twitter
class Unavailable < StandardError; end
Expand Down
5 changes: 3 additions & 2 deletions lib/twitter/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,13 @@ def clear
# If you want to get results do something other than iterate over them.
def fetch
@query[:q] = @query[:q].join(' ')
self.class.get('/search.json', {:query => @query})
SearchResultInfo.new_from_hash(self.class.get('/search.json', {:query => @query}))
end

def each
@result = fetch()
@result['results'].each { |r| yield r }
end
end
end
end

83 changes: 83 additions & 0 deletions lib/twitter/search_result.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
module Twitter
class SearchResult < Hash

# Creates an easier to work with hash from
# one with string-based keys
def self.new_from_hash(hash)
new.merge!(hash)
end

def created_at
self['created_at']
end

def created_at=(val)
self['created_at'] = val
end

def from_user
self['from_user']
end

def from_user=(val)
self['from_user'] = val
end

def from_user_id
self['from_user_id']
end

def from_user_id=(val)
self['from_user_id'] = val
end

def id
self['id']
end

def id=(val)
self['id'] = val
end

def iso_language_code
self['iso_language_code']
end

def iso_language_code=(val)
self['iso_language_code'] = val
end

def profile_image_url
self['profile_image_url']
end

def profile_image_url=(val)
self['profile_image_url'] = val
end

def text
self['text']
end

def text=(val)
self['text'] = val
end

def to_user
self['to_user']
end

def to_user=(val)
self['to_user'] = val
end

def to_user_id
self['to_user_id']
end

def to_user_id=(val)
self['to_user_id'] = val
end

end
end
82 changes: 82 additions & 0 deletions lib/twitter/search_result_info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
module Twitter
class SearchResultInfo < Hash

# Creates an easier to work with hash from
# one with string-based keys
def self.new_from_hash(hash)
i = new
i.merge!(hash)
search_results = []
i.results.each do |r|
search_results << SearchResult.new_from_hash(r)
end
i.results = search_results
i
end

def completed_in
self['completed_in']
end

def completed_in=(val)
self['completed_in'] = val
end

def max_id
self['max_id']
end

def max_id=(val)
self['max_id'] = val
end

def next_page
self['next_page']
end

def next_page=(val)
self['next_page'] = val
end

def page
self['page']
end

def page=(val)
self['page'] = val
end

def refresh_url
self['refresh_url']
end

def refresh_url=(val)
self['refresh_url'] = val
end

def results_per_page
self['results_per_page']
end

def results_per_page=(val)
self['results_per_page'] = val
end

def since_id
self['since_id']
end

def since_id=(val)
self['since_id'] = val
end

def results
self['results']
end

def results=(val)
self['results'] = val
end

end
end
2 changes: 1 addition & 1 deletion lib/twitter/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Twitter #:nodoc:
Version = '0.4.0'
Version = '0.4.1'
end
147 changes: 147 additions & 0 deletions spec/fixtures/search_result_info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
--- !map:Twitter::SearchResultInfo
results:
- !map:Twitter::SearchResult
text: "@jqr - wow. just really looked at httparty for first time - sweet! - have you been using that a lot?"
to_user_id: 176589
to_user: jqr
from_user: baldwindavid
id: 1073680192
from_user_id: 804487
iso_language_code: en
profile_image_url: http://s3.amazonaws.com/twitter_production/profile_images/67146299/before_400_normal.jpg
created_at: Tue, 23 Dec 2008 04:20:12 +0000
- !map:Twitter::SearchResult
text: the httparty gem rocks my socks. no better way to consume web services in ruby.
to_user_id:
from_user: mildmojo
id: 1073135700
from_user_id: 2090305
iso_language_code: en
profile_image_url: http://s3.amazonaws.com/twitter_production/profile_images/65215459/appicon_normal.png
created_at: Mon, 22 Dec 2008 22:20:43 +0000
- !map:Twitter::SearchResult
text: It's an HTTParty and everyone is invited!
to_user_id:
from_user: mypheme
id: 1067975762
from_user_id: 3067473
iso_language_code: en
profile_image_url: http://s3.amazonaws.com/twitter_production/profile_images/67872558/Picture_3_normal.png
created_at: Fri, 19 Dec 2008 22:09:23 +0000
- !map:Twitter::SearchResult
text: dans la famille des clients http,vous connaissiez curb, httparty, mais connaissiez-vous httpclient ? http://dev.ctor.org/httpclient/
to_user_id:
from_user: rubyfrance
id: 1064812719
from_user_id: 70780
iso_language_code: fr
profile_image_url: http://s3.amazonaws.com/twitter_production/profile_images/51884877/twitter_normal.png
created_at: Thu, 18 Dec 2008 12:46:27 +0000
- !map:Twitter::SearchResult
text: "@jnunemaker have you ever seen httparty get a Net::HTTPServerException: 411 &quot;Length Required&quot; back?"
to_user_id: 19106
to_user: jnunemaker
from_user: techpickles
id: 1064375973
from_user_id: 22902
iso_language_code: en
profile_image_url: http://s3.amazonaws.com/twitter_production/profile_images/64410494/Photo_9_normal.jpg
created_at: Thu, 18 Dec 2008 05:24:23 +0000
- !map:Twitter::SearchResult
text: playing with the tumblr api, httparty style
to_user_id:
from_user: techpickles
id: 1064355666
from_user_id: 22902
iso_language_code: en
profile_image_url: http://s3.amazonaws.com/twitter_production/profile_images/64410494/Photo_9_normal.jpg
created_at: Thu, 18 Dec 2008 05:07:59 +0000
- !map:Twitter::SearchResult
text: Testing from HTTParty, awesome ruby gem that allows for easy access to APIs over http.
to_user_id:
from_user: simonreed
id: 1063822669
from_user_id: 63911
iso_language_code: en
profile_image_url: http://s3.amazonaws.com/twitter_production/profile_images/53963586/fu_normal.jpg
created_at: Wed, 17 Dec 2008 23:21:34 +0000
- !map:Twitter::SearchResult
text: It's an HTTParty and everyone is invited!
to_user_id:
from_user: bernsno
id: 1063277742
from_user_id: 1495935
iso_language_code: en
profile_image_url: http://s3.amazonaws.com/twitter_production/profile_images/63820805/me_normal.gif
created_at: Wed, 17 Dec 2008 18:25:31 +0000
- !map:Twitter::SearchResult
text: "[ruby] Using Context and Stump to HTTParty like a Wufoo"
to_user_id:
from_user: rubymentary
id: 1062068801
from_user_id: 474717
iso_language_code: en
profile_image_url: http://static.twitter.com/images/default_profile_normal.png
created_at: Wed, 17 Dec 2008 03:37:00 +0000
- !map:Twitter::SearchResult
text: "net/http\xE3\x81\xAEwrapper\xE3\x81\xA0\xE3\x81\xA3\xE3\x81\x9F\xE3\x80\x82\xE3\x80\x82\xE3\x80\x82 &gt; HTTParty re: http://ff.im/h1Dg"
to_user_id:
from_user: nahi
id: 1061821371
from_user_id: 36221
iso_language_code: "no"
profile_image_url: http://static.twitter.com/images/default_profile_normal.png
created_at: Wed, 17 Dec 2008 00:58:19 +0000
- !map:Twitter::SearchResult
text: Using HTTParty to wrap the Wufoo form submit API, as we are switching the Ordered List contact form to Wufoo.
to_user_id:
from_user: jnunemaker
id: 1061117717
from_user_id: 19106
iso_language_code: en
profile_image_url: http://s3.amazonaws.com/twitter_production/profile_images/61024905/black250_normal.jpg
created_at: Tue, 16 Dec 2008 18:25:28 +0000
- !map:Twitter::SearchResult
text: Playing with HTTParty. Like it already. :)
to_user_id:
from_user: atog
id: 1058725999
from_user_id: 21795
iso_language_code: en
profile_image_url: http://s3.amazonaws.com/twitter_production/profile_images/61430136/ikke_vierkant_normal.jpg
created_at: Mon, 15 Dec 2008 16:06:26 +0000
- !map:Twitter::SearchResult
text: HTTParty is like sweet candy...
to_user_id:
from_user: levicole
id: 1055553905
from_user_id: 111971
iso_language_code: en
profile_image_url: http://s3.amazonaws.com/twitter_production/profile_images/24274692/Photo_2_normal.jpg
created_at: Sat, 13 Dec 2008 17:52:01 +0000
- !map:Twitter::SearchResult
text: "thinking httparty needs a bit of morph class generation magic: http://tinyurl.com/6774gz"
to_user_id:
from_user: delineator
id: 1054352107
from_user_id: 102024
iso_language_code: en
profile_image_url: http://s3.amazonaws.com/twitter_production/profile_images/63867007/rob_64_64_normal.png
created_at: Fri, 12 Dec 2008 22:53:41 +0000
- !map:Twitter::SearchResult
text: To use HTTParty, or just net/http? I'm going to have to change some headers, which means digging around. Hrm.
to_user_id:
from_user: HibiscuS4
id: 1050622779
from_user_id: 8780
iso_language_code: en
profile_image_url: http://s3.amazonaws.com/twitter_production/profile_images/60311231/Photo_9_normal.jpg
created_at: Thu, 11 Dec 2008 04:13:06 +0000
since_id: 0
max_id: 1078578631
refresh_url: ?since_id=1078578631&q=httparty
results_per_page: 15
next_page: ?page=2&max_id=1078578631&q=httparty
completed_in: 0.018799
page: 1
query: httparty
9 changes: 8 additions & 1 deletion spec/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,21 @@

describe "fetching" do
before do
@response = open(File.dirname(__FILE__) + '/fixtures/friends_timeline.xml').read
@response = YAML.load_file(File.dirname(__FILE__) + '/fixtures/search_result_info.yml')
@search.class.stub!(:get).and_return(@response)
end

it "should return results" do
@search.class.should_receive(:get).and_return(@response)
@search.from('jnunemaker').fetch().should == @response
end

it "should support dot notation" do
@search.class.should_receive(:get).and_return(@response)
info = @search.from('httparty').fetch()
info["max_id"].should == info.max_id
info["results"].first["text"].should == info.results.first.text
end
end

it "should be able to iterate over results" do
Expand Down

0 comments on commit 47d0506

Please sign in to comment.