Skip to content

Instantly share code, notes, and snippets.

@i-blis
Created November 28, 2012 17:51
Show Gist options
  • Save i-blis/4162855 to your computer and use it in GitHub Desktop.
Save i-blis/4162855 to your computer and use it in GitHub Desktop.
Download gist files to current directory
#!/usr/bin/env ruby
require 'github_api'
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.on("-a", "--add-gist-id", "Add gist ID to output filename") do |v|
options[:add] = true
end
opts.on("-q", "--quiet", "Run quietly") do |v|
options[:quiet] = true
end
end.parse!
ARGV.each do |gist_id|
gists = Github.gists.get gist_id
files = gists['files']
files.each_key do |file|
filename = files[file]['filename']
content = files[file]['content']
if options[:add]
barename, suffix = filename.split /\.(\w+)$/
filename = barename + "-#{gist_id}" + (suffix.nil? ? '' : ".#{suffix}")
end
File.open(filename, 'w') do |f|
puts "Writing #{filename}" unless options[:quiet]
f.write(content)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment