Skip to content

Instantly share code, notes, and snippets.

@vangberg
Forked from pieter/gistie.rb
Created July 22, 2008 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vangberg/1230 to your computer and use it in GitHub Desktop.
Save vangberg/1230 to your computer and use it in GitHub Desktop.
gistie
#!/usr/bin/env ruby
# Made by Pieter de Bie <frimmirf@gmail.com>
# Based on a "Pastie" task by someone
require "tempfile"
GIST_URL = 'http://gist.github.com/gists'
GIST_LOGIN_URL = 'https://gist.github.com/session'
USERNAME = "pieter"
PASSWORD = "xxxx"
def login_cookie
headers = `curl --insecure #{GIST_LOGIN_URL} -s -i -F"login=#{USERNAME}" -F "password=#{PASSWORD}"`
if headers =~ /Set-Cookie: (.*); do/
return $1
end
return nil
end
def create_snippet(filename, extension, data, cookie)
text_file = Tempfile.open('w+')
text_file << data
text_file.flush
cmd = <<-EOS
curl #{GIST_URL} \
-b '#{cookie}' \
-s -L -o /dev/null -w "%{url_effective}" \
-F "file_ext[gistfile1]=#{extension}" \
-F "file_name[gistfile1]=#{filename}" \
-F "file_contents[gistfile1]=<#{text_file.path}" \
-F "x=27" \
-F "y=27"
EOS
url = %x{#{cmd}}
text_file.close(true)
return url
end
if ARGV.include? "-p"
data = `pbpaste`
elsif !ARGV.empty?
@filename = ARGV.shift
data = File.read(File.expand_path(@filename))
elsif ENV["FILE"]
@filename = ENV["FILE"]
data = File.read(File.expand_path(ENV["FILE"]))
else
data = STDIN.read
end
@filename ||= "Unnamed File"
@extension = File.extname(@filename)
@extension = "txt" if @extension == ""
url = create_snippet(@filename, @extension, data, login_cookie)
`echo #{url} | pbcopy`
puts "Copied to pasteboard: #{url}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment