Permalink
Browse files

Added current trends and ability to exclude hash tags.

  • Loading branch information...
1 parent e9ea194 commit 549f34903be38232c24044d9972629a86a0503a4 @jnunemaker jnunemaker committed May 18, 2009
View
3 lib/twitter.rb
@@ -59,4 +59,5 @@ def self.follower_ids(id)
require File.join(directory, 'twitter', 'httpauth')
require File.join(directory, 'twitter', 'request')
require File.join(directory, 'twitter', 'base')
-require File.join(directory, 'twitter', 'search')
+require File.join(directory, 'twitter', 'search')
+require File.join(directory, 'twitter', 'trends')
View
13 lib/twitter/trends.rb
@@ -0,0 +1,13 @@
+module Twitter
+ class Trends
+ include HTTParty
+ base_uri 'search.twitter.com/trends'
+ format :json
+
+ # :exclude => 'hashtags' to exclude hashtags
+ def self.current(options={})
+ response = get('/current.json', :query => options)
+ response['trends'].values.flatten.map { |t| Mash.new(t) }
+ end
+ end
+end
View
1 test/fixtures/trends_current.json
@@ -0,0 +1 @@
+{"trends":{"2009-05-18 20:53:51":[{"query":"#musicmonday","name":"#musicmonday"},{"query":"#newdivide","name":"#newdivide"},{"query":"\"New Divide\"","name":"New Divide"},{"query":"#3turnoffwords","name":"#3turnoffwords"},{"query":"#3hotwords","name":"#3hotwords"},{"query":"#ecomonday","name":"#ecomonday"},{"query":"#medievalmovies","name":"#medievalmovies"},{"query":"\"Star Trek\"","name":"Star Trek"},{"query":"#wolframalpha","name":"#wolframalpha"},{"query":"AT&T","name":"AT&T"}]},"as_of":1242680031}
View
1 test/fixtures/trends_current_exclude.json
@@ -0,0 +1 @@
+{"as_of":1242679796,"trends":{"2009-05-18 20:49:56":[{"query":"\"New Divide\"","name":"New Divide"},{"query":"\"Star Trek\"","name":"Star Trek"},{"query":"AT&T","name":"AT&T"},{"query":"\"Angels & Demons\"","name":"Angels & Demons"},{"query":"RIP","name":"RIP"},{"query":"Dollhouse","name":"Dollhouse"},{"query":"Ashes","name":"Ashes"},{"query":"\"Linkin Park\"","name":"Linkin Park"},{"query":"\"Memorial Day\" OR \"Green Day\"","name":"Memorial Day"},{"query":"Brazil","name":"Brazil"}]}}
View
25 test/twitter/trends_test.rb
@@ -0,0 +1,25 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class TrendsTest < Test::Unit::TestCase
+ include Twitter
+
+ should "be able to get current trends" do
+ stub_get('http://search.twitter.com:80/trends/current.json', 'trends_current.json')
+ trends = Trends.current
+ trends.size.should == 10
+ trends[0].name.should == '#musicmonday'
+ trends[0].query.should == '#musicmonday'
+ trends[1].name.should == '#newdivide'
+ trends[1].query.should == '#newdivide'
+ end
+
+ should "be able to exclude hashtags from current trends" do
+ stub_get('http://search.twitter.com:80/trends/current.json?exclude=hashtags', 'trends_current_exclude.json')
+ trends = Trends.current(:exclude => 'hashtags')
+ trends.size.should == 10
+ trends[0].name.should == 'New Divide'
+ trends[0].query.should == %Q(\"New Divide\")
+ trends[1].name.should == 'Star Trek'
+ trends[1].query.should == %Q(\"Star Trek\")
+ end
+end

0 comments on commit 549f349

Please sign in to comment.