Fork Of

Revisions

gist: 2936 Download_button fork
public
Description:
gistie.rb Paste code to gist.github.com from the shell!
Public Clone URL: git://gist.github.com/2936.git
Embed All Files: show embed
gist #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env ruby
# GIST
# Paste to Gist.GitHub.com from the Shell
#
# Forked by Thomas Aylott / subtleGradient
# at 4096a998b6c6a0043e1ce158d43639b5db78a61c
#
# Original "gistie" by Pieter de Bie <frimmirf@gmail.com>
# Based on a "Pastie" task by someone
#
 
require "tempfile"
 
class Gist
  GIST_URL = 'http://gist.github.com/gists'
  GIST_LOGIN_URL = 'https://gist.github.com/session'
 
  def login_cookie
    begin
      username = File.read(File.expand_path('~/.gist_username')).chomp
      password = File.read(File.expand_path('~/.gist_pass')).chomp
    rescue => e
      return nil
    end
    
    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 initialize(filename, extension, data, login=false)
    cookie = login_cookie if login
    
    text_file = Tempfile.open('w+')
    text_file << data
    text_file.flush
 
    cmd = "curl #{GIST_URL} \\\n"
    cmd += "-b '#{cookie}' \\\n" if cookie
    cmd += <<-EOS
-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)
  end
  
  def to_s
    @url
  end
  
end
 
if __FILE__ == $0
  
  login = !ARGV.delete('-a')
  
  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 == ""
 
  puts Gist.new(@filename, @extension, data, login)
  
end
 
install.sh #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ln -s "${PWD}/gist" ~/bin/gist
chmod +x ~/bin/gist
 
touch ~/.gist_username
chmod 600 ~/.gist_username
 
touch ~/.gist_pass
chmod 600 ~/.gist_pass
 
echo "Put your login and password for GitHub into '~/.gist_username' and '~/.gist_pass'"
 
$EDITOR ~/.gist_username
$EDITOR ~/.gist_pass
 
Paste to Gist_GitHub Anonymously.tmCommand #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>beforeRunningCommand</key>
<string>nop</string>
<key>bundleUUID</key>
<string>2E6B9F06-DDB6-4707-A2BB-8751CF1722A0</string>
<key>command</key>
<string>cat|gist -a "$TM_FILENAME"
</string>
<key>input</key>
<string>selection</string>
<key>keyEquivalent</key>
<string>^@V</string>
<key>name</key>
<string>Paste to Gist.GitHub Anonymously</string>
<key>output</key>
<string>showAsHTML</string>
<key>uuid</key>
<string>CF0D0F28-0F67-4348-B68E-6A677EEA7C97</string>
</dict>
</plist>
 
Paste to Gist_GitHub.tmCommand #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>beforeRunningCommand</key>
<string>nop</string>
<key>bundleUUID</key>
<string>2E6B9F06-DDB6-4707-A2BB-8751CF1722A0</string>
<key>command</key>
<string>cat|gist "$TM_FILENAME"
</string>
<key>input</key>
<string>selection</string>
<key>keyEquivalent</key>
<string>^@V</string>
<key>name</key>
<string>Paste to Gist.GitHub</string>
<key>output</key>
<string>showAsHTML</string>
<key>uuid</key>
<string>6BD4B983-AF2F-4380-AEB1-EE0B431D977E</string>
</dict>
</plist>