Skip to content

Commit

Permalink
Add methods and specs for Search #source, #place, #near, #positive, #…
Browse files Browse the repository at this point in the history
…negative, and #question
  • Loading branch information
sferik committed Nov 1, 2010
1 parent e8ebfc0 commit 1871913
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/twitter/client/utils.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Twitter
class Client
# @private
module Utils
private

Expand Down
87 changes: 75 additions & 12 deletions lib/twitter/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
48 changes: 48 additions & 0 deletions spec/twitter/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 1871913

Please sign in to comment.