bin/gistribute in gistribute-0.1 vs bin/gistribute in gistribute-0.2

- old
+ new

@@ -1,29 +1,30 @@ #!/usr/bin/env ruby +require "colorize" +require "fileutils" +require "json" +require "open-uri" + +# Print to STDOUT to clear line +CLEAR = "\r\e[K" + if %w[-v --version].include? ARGV.first puts "Gistribute #{File.read(File.expand_path("../../VERSION", __FILE__)).strip}" exit 0 end -require "json" -require "fileutils" -require "open-uri" -require "nutella" - print "Downloading data..." # The user can pass in either just the ID or the entire URL to the Gist. id = ARGV.first[/(^|\/)([[:xdigit:]]+)/, 2] begin - gist = JSON.parse open("https://api.github.com/gists/#{id}").read + gist = JSON.parse(URI.open("https://api.github.com/gists/#{id}").read) rescue OpenURI::HTTPError => msg - print <<-EOS.heredoc.red - - - There was an error downloading the requested Gist. + print <<~EOS.red + #{CLEAR}There was an error downloading the requested Gist. The error is as follows: EOS puts msg puts "The ID that was queried is:".red @@ -37,12 +38,12 @@ # # { # "html_url" => "Link to the Gist", # "description" => "The description for the Gist", # -# "user" => nil, # IF ANONYMOUS -# "user" => { # IF TIED TO A USER +# "owner" => nil, # IF ANONYMOUS +# "owner" => { # IF TIED TO A USER # "login" => "username" # }, # # "files" => { # "filename of first file" => { @@ -56,31 +57,30 @@ # check to see if it's empty- if not, put newlines on each side so that it # will be padded when displayed in the output. desc = gist["description"].gsub(/(.{1,79})(\s+|\Z)/, "\\1\n").strip desc = "\n#{desc}\n" unless desc.empty? -puts <<EOS -\n -Finished downloading Gist from: #{gist["html_url"]} -Gist uploaded by #{ - gist["user"] ? "user #{gist["user"]["login"]}" : "an anonymous user" -}. -#{desc} -Beginning install... +puts <<~EOS + #{CLEAR}Finished downloading Gist from: #{gist["html_url"]} + Gist uploaded by #{ + gist["owner"] ? "user #{gist["owner"]["login"]}" : "an anonymous user" + }. + #{desc} + Beginning install... EOS gist["files"].each do |filename, data| metadata = filename.split("||").map &:strip # | as path separator in the Gist's file name, as Gist doesn't allow the # usage of /. - path = metadata.last.gsub /[~|]/, "|" => "/", "~" => Dir.home + path = metadata.last.gsub(/[~|]/, "|" => "/", "~" => Dir.home) # Default description is the name of the file. description = metadata.size == 1 ? File.basename(path) : metadata.first puts " #{"*".green} Installing #{description}..." # Handle directories that don't exist. FileUtils.mkdir_p File.dirname(path) - File.write path, data["content"] + File.write(path, data["content"]) end