Skip to content

Instantly share code, notes, and snippets.

@Oshuma
Created February 16, 2009 03:43
Show Gist options
  • Select an option

  • Save Oshuma/64988 to your computer and use it in GitHub Desktop.

Select an option

Save Oshuma/64988 to your computer and use it in GitHub Desktop.
Simple class to grab the latest trendy asshole topics.
require 'json'
require 'open-uri'
# Access the latest Twitter trends.
#
# Usage:
#
# TweetTrend.new.display
#
# - or -
#
# current = TweetTrend.new
# current.trends.each do |trend|
# puts trend['name']
# puts trend['url']
# end
#
# current.get_trends # Re-fetch the trends.
class TweetTrend
# Accessor for the +trends+ hash.
attr_accessor :trends
HOST = 'http://search.twitter.com/trends.json'
def initialize
get_trends
end
# Prints a list of the +trends+ to STDOUT.
def display
$stdout.puts "Twitter trends as of #{@time}"
$stdout.puts # newline
@trends.each do |trend|
$stdout.puts trend['name']
$stdout.puts "-- #{trend['url']}"
end
end
# Grabs the latest trends from HOST.
def get_trends
response = JSON.parse(open(HOST).read)
@time = response['as_of']
@trends = response['trends']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment