Skip to content
Browse files

Added support for trends/available and trends/location

  • Loading branch information...
1 parent e3e8f7e commit 39b8d8dd3bb25cb5cd081cae23486fb47c25ec8f @pengwynn pengwynn committed
View
1 History
@@ -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
View
18 lib/twitter.rb
@@ -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')
View
3 lib/twitter/base.rb
@@ -284,7 +284,8 @@ def blocked_ids
def blocking(options={})
perform_get("/blocks/blocking.json", options)
end
-
+
+
protected
def self.mime_type(file)
case
View
8 lib/twitter/trends.rb
@@ -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)
View
253 test/fixtures/trends_available.json
@@ -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": ""
+}]
View
1,926 test/fixtures/trends_daily.json
1,925 additions, 1 deletion not shown because the diff is too large. Please use a local Git client to view these changes.
View
57 test/fixtures/trends_location.json
@@ -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"
+}]
View
17 test/twitter/trends_test.rb
@@ -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.
Something went wrong with that request. Please try again.