Permalink
Browse files

Add methods and specs for Search #source, #place, #near, #positive, #…

…negative, and #question
  • Loading branch information...
1 parent e8ebfc0 commit 1871913342a5621edfebb9a7c8be705608e082d5 @sferik committed Nov 1, 2010
Showing with 124 additions and 12 deletions.
  1. +1 −0 lib/twitter/client/utils.rb
  2. +75 −12 lib/twitter/search.rb
  3. +48 −0 spec/twitter/search_spec.rb
@@ -1,5 +1,6 @@
module Twitter
class Client
+ # @private
module Utils
private
View
@@ -125,6 +125,36 @@ def until_date(date)
end
alias :until :until_date
+ # Only include tweets with a positive attitude
+ #
+ # @return [Twitter::Search] self
+ # @example
+ # Twitter::Search.new.positive.fetch # Returns an array of tweets containing happy emoticons
+ def positive
+ @query[:q] << ":)"
+ self
+ end
+
+ # Only include tweets with a negative attitude
+ #
+ # @return [Twitter::Search] self
+ # @example
+ # Twitter::Search.new.negative.fetch # Returns an array of tweets containing sad emoticons
+ def negative
+ @query[:q] << ":("
+ self
+ end
+
+ # Only include tweets that are asking a question
+ #
+ # @return [Twitter::Search] self
+ # @example
+ # Twitter::Search.new.question.fetch # Returns an array of tweets containing question marks
+ def question
+ @query[:q] << "?"
+ self
+ end
+
# @group Demographic filters
# Only include tweets in a given language, specified by an ISO 639-1 code
@@ -155,7 +185,7 @@ def locale(code)
self
end
- # Only include tweets from users located within a given radius of a given location, specified by latitude and longitude
+ # Only include tweets from users in a given radius of a given location, specified by latitude and longitude
#
# @param lat [Float] A latitude
# @param long [Float] A longitude
@@ -168,6 +198,28 @@ def geocode(lat, long, radius)
self
end
+ # Only include tweets from users in a given place, specified by a place ID
+ #
+ # @param place_id [String] A place ID
+ # @return [Twitter::Search] self
+ # @example
+ # Twitter::Search.new.place("5a110d312052166f").fetch # Returns an array of tweets from San Francisco
+ def place(place_id)
+ @query[:q] << "place:#{place_id}"
+ self
+ end
+
+ # Only include tweets from users near a given location
+ #
+ # @param location [String] A place ID
+ # @return [Twitter::Search] self
+ # @example
+ # Twitter::Search.new.near("San Francisco").fetch # Returns an array of tweets near San Francisco
+ def near(location)
+ @query[:q] << "near:#{location.inspect}"
+ self
+ end
+
# @group User filters
# Only include tweets from a given user, specified by screen_name
@@ -289,17 +341,6 @@ def excluding_hashtag(tag)
alias :excludes_hashtag :excluding_hashtag
alias :exclude_hashtag :excluding_hashtag
- # Specify what type of search results you want to receive
- #
- # @param result_type [String] The type of results you want to receive ('recent', 'popular', or 'mixed')
- # @return [Twitter::Search] self
- # @example
- # Twitter::Search.new.containing("twitter").result_type('recent').fetch # Returns an array of recent tweets containing "twitter"
- def result_type(result_type="mixed")
- @query[:result_type] = result_type
- self
- end
-
# Only include tweets with an ID greater than (that is, more recent than) the specified ID.
#
# @param id [Integer] A Twitter status ID
@@ -324,6 +365,28 @@ def max_id(id)
end
alias :max :max_id
+ # Specify what type of search results you want to receive
+ #
+ # @param result_type [String] The type of results you want to receive ('recent', 'popular', or 'mixed')
+ # @return [Twitter::Search] self
+ # @example
+ # Twitter::Search.new.containing("twitter").result_type("recent").fetch # Returns an array of recent tweets containing "twitter"
+ def result_type(result_type="mixed")
+ @query[:result_type] = result_type
+ self
+ end
+
+ # Only include tweets from a given source
+ #
+ # @param source [String] A Twitter source
+ # @return [Twitter::Search] self
+ # @example
+ # Twitter::Search.new.containing("twitter").source("Hibari").fetch # Returns an array of tweets containing "twitter", posted from Hibari
+ def source(source)
+ @query[:q] << "source:#{source}"
+ self
+ end
+
# @group Paging
# Specify the number of tweets to return per page
@@ -217,6 +217,14 @@
end
+ describe ".source" do
+
+ it "should set the source" do
+ @client.source("Hibari").query[:q].should include 'source:Hibari'
+ end
+
+ end
+
describe ".since_id" do
it "should set the since id" do
@@ -249,6 +257,30 @@
end
+ describe ".positive" do
+
+ it "should set the query to include ':)'" do
+ @client.positive.query[:q].should include ':)'
+ end
+
+ end
+
+ describe ".negative" do
+
+ it "should set the query to include ':('" do
+ @client.negative.query[:q].should include ':('
+ end
+
+ end
+
+ describe ".question" do
+
+ it "should set the query to include '?'" do
+ @client.question.query[:q].should include '?'
+ end
+
+ end
+
describe ".geocode" do
it "should set the geocode" do
@@ -257,6 +289,22 @@
end
+ describe ".place" do
+
+ it "should set the place" do
+ @client.place("5a110d312052166f").query[:q].should include 'place:5a110d312052166f'
+ end
+
+ end
+
+ describe ".near" do
+
+ it "should set the near location" do
+ @client.near("San Francisco").query[:q].should include 'near:"San Francisco"'
+ end
+
+ end
+
describe ".per_page" do
it "should set the number of results per page" do

0 comments on commit 1871913

Please sign in to comment.