Skip to content

Commit

Permalink
Added user agent option to Twitter::Search and the masses rejoiced in…
Browse files Browse the repository at this point in the history
… unison!
  • Loading branch information
jnunemaker committed Jun 27, 2009
1 parent 9b0dbf5 commit e8fbad6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
4 changes: 3 additions & 1 deletion History
@@ -1,6 +1,8 @@
0.6.12 - June 26, 2009 0.6.12 - June 26, 2009
* 1 minor addition * 2 minor additions
* fixed fakeweb test issue (obie fernandez) * fixed fakeweb test issue (obie fernandez)
* added user agent option to searches

0.6.11 - May 18, 2009 0.6.11 - May 18, 2009
* 1 minor addition * 1 minor addition
* Added the ability to sign in with twitter instead of authorizing * Added the ability to sign in with twitter instead of authorizing
Expand Down
9 changes: 7 additions & 2 deletions lib/twitter/search.rb
Expand Up @@ -5,11 +5,16 @@ class Search


attr_reader :result, :query attr_reader :result, :query


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


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

def from(user) def from(user)
@query[:q] << "from:#{user}" @query[:q] << "from:#{user}"
self self
Expand Down Expand Up @@ -92,7 +97,7 @@ def fetch(force=false)
if @fetch.nil? || force if @fetch.nil? || force
query = @query.dup query = @query.dup
query[:q] = query[:q].join(' ') query[:q] = query[:q].join(' ')
response = self.class.get('http://search.twitter.com/search.json', :query => query, :format => :json) response = self.class.get('http://search.twitter.com/search.json', :query => query, :format => :json, :headers => {'User-Agent' => user_agent})
@fetch = Mash.new(response) @fetch = Mash.new(response)
end end


Expand Down
27 changes: 21 additions & 6 deletions test/twitter/search_test.rb
Expand Up @@ -9,6 +9,21 @@ class SearchTest < Test::Unit::TestCase
should "should be able to initialize with a search term" do should "should be able to initialize with a search term" do
Twitter::Search.new('httparty').query[:q].should include('httparty') Twitter::Search.new('httparty').query[:q].should include('httparty')
end end

should "default user agent to Ruby Twitter Gem" do
search = Twitter::Search.new('foo')
search.user_agent.should == 'Ruby Twitter Gem'
end

should "allow overriding default user agent" do
search = Twitter::Search.new('foo', :user_agent => 'Foobar')
search.user_agent.should == 'Foobar'
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.new('foo', :user_agent => 'Foobar').fetch()
end


should "should be able to specify from" do should "should be able to specify from" do
@search.from('jnunemaker').query[:q].should include('from:jnunemaker') @search.from('jnunemaker').query[:q].should include('from:jnunemaker')
Expand Down Expand Up @@ -44,37 +59,37 @@ class SearchTest < Test::Unit::TestCase


should "should be able to specify the language" do should "should be able to specify the language" do
@search.lang('en') @search.lang('en')
@search.class.expects(:get).with('http://search.twitter.com/search.json', :query => {:lang => 'en', :q => ''}, :format => :json).returns({'foo' => 'bar'}) @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.fetch() @search.fetch()
end end


should "should be able to specify the number of results per page" do should "should be able to specify the number of results per page" do
@search.per_page(25) @search.per_page(25)
@search.class.expects(:get).with('http://search.twitter.com/search.json', :query => {:rpp => 25, :q => ''}, :format => :json).returns({'foo' => 'bar'}) @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.fetch() @search.fetch()
end end


should "should be able to specify the page number" do should "should be able to specify the page number" do
@search.page(20) @search.page(20)
@search.class.expects(:get).with('http://search.twitter.com/search.json', :query => {:page => 20, :q => ''}, :format => :json).returns({'foo' => 'bar'}) @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.fetch() @search.fetch()
end end


should "should be able to specify only returning results greater than an id" do should "should be able to specify only returning results greater than an id" do
@search.since(1234) @search.since(1234)
@search.class.expects(:get).with('http://search.twitter.com/search.json', :query => {:since_id => 1234, :q => ''}, :format => :json).returns({'foo' => 'bar'}) @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.fetch() @search.fetch()
end end


should "should be able to specify geo coordinates" do should "should be able to specify geo coordinates" do
@search.geocode('40.757929', '-73.985506', '25mi') @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).returns({'foo' => 'bar'}) @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.fetch() @search.fetch()
end end


should "should be able to specify max id" do should "should be able to specify max id" do
@search.max(1234) @search.max(1234)
@search.class.expects(:get).with('http://search.twitter.com/search.json', :query => {:max_id => 1234, :q => ''}, :format => :json).returns({'foo' => 'bar'}) @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.fetch() @search.fetch()
end end


Expand Down

0 comments on commit e8fbad6

Please sign in to comment.