Skip to content

Commit

Permalink
Added Twitter API versioning support for search methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Mar 21, 2010
1 parent aff5d89 commit 9ed3709
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
28 changes: 15 additions & 13 deletions lib/twitter/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,47 @@ module Twitter
class Search
include HTTParty
include Enumerable
base_uri "api.twitter.com/1/search"
format :json

attr_reader :result, :query

def initialize(q=nil, options={})
@options = options
clear
containing(q) if q && q.strip != ''
containing(q) if q && q.strip != ""
end

def user_agent
@options[:user_agent] || 'Ruby Twitter Gem'
@options[:user_agent] || "Ruby Twitter Gem"
end

def from(user,exclude=false)
@query[:q] << "#{exclude ? '-' : ''}from:#{user}"
@query[:q] << "#{exclude ? "-" : ""}from:#{user}"
self
end

def to(user,exclude=false)
@query[:q] << "#{exclude ? '-' : ''}to:#{user}"
@query[:q] << "#{exclude ? "-" : ""}to:#{user}"
self
end

def referencing(user,exclude=false)
@query[:q] << "#{exclude ? '-' : ''}@#{user}"
@query[:q] << "#{exclude ? "-" : ""}@#{user}"
self
end
alias :references :referencing
alias :ref :referencing

def containing(word,exclude=false)
@query[:q] << "#{exclude ? '-' : ''}#{word}"
@query[:q] << "#{exclude ? "-" : ""}#{word}"
self
end
alias :contains :containing

# adds filtering based on hash tag ie: #twitter
def hashed(tag,exclude=false)
@query[:q] << "#{exclude ? '-' : ''}\##{tag}"
@query[:q] << "#{exclude ? "-" : ""}\##{tag}"
self
end

Expand Down Expand Up @@ -101,7 +103,7 @@ def until_date(until_date)

# Ranges like 25km and 50mi work.
def geocode(lat, long, range)
@query[:geocode] = [lat, long, range].join(',')
@query[:geocode] = [lat, long, range].join(",")
self
end

Expand All @@ -121,32 +123,32 @@ def clear
def fetch(force=false)
if @fetch.nil? || force
query = @query.dup
query[:q] = query[:q].join(' ')
query[:q] = query[:q].join(" ")
perform_get(query)
end

@fetch
end

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

def next_page?
!!fetch()['next_page']
!!fetch()["next_page"]
end

def fetch_next_page
if next_page?
s = Search.new(nil, :user_agent => user_agent)
s.perform_get(fetch()['next_page'][1..-1])
s.perform_get(fetch()["next_page"][1..-1])
s
end
end

protected
def perform_get(query)
response = self.class.get('http://search.twitter.com/search.json', :query => query, :format => :json, :headers => {'User-Agent' => user_agent})
response = self.class.get("http://api.twitter.com/#{API_VERSION}/search.json", :query => query, :format => :json, :headers => {"User-Agent" => user_agent})
@fetch = Hashie::Mash.new(response)
end
end
Expand Down
28 changes: 14 additions & 14 deletions test/twitter/search_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SearchTest < Test::Unit::TestCase
end

should "pass user agent along with headers when making request" do
Twitter::Search.expects(:get).with('http://search.twitter.com/search.json', {:format => :json, :query => {:q => 'foo'}, :headers => {'User-Agent' => 'Foobar'}})
Twitter::Search.expects(:get).with('http://api.twitter.com/1/search.json', {:format => :json, :query => {:q => 'foo'}, :headers => {'User-Agent' => 'Foobar'}})
Twitter::Search.new('foo', :user_agent => 'Foobar').fetch()
end

Expand Down Expand Up @@ -75,61 +75,61 @@ class SearchTest < Test::Unit::TestCase

should "should be able to specify the language" do
@search.lang('en')
@search.class.expects(:get).with('http://search.twitter.com/search.json', :query => {:lang => 'en', :q => ''}, :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({'foo' => 'bar'})
@search.class.expects(:get).with('http://api.twitter.com/1/search.json', :query => {:lang => 'en', :q => ''}, :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({'foo' => 'bar'})
@search.fetch()
end

should "should be able to specify the number of results per page" do
@search.per_page(25)
@search.class.expects(:get).with('http://search.twitter.com/search.json', :query => {:rpp => 25, :q => ''}, :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({'foo' => 'bar'})
@search.class.expects(:get).with('http://api.twitter.com/1/search.json', :query => {:rpp => 25, :q => ''}, :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({'foo' => 'bar'})
@search.fetch()
end

should "should be able to specify the page number" do
@search.page(20)
@search.class.expects(:get).with('http://search.twitter.com/search.json', :query => {:page => 20, :q => ''}, :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({'foo' => 'bar'})
@search.class.expects(:get).with('http://api.twitter.com/1/search.json', :query => {:page => 20, :q => ''}, :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({'foo' => 'bar'})
@search.fetch()
end

should "should be able to specify only returning results greater than an id" do
@search.since(1234)
@search.class.expects(:get).with('http://search.twitter.com/search.json', :query => {:since_id => 1234, :q => ''}, :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({'foo' => 'bar'})
@search.class.expects(:get).with('http://api.twitter.com/1/search.json', :query => {:since_id => 1234, :q => ''}, :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({'foo' => 'bar'})
@search.fetch()
end

should "should be able to specify since a date" do
@search.since_date('2009-04-14')
@search.class.expects(:get).with('http://search.twitter.com/search.json', :query => { :since => '2009-04-14', :q => ''}, :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({ 'foo' => 'bar'})
@search.class.expects(:get).with('http://api.twitter.com/1/search.json', :query => { :since => '2009-04-14', :q => ''}, :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({ 'foo' => 'bar'})
@search.fetch
end

should "should be able to specify until a date" do
@search.until_date('2009-04-14')
@search.class.expects(:get).with('http://search.twitter.com/search.json', :query => { :until => '2009-04-14', :q => ''}, :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({ 'foo' => 'bar'})
@search.class.expects(:get).with('http://api.twitter.com/1/search.json', :query => { :until => '2009-04-14', :q => ''}, :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({ 'foo' => 'bar'})
@search.fetch
end

should "should be able to specify geo coordinates" do
@search.geocode('40.757929', '-73.985506', '25mi')
@search.class.expects(:get).with('http://search.twitter.com/search.json', :query => {:geocode => '40.757929,-73.985506,25mi', :q => ''}, :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({'foo' => 'bar'})
@search.class.expects(:get).with('http://api.twitter.com/1/search.json', :query => {:geocode => '40.757929,-73.985506,25mi', :q => ''}, :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({'foo' => 'bar'})
@search.fetch()
end

should "should be able to specify max id" do
@search.max(1234)
@search.class.expects(:get).with('http://search.twitter.com/search.json', :query => {:max_id => 1234, :q => ''}, :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({'foo' => 'bar'})
@search.class.expects(:get).with('http://api.twitter.com/1/search.json', :query => {:max_id => 1234, :q => ''}, :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({'foo' => 'bar'})
@search.fetch()
end

should "should be able to set the phrase" do
@search.phrase("Who Dat")
@search.class.expects(:get).with('http://search.twitter.com/search.json', :query => {:phrase => "Who Dat", :q => ''}, :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({'foo' => 'bar'})
@search.class.expects(:get).with('http://api.twitter.com/1/search.json', :query => {:phrase => "Who Dat", :q => ''}, :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({'foo' => 'bar'})
@search.fetch()
end

should "should be able to set the result type" do
@search.result_type("popular")
@search.class.expects(:get).with('http://search.twitter.com/search.json', :query => {:result_type => 'popular', :q => ''}, :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({'foo' => 'bar'})
@search.class.expects(:get).with('http://api.twitter.com/1/search.json', :query => {:result_type => 'popular', :q => ''}, :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({'foo' => 'bar'})
@search.fetch()
end

Expand All @@ -149,7 +149,7 @@ class SearchTest < Test::Unit::TestCase

context "fetching" do
setup do
stub_get('http://search.twitter.com:80/search.json?q=%40jnunemaker', 'search.json')
stub_get('http://api.twitter.com:80/1/search.json?q=%40jnunemaker', 'search.json')
@search = Twitter::Search.new('@jnunemaker')
@response = @search.fetch
end
Expand Down Expand Up @@ -179,14 +179,14 @@ class SearchTest < Test::Unit::TestCase
end

should "be able to fetch the next page" do
Twitter::Search.expects(:get).with('http://search.twitter.com/search.json', :query => 'page=2&max_id=1446791544&q=%40jnunemaker', :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({'foo' => 'bar'})
Twitter::Search.expects(:get).with('http://api.twitter.com/1/search.json', :query => 'page=2&max_id=1446791544&q=%40jnunemaker', :format => :json, :headers => {'User-Agent' => 'Ruby Twitter Gem'}).returns({'foo' => 'bar'})
@search.fetch_next_page
end
end

context "iterating over results" do
setup do
stub_get('http://search.twitter.com:80/search.json?q=from%3Ajnunemaker', 'search_from_jnunemaker.json')
stub_get('http://api.twitter.com:80/1/search.json?q=from%3Ajnunemaker', 'search_from_jnunemaker.json')
@search.from('jnunemaker')
end

Expand Down

0 comments on commit 9ed3709

Please sign in to comment.