# Cat2rafb (#77)

**URL:** <https://www.ruby-forum.com/t/cat2rafb-77/94387>\
**Category:** Ruby\
**Created:** [May 3, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/cat2rafb-77/94387 "2006-05-03T18:56:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![aaronpowell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7bcc69/32.png) [@aaronpowell](https://www.ruby-forum.com/u/aaronpowell)\
**Post date:** [May 3, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/cat2rafb-77/94387/1 "2006-05-03T18:56:33Z")

</div>

Here’s my solution, also available at [http://rubyurl.com/XxX](http://rubyurl.com/XxX)

–Aaron

# Solution to [QUIZ] cat2rafb (#77)

# By Aaron P.

require ‘rubygems’  
require ‘mechanize’  
require ‘getoptlong’

PASTE\_URL = ‘[http://rafb.net/paste/](http://rafb.net/paste/)’  
RUBY\_URL = ‘[http://rubyurl.com/](http://rubyurl.com/)’

# Get options

parser = GetoptLong.new  
parser.set\_options( [‘–lang’, ‘-l’, GetoptLong::OPTIONAL\_ARGUMENT],  
[‘–nick’, ‘-n’, GetoptLong::OPTIONAL\_ARGUMENT],  
[‘–desc’, ‘-d’, GetoptLong::OPTIONAL\_ARGUMENT],  
[‘–cvt\_tabs’, ‘-t’, GetoptLong::OPTIONAL\_ARGUMENT]  
)  
opt\_hash = {}  
parser.each\_option { |name, arg| opt\_hash[name.sub(/^–/, ‘’)] = arg }

# Get the text to be uploaded

buffer = String.new  
if ARGV.length \> 0  
ARGV.each { |f| File.open(f, “r”) { |file| buffer \<\< file.read } }  
else  
buffer = $stdin.read  
end

agent = WWW::Mechanize.new

# Get the Paste() page

page = agent.get(PASTE\_URL)  
form = page.forms.first  
form.fields.name(‘text’).first.value = buffer

# Set all the options

opt\_hash.each { |k,v| form.fields.name(k).first.value = v }

# Submit the form

page = agent.submit(form)  
text\_url = page.uri.to\_s

# Submit the link to RUBY URL

page = agent.get(RUBY\_URL)  
form = page.forms.first  
form.fields.name(‘rubyurl[website\_url]’).first.value = text\_url  
page = agent.submit(form)  
puts page.links.find { |l| l.text == l.href }.href
