Skip to content
Browse files

Added user agent option to Twitter::Search and the masses rejoiced in…

… unison!
  • Loading branch information...
1 parent 9b0dbf5 commit e8fbad6a9cfdcfaad4938f7243fc971a1ea8ac8c @jnunemaker jnunemaker committed Jun 26, 2009
Showing with 31 additions and 9 deletions.
  1. +3 −1 History
  2. +7 −2 lib/twitter/search.rb
  3. +21 −6 test/twitter/search_test.rb
View
4 History
@@ -1,6 +1,8 @@
0.6.12 - June 26, 2009
-* 1 minor addition
+* 2 minor additions
* fixed fakeweb test issue (obie fernandez)
+ * added user agent option to searches
+
0.6.11 - May 18, 2009
* 1 minor addition
* Added the ability to sign in with twitter instead of authorizing
View
9 lib/twitter/search.rb
@@ -5,11 +5,16 @@ class Search
attr_reader :result, :query
- def initialize(q=nil)
+ def initialize(q=nil, options={})
+ @options = options
clear
containing(q) if q && q.strip != ''
end
+ def user_agent
+ @options[:user_agent] || 'Ruby Twitter Gem'
+ end
+
def from(user)
@query[:q] << "from:#{user}"
self
@@ -92,7 +97,7 @@ def fetch(force=false)
if @fetch.nil? || force
query = @query.dup
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)
end
View
27 test/twitter/search_test.rb
@@ -9,6 +9,21 @@ class SearchTest < Test::Unit::TestCase
should "should be able to initialize with a search term" do
Twitter::Search.new('httparty').query[:q].should include('httparty')
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
@search.from('jnunemaker').query[:q].should include('from:jnunemaker')
@@ -44,37 +59,37 @@ 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).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()
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).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()
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).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()
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).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()
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).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()
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).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()
end

0 comments on commit e8fbad6

Please sign in to comment.
Something went wrong with that request. Please try again.