Skip to content

Commit

Permalink
Added Twitter API versioning support for trends methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Mar 21, 2010
1 parent 9ed3709 commit 6f23c5e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
14 changes: 7 additions & 7 deletions lib/twitter/trends.rb
@@ -1,37 +1,37 @@
module Twitter
class Trends
include HTTParty
base_uri 'search.twitter.com/trends'
base_uri "api.twitter.com/1/trends"
format :json

# :exclude => 'hashtags' to exclude hashtags
def self.current(options={})
mashup(get('/current.json', :query => options))
mashup(get("/current.json", :query => options))
end

# :exclude => 'hashtags' to exclude hashtags
# :date => yyyy-mm-dd for specific date
def self.daily(options={})
mashup(get('/daily.json', :query => options))
mashup(get("/daily.json", :query => options))
end

# :exclude => 'hashtags' to exclude hashtags
# :date => yyyy-mm-dd for specific date
def self.weekly(options={})
mashup(get('/weekly.json', :query => options))
mashup(get("/weekly.json", :query => options))
end

def self.available(query={})
locations = get('http://api.twitter.com/1/trends/available.json', :query => query).map{|location| Hashie::Mash.new(location)}
locations = get("http://api.twitter.com/#{API_VERSION}/trends/available.json", :query => query).map{|location| Hashie::Mash.new(location)}
end

def self.for_location(woeid)
get("http://api.twitter.com/1/trends/#{woeid}.json").map{|location| Hashie::Mash.new(location)}
get("http://api.twitter.com/#{API_VERSION}/trends/#{woeid}.json").map{|location| Hashie::Mash.new(location)}
end

private
def self.mashup(response)
response['trends'].values.flatten.map { |t| Hashie::Mash.new(t) }
response["trends"].values.flatten.map { |t| Hashie::Mash.new(t) }
end
end
end
22 changes: 11 additions & 11 deletions test/twitter/trends_test.rb
Expand Up @@ -5,7 +5,7 @@ class TrendsTest < Test::Unit::TestCase

context "Getting current trends" do
should "work" do
stub_get 'http://search.twitter.com:80/trends/current.json', 'trends_current.json'
stub_get 'http://api.twitter.com:80/1/trends/current.json', 'trends_current.json'
trends = Trends.current
trends.size.should == 10
trends[0].name.should == '#musicmonday'
Expand All @@ -15,7 +15,7 @@ class TrendsTest < Test::Unit::TestCase
end

should "be able to exclude hashtags" do
stub_get 'http://search.twitter.com:80/trends/current.json?exclude=hashtags', 'trends_current_exclude.json'
stub_get 'http://api.twitter.com:80/1/trends/current.json?exclude=hashtags', 'trends_current_exclude.json'
trends = Trends.current(:exclude => 'hashtags')
trends.size.should == 10
trends[0].name.should == 'New Divide'
Expand All @@ -27,31 +27,31 @@ class TrendsTest < Test::Unit::TestCase

context "Getting daily trends" do
should "work" do
stub_get 'http://search.twitter.com:80/trends/daily.json?', 'trends_daily.json'
stub_get 'http://api.twitter.com:80/1/trends/daily.json?', 'trends_daily.json'
trends = Trends.daily
trends.size.should == 480
trends[0].name.should == '#3turnoffwords'
trends[0].query.should == '#3turnoffwords'
end

should "be able to exclude hastags" do
stub_get 'http://search.twitter.com:80/trends/daily.json?exclude=hashtags', 'trends_daily_exclude.json'
stub_get 'http://api.twitter.com:80/1/trends/daily.json?exclude=hashtags', 'trends_daily_exclude.json'
trends = Trends.daily(:exclude => 'hashtags')
trends.size.should == 480
trends[0].name.should == 'Kobe'
trends[0].query.should == %Q(Kobe)
end

should "be able to get for specific date (with date string)" do
stub_get 'http://search.twitter.com:80/trends/daily.json?date=2009-05-01', 'trends_daily_date.json'
stub_get 'http://api.twitter.com:80/1/trends/daily.json?date=2009-05-01', 'trends_daily_date.json'
trends = Trends.daily(:date => '2009-05-01')
trends.size.should == 440
trends[0].name.should == 'Swine Flu'
trends[0].query.should == %Q(\"Swine Flu\" OR Flu)
end

should "be able to get for specific date (with date object)" do
stub_get 'http://search.twitter.com:80/trends/daily.json?date=2009-05-01', 'trends_daily_date.json'
stub_get 'http://api.twitter.com:80/1/trends/daily.json?date=2009-05-01', 'trends_daily_date.json'
trends = Trends.daily(:date => Date.new(2009, 5, 1))
trends.size.should == 440
trends[0].name.should == 'Swine Flu'
Expand All @@ -61,31 +61,31 @@ class TrendsTest < Test::Unit::TestCase

context "Getting weekly trends" do
should "work" do
stub_get 'http://search.twitter.com:80/trends/weekly.json?', 'trends_weekly.json'
stub_get 'http://api.twitter.com:80/1/trends/weekly.json?', 'trends_weekly.json'
trends = Trends.weekly
trends.size.should == 210
trends[0].name.should == 'Happy Mothers Day'
trends[0].query.should == %Q(\"Happy Mothers Day\" OR \"Mothers Day\")
end

should "be able to exclude hastags" do
stub_get 'http://search.twitter.com:80/trends/weekly.json?exclude=hashtags', 'trends_weekly_exclude.json'
stub_get 'http://api.twitter.com:80/1/trends/weekly.json?exclude=hashtags', 'trends_weekly_exclude.json'
trends = Trends.weekly(:exclude => 'hashtags')
trends.size.should == 210
trends[0].name.should == 'Happy Mothers Day'
trends[0].query.should == %Q(\"Happy Mothers Day\" OR \"Mothers Day\")
end

should "be able to get for specific date (with date string)" do
stub_get 'http://search.twitter.com:80/trends/weekly.json?date=2009-05-01', 'trends_weekly_date.json'
stub_get 'http://api.twitter.com:80/1/trends/weekly.json?date=2009-05-01', 'trends_weekly_date.json'
trends = Trends.weekly(:date => '2009-05-01')
trends.size.should == 210
trends[0].name.should == 'TGIF'
trends[0].query.should == 'TGIF'
end

should "be able to get for specific date (with date object)" do
stub_get 'http://search.twitter.com:80/trends/weekly.json?date=2009-05-01', 'trends_weekly_date.json'
stub_get 'http://api.twitter.com:80/1/trends/weekly.json?date=2009-05-01', 'trends_weekly_date.json'
trends = Trends.weekly(:date => Date.new(2009, 5, 1))
trends.size.should == 210
trends[0].name.should == 'TGIF'
Expand All @@ -109,4 +109,4 @@ class TrendsTest < Test::Unit::TestCase
end
end

end
end

0 comments on commit 6f23c5e

Please sign in to comment.