Skip to content

Instantly share code, notes, and snippets.

@jnx
Created May 31, 2012 07:44
Show Gist options
  • Save jnx/2841711 to your computer and use it in GitHub Desktop.
Save jnx/2841711 to your computer and use it in GitHub Desktop.
Hackernews in terminal
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