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.
Given a url, tries to open it in your browser. TODO: Linux
# File lib/gist.rb, line 166 def browse(url) if RUBY_PLATFORM =~ /darwin/ `open #{url}` elsif RUBY_PLATFORM =~ /linux/ `#{ENV['BROWSER']} #{url}` elsif ENV['OS'] == 'Windows_NT' or RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw|wince/ `start "" "#{url}"` end end
Tries to copy passed content to the clipboard.
# File lib/gist.rb, line 178 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
Parses command line arguments and does what needs to be done.
# File lib/gist.rb, line 43 def execute(*args) private_gist = defaults["private"] gist_filename = nil gist_extension = defaults["extension"] browse_enabled = defaults["browse"] description = nil 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('-d','--description DESCRIPTION', 'Set description of the new gist') do |d| description = d 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 begin opts.parse!(args) 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, description) browse(url) if browse_enabled puts copy(url) rescue => e warn e puts opts end end
Given a gist id, returns its content.
# File lib/gist.rb, line 159 def read(gist_id) data = JSON.parse(open(GIST_URL % gist_id).read) data["files"].map{|name, content| content['content'] }.join("\n\n") end
Create a gist on gist.github.com
# File lib/gist.rb, line 126 def write(files, private_gist = false, description = nil) 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.ca_file = ca_cert req = Net::HTTP::Post.new(url.path) req.body = JSON.generate(data(files, private_gist, description)) user, password = auth() if user && password req.basic_auth(user, password) end response = http.start{|h| h.request(req) } case response when Net::HTTPCreated JSON.parse(response.body)['html_url'] else puts "Creating gist failed: #{response.code} #{response.message}" exit(false) end end
Generated with the Darkfish Rdoc Generator 2.