Skip to content

Workaround for HTTPS + using TM_GIT #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletions Support/lib/gist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 }
Expand All @@ -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