Skip to content

Commit

Permalink
Added support for trends/available and trends/location
Browse files Browse the repository at this point in the history
  • Loading branch information
pengwynn committed Feb 11, 2010
1 parent e3e8f7e commit 39b8d8d
Show file tree
Hide file tree
Showing 8 changed files with 2,281 additions and 2 deletions.
1 change: 1 addition & 0 deletions History
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
0.8.4 - February 11, 2010
* Added membership query options // thanks ming yeow ng
* Added support for a phrase search (instead of a group of words) from @zagari
* Added support for trends/available and trends/location
0.8.3 - January 29, 2010
* Added patch for unauthenticated list timelines from @spastorino
0.8.2 - January 21, 2010
Expand Down
18 changes: 18 additions & 0 deletions lib/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ def self.list_timeline(list_owner_username, slug, query = {})
end
end

module Hashie
class Mash

# Converts all of the keys to strings, optionally formatting key name
def rubyify_keys!
keys.each{|k|
v = delete(k)
new_key = k.to_s.underscore
self[new_key] = v
v.rubyify_keys! if v.is_a?(Hash)
v.each{|p| p.rubyify_keys! if p.is_a?(Hash)} if v.is_a?(Array)
}
self
end

end
end

directory = File.expand_path(File.dirname(__FILE__))

require File.join(directory, 'twitter', 'oauth')
Expand Down
3 changes: 2 additions & 1 deletion lib/twitter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ def blocked_ids
def blocking(options={})
perform_get("/blocks/blocking.json", options)
end



protected
def self.mime_type(file)
case
Expand Down
8 changes: 8 additions & 0 deletions lib/twitter/trends.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ def self.daily(options={})
def self.weekly(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)}
end

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

private
def self.mashup(response)
Expand Down
253 changes: 253 additions & 0 deletions test/fixtures/trends_available.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
[{
"url": "http://where.yahooapis.com/v1/place/23424803",
"placeType": {
"code": 12,
"name": "Country"
},
"woeid": 23424803,
"countryCode": "IE",
"name": "Ireland",
"country": "Ireland"
},
{
"countryCode": "MX",
"country": "Mexico",
"url": "http://where.yahooapis.com/v1/place/23424900",
"name": "Mexico",
"placeType": {
"code": 12,
"name": "Country"
},
"woeid": 23424900
},
{
"url": "http://where.yahooapis.com/v1/place/23424975",
"woeid": 23424975,
"placeType": {
"code": 12,
"name": "Country"
},
"countryCode": "GB",
"name": "United Kingdom",
"country": "United Kingdom"
},
{
"url": "http://where.yahooapis.com/v1/place/2358820",
"placeType": {
"code": 7,
"name": "Town"
},
"countryCode": "US",
"woeid": 2358820,
"name": "Baltimore",
"country": "United States"
},
{
"countryCode": "US",
"country": "United States",
"url": "http://where.yahooapis.com/v1/place/2514815",
"name": "Washington",
"placeType": {
"code": 7,
"name": "Town"
},
"woeid": 2514815
},
{
"url": "http://where.yahooapis.com/v1/place/2367105",
"placeType": {
"code": 7,
"name": "Town"
},
"woeid": 2367105,
"countryCode": "US",
"name": "Boston",
"country": "United States"
},
{
"country": "Brazil",
"url": "http://where.yahooapis.com/v1/place/455827",
"placeType": {
"code": 7,
"name": "Town"
},
"countryCode": "BR",
"name": "Sao Paulo",
"woeid": 455827
},
{
"country": "United States",
"url": "http://where.yahooapis.com/v1/place/2459115",
"placeType": {
"code": 7,
"name": "Town"
},
"countryCode": "US",
"name": "New York",
"woeid": 2459115
},
{
"url": "http://where.yahooapis.com/v1/place/2487796",
"countryCode": "US",
"woeid": 2487796,
"name": "San Antonio",
"country": "United States",
"placeType": {
"code": 7,
"name": "Town"
}
},
{
"url": "http://where.yahooapis.com/v1/place/23424977",
"woeid": 23424977,
"placeType": {
"code": 12,
"name": "Country"
},
"countryCode": "US",
"name": "United States",
"country": "United States"
},
{
"url": "http://where.yahooapis.com/v1/place/23424768",
"woeid": 23424768,
"placeType": {
"code": 12,
"name": "Country"
},
"countryCode": "BR",
"name": "Brazil",
"country": "Brazil"
},
{
"url": "http://where.yahooapis.com/v1/place/2379574",
"placeType": {
"code": 7,
"name": "Town"
},
"countryCode": "US",
"woeid": 2379574,
"name": "Chicago",
"country": "United States"
},
{
"url": "http://where.yahooapis.com/v1/place/2471217",
"placeType": {
"code": 7,
"name": "Town"
},
"woeid": 2471217,
"countryCode": "US",
"name": "Philadelphia",
"country": "United States"
},
{
"countryCode": "US",
"country": "United States",
"url": "http://where.yahooapis.com/v1/place/2424766",
"name": "Houston",
"placeType": {
"code": 7,
"name": "Town"
},
"woeid": 2424766
},
{
"countryCode": "US",
"country": "United States",
"url": "http://where.yahooapis.com/v1/place/2442047",
"name": "Los Angeles",
"placeType": {
"code": 7,
"name": "Town"
},
"woeid": 2442047
},
{
"countryCode": "US",
"country": "United States",
"url": "http://where.yahooapis.com/v1/place/2487956",
"name": "San Francisco",
"placeType": {
"code": 7,
"name": "Town"
},
"woeid": 2487956
},
{
"url": "http://where.yahooapis.com/v1/place/23424775",
"woeid": 23424775,
"placeType": {
"code": 12,
"name": "Country"
},
"countryCode": "CA",
"name": "Canada",
"country": "Canada"
},
{
"country": "United States",
"url": "http://where.yahooapis.com/v1/place/2357024",
"placeType": {
"code": 7,
"name": "Town"
},
"countryCode": "US",
"name": "Atlanta",
"woeid": 2357024
},
{
"url": "http://where.yahooapis.com/v1/place/2406080",
"countryCode": "US",
"woeid": 2406080,
"name": "Fort Worth",
"country": "United States",
"placeType": {
"code": 7,
"name": "Town"
}
},
{
"url": "http://where.yahooapis.com/v1/place/2388929",
"countryCode": "US",
"woeid": 2388929,
"name": "Dallas",
"country": "United States",
"placeType": {
"code": 7,
"name": "Town"
}
},
{
"countryCode": "US",
"country": "United States",
"url": "http://where.yahooapis.com/v1/place/2490383",
"name": "Seattle",
"placeType": {
"code": 7,
"name": "Town"
},
"woeid": 2490383
},
{
"countryCode": "GB",
"country": "United Kingdom",
"url": "http://where.yahooapis.com/v1/place/44418",
"name": "London",
"placeType": {
"code": 7,
"name": "Town"
},
"woeid": 44418
},
{
"url": "http://where.yahooapis.com/v1/place/1",
"placeType": {
"code": 19,
"name": "Supername"
},
"countryCode": null,
"woeid": 1,
"name": "Earth",
"country": ""
}]
1,926 changes: 1,925 additions & 1 deletion test/fixtures/trends_daily.json

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions test/fixtures/trends_location.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[{
"trends": [{
"url": "http://search.twitter.com/search?q=%23%23cakkatvstreamers",
"name": "##cakkatvstreamers",
"query": "%23%23cakkatvstreamers"
},
{
"url": "http://search.twitter.com/search?q=Google+Buzz",
"name": "Google Buzz",
"query": "Google+Buzz"
},
{
"url": "http://search.twitter.com/search?q=iPad",
"name": "iPad",
"query": "iPad"
},
{
"url": "http://search.twitter.com/search?q=Fashion+Week",
"name": "Fashion Week",
"query": "Fashion+Week"
},
{
"url": "http://search.twitter.com/search?q=Trivium",
"name": "Trivium",
"query": "Trivium"
},
{
"url": "http://search.twitter.com/search?q=Haiti",
"name": "Haiti",
"query": "Haiti"
},
{
"url": "http://search.twitter.com/search?q=%23hbu",
"name": "#hbu",
"query": "%23hbu"
},
{
"url": "http://search.twitter.com/search?q=Olympics",
"name": "Olympics",
"query": "Olympics"
},
{
"url": "http://search.twitter.com/search?q=Event",
"name": "Event",
"query": "Event"
},
{
"url": "http://search.twitter.com/search?q=Gmail",
"name": "Gmail",
"query": "Gmail"
}],
"locations": [{
"woeid": 2487956,
"name": "San Francisco"
}],
"as_of": "2010-02-11T14:00:02Z"
}]
17 changes: 17 additions & 0 deletions test/twitter/trends_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,21 @@ class TrendsTest < Test::Unit::TestCase
trends[0].query.should == 'TGIF'
end
end

context "Getting local trends" do

should "return a list of available locations" do
stub_get 'http://api.twitter.com/1/trends/available.json?lat=33.237593417&lng=-96.960559033', 'trends_available.json'
locations = Trends.available(:lat => 33.237593417, :lng => -96.960559033)
locations.first.country.should == 'Ireland'
locations.first.placeType.code.should == 12
end

should "return a list of trends for a given location" do
stub_get 'http://api.twitter.com/1/trends/2487956.json', 'trends_location.json'
trends = Trends.for_location(2487956).first.trends
trends.last.name.should == 'Gmail'
end
end

end

0 comments on commit 39b8d8d

Please sign in to comment.