Skip to content

Instantly share code, notes, and snippets.

@zmalltalker
Created July 30, 2008 14:54
Show Gist options
  • Save zmalltalker/3270 to your computer and use it in GitHub Desktop.
Save zmalltalker/3270 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'hpricot'
require 'open-uri'
class Gist
attr_accessor :gist_id, :title, :author
def self.find_all(author="zmalltalker")
result = []
doc = Hpricot(open("http://gist.github.com/#{author}"))
(doc/"div.file").each do |file|
link = (file/"div.info/span/a").first
g = new
g.author = author
g.gist_id = link[:href].split("/").last
data = (file/"//div.highlight").inner_text[0,50]
g.title = data
result << g
end
return result
end
def contents
@contents ||= load_attributes
end
def to_s
result = []
result << "Gist ##{gist_id} by #{author} #{raw_link}"
result << "=" * 80
result << contents
result << "\n" * 5
result.join("\n")
end
def load_attributes
if link = raw_link
doc = open(link).read
return doc
end
end
def raw_link
@raw_link ||= find_raw_link
end
def find_raw_link
doc = Hpricot(open("http://gist.github.com/#{gist_id}"))
links = (doc/"a")
links.each do |m|
if m.inner_html == 'raw'
return "http://gist.github.com#{m[:href]}"
end
end
end
end
gists = Gist.find_all
gists.each do |g|
puts g
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment