Created
February 16, 2009 03:43
-
-
Save Oshuma/64988 to your computer and use it in GitHub Desktop.
Simple class to grab the latest trendy asshole topics.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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