Skip to content

Commit

Permalink
raise a SearchOperatorError when near: or within: are in the query
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Croak committed May 18, 2009
1 parent cbf50bb commit 4c040be
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
13 changes: 13 additions & 0 deletions lib/twitter_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
require File.join(File.dirname(__FILE__), 'trends')

module TwitterSearch
class SearchOperatorError < ArgumentError
end

class Client
TWITTER_SEARCH_API_URL = 'http://search.twitter.com/search.json'
TWITTER_TRENDS_API_URL = 'http://search.twitter.com/trends/current.json'
Expand All @@ -29,6 +32,8 @@ def query(opts = {})
url = URI.parse(TWITTER_SEARCH_API_URL)
url.query = sanitize_query(opts)

ensure_no_location_operators(url.query)

req = Net::HTTP::Get.new(url.path)
http = Net::HTTP.new(url.host, url.port)
http.read_timeout = timeout
Expand Down Expand Up @@ -71,5 +76,13 @@ def sanitize_query_hash(query_hash)
}.join('&')
end

def ensure_no_location_operators(query_string)
if query_string.include?("near%3A") ||
query_string.include?("within%3A")
raise TwitterSearch::SearchOperatorError,
"near: and within: are available from the Twitter Search web interface, but not the API. The API requires the geocode parameter. See dancroak/twitter-search README."
end
end

end
end
24 changes: 10 additions & 14 deletions test/operators_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,20 @@ class OperatorsTest < Test::Unit::TestCase # :nodoc:
end

context "@client.query :q => '\"happy hour\" near:\"san francisco\"'" do
setup do
@tweets = read_yaml :file => 'happy_hour_near_sf'
end

# Twitter Search API requires the geocode parameter for location searching
should 'not find tweets using the near operator' do
assert ! @tweets.any?
should 'raise SearchOperatorError' do
assert_raise TwitterSearch::SearchOperatorError do
client = TwitterSearch::Client.new
client.query '"happy hour" near:"san francisco"'
end
end
end

context "@client.query :q => 'near:NYC within:15mi'" do
setup do
@tweets = read_yaml :file => 'within_15mi_nyc'
end

# Twitter Search API requires the geocode parameter for location searching
should 'not find tweets using the near operator' do
assert ! @tweets.any?
should 'raise SearchOperatorError' do
assert_raise TwitterSearch::SearchOperatorError do
client = TwitterSearch::Client.new
client.query 'near:NYC within:15mi'
end
end
end

Expand Down

0 comments on commit 4c040be

Please sign in to comment.