diff --git a/Support/lib/gist.rb b/Support/lib/gist.rb index 6a80d9c..ff0c18e 100755 --- a/Support/lib/gist.rb +++ b/Support/lib/gist.rb @@ -2,7 +2,7 @@ # Taken from defunkt's gist repository: http://github.com/defunkt/gist/tree/master require 'open-uri' -require 'net/http' +require 'net/https' module Gist extend self @@ -14,33 +14,42 @@ def read(gist_id) return help if gist_id == '-h' || gist_id.nil? || gist_id[/help/] open(@@gist_url % gist_id).read end - + def add_file(name, content) load_files @@files << {'name' => name, 'content' => content} puts "#{name} added." save_files end - + def send(private_gist) load_files url = URI.parse('https://gist.github.com/gists') - req = Net::HTTP.post_form(url, data(private_gist)) - case req + + req = Net::HTTP::Post.new(url.path) + req.set_form_data data(private_gist) + + http = Net::HTTP.new(url.host, url.port) + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + + res = http.request req + + case res when Net::HTTPBadRequest - print "Ewww, not your fault, but something bad happened. No gist created." + print "Ewww, not your fault, but something bad happened. No gist created.\n#{res.body}" when Net::HTTPFound - url = copy req['Location'] + url = copy res['Location'] print "Created gist at #{url}. URL copied to clipboard." end clear end - + def clear @@files = [] save_files end - + def process_selection selection = nil gistname = nil @@ -51,10 +60,10 @@ def process_selection selection = STDIN.read gistname = get_gist_name end - + add_file(gistname, selection) end - + # Add extension for supported modes based on TM_SCOPE # Cribbed from http://github.com/defunkt/gist.el/tree/master/gist.el def get_extension @@ -94,7 +103,7 @@ def get_extension else "txt" end end - + def get_gist_name if filepath = ENV['TM_FILEPATH'] ENV['TM_PROJECT_DIRECTORY'] ? filepath.sub(ENV['TM_PROJECT_DIRECTORY'], '') : File.basename(filepath) @@ -110,12 +119,12 @@ def load_files @@files = Marshal.load(File.read(path)) @@files ||= [] end - + def save_files path = File.join(File.dirname(__FILE__), 'tmp_gists') File.open(path, 'w') {|f| f.puts Marshal.dump(@@files) } end - + def copy(content) return content if `which pbcopy`.strip == '' IO.popen('pbcopy', 'r+') { |clip| clip.puts content } @@ -135,10 +144,14 @@ def data(private_gist) end def auth - user = `git config --global github.user`.strip - token = `git config --global github.token`.strip + user = `#{git} config --global github.user`.strip + token = `#{git} config --global github.token`.strip user.empty? ? {} : { 'login' => user, 'token' => token } end + + def git + @git ||= ENV['TM_GIT'] || 'git' + end end