require 'open-uri'
require 'json'

url = 'http://search.twitter.com/trends.json'

buffer = open(url, "UserAgent" => "Ruby-Wget").read

# convert JSON data into a hash
result = JSON.parse(buffer)

trends = result['trends']
trends.each do |subject|
  puts subject['name'] + ' ' + subject['url']
end

output:
Clay Aiken http://search.twitter.com/search?q=Clay+Aiken
Sarah Palin http://search.twitter.com/search?q=Sarah+Palin
Heroes http://search.twitter.com/search?q=Heroes
McCain http://search.twitter.com/search?q=McCain
iPhone http://search.twitter.com/search?q=iPhone
Obama http://search.twitter.com/search?q=Obama
Apple http://search.twitter.com/search?q=Apple
T-Mobile G1 http://search.twitter.com/search?q=T-Mobile+G1
AT&T http://search.twitter.com/search?q=AT%26T
CS4 http://search.twitter.com/search?q=CS4

Advertisements