Skip to content

Instantly share code, notes, and snippets.

@dbc-challenges
Last active December 16, 2015 02:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbc-challenges/5360499 to your computer and use it in GitHub Desktop.
Save dbc-challenges/5360499 to your computer and use it in GitHub Desktop.
save this toyour ~/bin directory as "fgist" then run "chmod +x ~/bin/fgist" then make sure ~/bin is on your $PATH This will download all the contents of a gist to your current directory, given a gist url. run as: fgist https://gist.github.com/dbc-challenges/5360499
#!/usr/bin/env ruby
require 'net/https'
require 'json'
# a simple wrapper to do an HTTP GET
def fetch_uri(uri)
uri = URI(uri)
Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.get(uri.path).body
end
end
# GETs the URI and returns a Ruby hash.
def fetch_json(uri)
result = fetch_uri(uri)
JSON.load(result)
end
def download_files(json)
json["files"].each do |filename, file_hash|
print "downloading #{filename}... "
File.open(filename, "w+") do |f|
f.write(file_hash["content"])
end
puts "done."
end
end
# Let's begin!
gist_id = ARGV[0].match(/\/([\w\d]+)$/)[1]
json = fetch_json("https://api.github.com/gists/#{gist_id}")
download_files(json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment