Skip to content

Commit

Permalink
Added current trends and ability to exclude hash tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed May 18, 2009
1 parent e9ea194 commit 549f349
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
13 changes: 13 additions & 0 deletions lib/twitter/trends.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/fixtures/trends_current.json
Original file line number Diff line number Diff line change
@@ -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}
1 change: 1 addition & 0 deletions test/fixtures/trends_current_exclude.json
Original file line number Diff line number Diff line change
@@ -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"}]}}
25 changes: 25 additions & 0 deletions test/twitter/trends_test.rb
Original file line number Diff line number Diff line change
@@ -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.