Skip to content

RESTful pasting

Saturday, 30 April 2005  |  richard dale

I found this great little ruby program on isaac's random rants blog - it takes the contents of the klipper clipboard, sends it to rafb.net which is 'code snippets temporary storage' site. You put your clipping there, and it returns you the URL back on the clipboard that you can paste into an IRC channel or whatever. I added a 'lang=Ruby' attribute too so the snippet gets labelled as a Ruby one.

require 'Korundum'
require 'net/http'

about = KDE::AboutData.new("paster", "RAFB paster", "0.1")
KDE::CmdLineArgs.init(ARGV, about)
a = KDE::Application.new()
klipper = KDE::DCOPRef.new("klipper", "klipper")
text = klipper.getClipboardContents
text.gsub!(/[^A-Za-z0-9]/) { sprintf("%%%02X", $&[0] )}

Net::HTTP.start('rafb.net', 80) { |http|
    response = http.post('/paste/paste.php',
        "nick=#{ENV['LOGNAME']}&lang=Ruby&desc=&cvt_tabs=no&text=#{text}")
    klipper.clipboardContents = "http://rafb.net#{response['Location']}"
}

I think it shows the power of ruby's dynamism to communicate so simply over both DCOP and the net with such little code. Maybe that will prove a korundum killer feature - acting as a kind of glue between networked services; like DCOP, REST, SOAP and so on.