Created
April 29, 2010 16:17
-
-
Save jmorton/383837 to your computer and use it in GitHub Desktop.
Load and evaluate gists
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For example: