Skip to content

Instantly share code, notes, and snippets.

@jmorton
Created April 29, 2010 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmorton/383837 to your computer and use it in GitHub Desktop.
Save jmorton/383837 to your computer and use it in GitHub Desktop.
Load and evaluate gists
def gitget(uri)
# We look for very specific links within the page like:
# <a href="/raw/383785/5bcefcae03bbc40d6ce40591d93f278585e47950/example.rb">raw</a>
raw_uri_pattern = /\<a href="(.+)"\>raw\<\/a\>/
begin
# Load the referenced gist
gist_html = open(uri).read
# Match the HTML against the given pattern and load each
# referenced raw file. The formatted source would need
# a bunch of parsing to make it useful so we just fetch
# each raw file instead.
file_names = gist_html.scan(raw_uri_pattern).map.map do |match|
# Extract the first grouping of the match
file_name = match.first
# The path is relative, we make it absolute
raw_uri = "http://gist.github.com#{file_name}"
# This seems exceptionally dangerous...
src = open(raw_uri).read
Kernel::eval(src.untaint)
# Keep track of files we load...
file_name
end
return "Eval'd #{file_names.join(', ')}"
rescue Exception => e
# Or report generic unhelpful failure
puts "Could not load the gist."
puts e.to_s
end
end
@jmorton
Copy link
Author

jmorton commented Apr 29, 2010

For example:

gitget 'http://gist.github.com/383785'
localhost()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment