Created
May 31, 2012 07:44
-
-
Save jnx/2841711 to your computer and use it in GitHub Desktop.
Hackernews in terminal
This file contains 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 'open-uri' | |
class Story | |
attr_accessor :title, :url | |
def initialize(title, url) | |
@title = title | |
@url = url | |
end | |
def open | |
`open #{url}` | |
end | |
end | |
class HackerNewsParser | |
REG_EXP = /<td class="title"><a href="(http[^"]+)">([^>]+)<\/a>/ | |
def initialize | |
@html_source = open('http://news.ycombinator.com').read | |
@stories = [] | |
end | |
def parse | |
@html_source.scan(REG_EXP) do |match| | |
@stories.push(Story.new(match[1], match[0])) | |
end | |
@stories | |
end | |
end | |
stories = HackerNewsParser.new.parse | |
loop do | |
stories.each_with_index do |story, index| | |
puts "#{index + 1} #{story.title}" | |
end | |
story_number = gets.to_i | |
stories[story_number - 1].open | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment