Module: Gist

Extended by:
Gist
Included in:
Gist
Defined in:
lib/gist.rb,
lib/gist/manpage.rb,
lib/gist/version.rb,
lib/gist/standalone.rb

Overview

You can use this class from other scripts with the greatest of ease.

>> Gist.read(gist_id)
Returns the body of gist_id as a string.

>> Gist.write(content)
Creates a gist from the string `content`. Returns the URL of the
new gist.

>> Gist.copy(string)
Copies string to the clipboard.

>> Gist.browse(url)
Opens URL in your default browser.

Defined Under Namespace

Modules: Manpage, Standalone

Constant Summary collapse

GIST_URL =
'https://gist.github.com/%s.txt'
CREATE_URL =
'https://gist.github.com/gists'
PROXY =
ENV['HTTP_PROXY'] ? URI(ENV['HTTP_PROXY']) : nil
PROXY_HOST =
PROXY ? PROXY.host : nil
PROXY_PORT =
PROXY ? PROXY.port : nil
VERSION =
Version = '2.0.2'

Instance Method Summary collapse

Instance Method Details

#browse(url) ⇒ Object

Given a url, tries to open it in your browser. TODO: Linux



138
139
140
141
142
143
144
145
# File 'lib/gist.rb', line 138

def browse(url)
  if RUBY_PLATFORM =~ /darwin/
    `open #{url}`
  elsif ENV['OS'] == 'Windows_NT' or
    RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw|wince/i
    `start "" "#{url}"`
  end
end

#copy(content) ⇒ Object

Tries to copy passed content to the clipboard.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/gist.rb', line 148

def copy(content)
  cmd = case true
  when system("type pbcopy > /dev/null 2>&1")
    :pbcopy
  when system("type xclip > /dev/null 2>&1")
    :xclip
  when system("type putclip > /dev/null 2>&1")
    :putclip
  end

  if cmd
    IO.popen(cmd.to_s, 'r+') { |clip| clip.print content }
  end

  content
end

#execute(*args) ⇒ Object

Parses command line arguments and does what needs to be done.



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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/gist.rb', line 34

def execute(*args)
  private_gist = defaults["private"]
  gist_filename = nil
  gist_extension = defaults["extension"]
  browse_enabled = defaults["browse"]

  opts = OptionParser.new do |opts|
    opts.banner = "Usage: gist [options] [filename or stdin] [filename] ...\n" +
      "Filename '-' forces gist to read from stdin."

    opts.on('-p', '--[no-]private', 'Make the gist private') do |priv|
      private_gist = priv
    end

    t_desc = 'Set syntax highlighting of the Gist by file extension'
    opts.on('-t', '--type [EXTENSION]', t_desc) do |extension|
      gist_extension = '.' + extension
    end

    opts.on('-o','--[no-]open', 'Open gist in browser') do |o|
      browse_enabled = o
    end

    opts.on('-m', '--man', 'Print manual') do
      Gist::Manpage.display("gist")
    end

    opts.on('-v', '--version', 'Print version') do
      puts Gist::Version
      exit
    end

    opts.on('-h', '--help', 'Display this screen') do
      puts opts
      exit
    end
  end

  opts.parse!(args)

  begin
    if $stdin.tty? && args[0] != '-'
      # Run without stdin.

      if args.empty?
        # No args, print help.
        puts opts
        exit
      end

      files = args.inject([]) do |files, file|
        # Check if arg is a file. If so, grab the content.
        abort "Can't find #{file}" unless File.exists?(file)

        files.push({
          :input     => File.read(file),
          :filename  => file,
          :extension => (File.extname(file) if file.include?('.'))
        })
      end

    else
      # Read from standard input.
      input = $stdin.read
      files = [{:input => input, :extension => gist_extension}]
    end

    url = write(files, private_gist)
    browse(url) if browse_enabled
    puts copy(url)
  rescue => e
    warn e
    puts opts
  end
end

#read(gist_id) ⇒ Object

Given a gist id, returns its content.



132
133
134
# File 'lib/gist.rb', line 132

def read(gist_id)
  open(GIST_URL % gist_id).read
end

#write(files, private_gist = false) ⇒ Object

Create a gist on gist.github.com



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/gist.rb', line 111

def write(files, private_gist = false)
  url = URI.parse(CREATE_URL)

  if PROXY_HOST
    proxy = Net::HTTP::Proxy(PROXY_HOST, PROXY_PORT)
    http  = proxy.new(url.host, url.port)
  else
    http = Net::HTTP.new(url.host, url.port)
  end

  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  http.cert = OpenSSL::X509::Certificate.new(ca_cert)

  req = Net::HTTP::Post.new(url.path)
  req.form_data = data(files, private_gist)

  http.start{|h| h.request(req) }['Location']
end