RubyからTwitter APIを呼び出してツイッターのトレンド情報を取得するプログラム
require 'open-uri'
require 'json'
require 'timeout'
json = nil
begin
timeout(10) {
url = "http://api.twitter.com/1/trends/23424856.json"
json = URI(url).read
}
rescue Timeout::Error
puts "サーバーに接続出来ませんでした。"
rescue
puts "サーバーに接続出来ませんでした。"
else
hash = JSON.parse(json)
hash[0]["trends"].each { |trends|
name = trends["name"]
print(name,"\n")
}
end