Created
February 22, 2010 16:26
-
-
Save alexknowshtml/311223 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Dropzone Destination Info | |
# Name: GitHub Gist | |
# Description: Drag text to share it instantly via GitHub Gist. Hold down option to make a public Gist. Click to copy and goto the last URL. | |
# Handles: NSStringPboardType | |
# Events: Clicked, Dragged | |
# KeyModifiers: Option | |
# Creator: Edgar Suarez | |
# URL: http://e.dgar.org | |
# IconURL: http://aptonic.com/destinations/icons/gist.png | |
# OptionsNIB: Login | |
# LoginTitle: Your GitHub username and API token | |
require 'open-uri' | |
require 'net/http' | |
require 'yaml' | |
class Gist | |
@@gist_url = 'http://gist.github.com/%s' | |
@@last_url_filename = File.expand_path("~/.dz_last_gist") | |
class << self | |
def post(content, private_gist) | |
url = URI.parse('http://gist.github.com/api/v1/yaml/new') | |
request = Net::HTTP.post_form(url, build_params(prompt_filename, content, private_gist)) | |
if request.code.to_i == 200 | |
response = YAML.load(request.body) | |
persist(@@gist_url % response['gists'][0][:repo]) | |
else | |
raise "Error: #{request.code}" | |
end | |
end | |
def get_last_url | |
if File.readable?(@@last_url_filename) | |
File.open(@@last_url_filename).read.chomp | |
else | |
$dz.finish("No gists yet") | |
$dz.url(false) | |
Process.exit! | |
end | |
end | |
private | |
def prompt_filename | |
output = `./CocoaDialog standard-inputbox --title "Gist name" --e --informative-text "Enter gist name:"` | |
button, filename = output.split("\n") | |
if button == "2" | |
$dz.finish("Cancelled") | |
$dz.url(false) | |
Process.exit! | |
end | |
if filename == nil | |
$dz.finish("Empty name") | |
$dz.url(false) | |
Process.exit! | |
end | |
return filename | |
end | |
def persist(url) | |
File.open(@@last_url_filename, 'w') do |f| | |
f.puts url | |
end | |
url | |
end | |
def build_params(filename, content, private_gist) | |
{ | |
"files[#{filename}]" => content, | |
:login => ENV['USERNAME'], | |
:token => ENV['PASSWORD'], | |
}.merge(private_gist ? { 'private' => 'true' } : {}) | |
end | |
end | |
end | |
# DZ events | |
def dragged | |
$dz.determinate(false) | |
$dz.begin("Creating gist...") | |
begin | |
url = Gist.post($items[0], ENV['KEY_MODIFIERS'] == "") | |
$dz.finish("URL is now on clipboard") | |
$dz.url(url) | |
rescue Exception => e | |
$dz.finish("Error uploading gist!") | |
$dz.url(false) | |
exit | |
end | |
end | |
def clicked | |
url = Gist.get_last_url | |
$dz.finish("URL is now on clipboard") | |
$dz.url(url) | |
system("open #{url}") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment