# Fwd: cat2rafb (#77)

**URL:** <https://www.ruby-forum.com/t/fwd-cat2rafb-77/94431>\
**Category:** Ruby\
**Created:** [May 3, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/fwd-cat2rafb-77/94431 "2006-05-03T19:07:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fjom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/91b2a8/32.png) [@fjom](https://www.ruby-forum.com/u/fjom)\
**Post date:** [May 3, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/fwd-cat2rafb-77/94431/1 "2006-05-03T19:07:16Z")

</div>

On 4/28/06, Ruby Q. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. Enjoy!  
> by Sean C.  
> entering the command.  
> Here’s my solution, It’s not the best, the smallest, the fastest or the  
> anything-est, but IÂ´ve learned a lot from looking at other people’s  
> solutions for ealier Quizes and this might help someone else in time.

Francisco

#!/usr/bin/ruby  
require ‘net/http’  
require ‘uri’

class NoPaste  
attr\_reader :answer

def initialize( params={“lang”=\>“Ruby”,“nick”=\>“Myself”,  
“desc”=\>“test”,“cvt\_tabs”=\>“No”,“text”=\>“fetch”,“submit”=\>“Paste” },  
uri=‘[http://rafb.net/paste/paste.php](http://rafb.net/paste/paste.php)’,  
base\_uri=‘[http://rafb.net](http://rafb.net)’  
)  
@uri=uri  
@base\_uri=base\_uri  
@params=params  
@answer=fetch\_post\_rafbe  
end

def fetch\_post\_rafbe  
response=Net::HTTP.post\_form(URI.parse(@uri),@params)  
begin  
raise “Something went wrong while posting” unless  
response[‘location’]

```
rescue
  puts $!,"\n"
  exit
end
@base_uri+response['location']

```

end

def rubyurl  
puts “Compressing at rubyurl”  
uri="  
[http://rubyurl.com/rubyurl/remote?website\_url=#{@answer}](http://rubyurl.com/rubyurl/remote?website_url=#%7B@answer%7D)[http://rubyurl.com/rubyurl/remote?website\_url=#{@answer}](http://rubyurl.com/rubyurl/remote?website_url=#%7B@answer%7D)  
"  
response=Net::HTTP.get\_response(URI.parse(uri))  
begin  
raise “Something went wrong while posting” unless  
response[‘location’]

```
rescue
  puts $!,"\n"
  exit
end
response['location'].gsub("rubyurl/show/","")

```

end  
end

if **FILE** == $0  
require ‘optparse’  
options={‘lang’=\>“Ruby”,‘nick’=\>“myself”,‘desc’=\>“”,  
‘text’=\>“”, ‘file’=\>nil, ‘r’=\>nil,  
“cvt\_tabs”=\>“No”,“submit”=\>“Paste” }  
opts=OptionParser.new do |opts|  
opts.banner= “Posts a code snippet to [rafb.net](http://rafb.net) and returns the  
address\n”  
“so you can show it to your friends on IRC\n\n”  
“Usage: posttorafb [OPTIONS]”  
opts.separator “”  
opts.separator “Options:”  
opts.on(“-l”,“–lang LANGUAGE”,  
“LANGUAGE category.”," Default:Ruby") do |lang|  
LANGS = %w{C89 C C++ C# Java Pascal Perl PHP PL/I Python Ruby  
Scheme  
SQL VB Plain\ Text}  
begin  
raise “Invalid Argument. Language must be one  
of:\n#{LANGS.join(”  
“)}” unless LANGS.include?(lang)  
rescue  
puts $!,“\n”  
exit  
end  
options[‘lang’]=lang  
end  
opts.on(“-n”,“–nick NAME”,  
“NAME to post under.”," Default:myself") do |nick|  
options[‘nick’]=nick  
end  
opts.on(“-d”,“–desc DESCRIPTION”,  
“DESCRIPTION of snippet.”," Default:blank") do |desc|  
options[‘desc’]=desc  
end  
opts.on(“-f”,“–file FILE”,  
“FILE containing snippet.”," Default: enter from  
console") do  
|file|  
options[‘file’]=file  
end  
opts.on(“-r”,“–rubyurl”, “Send to [rubyurl.com](http://rubyurl.com) for url shortening”)  
do  
|r|  
options[‘r’]=true  
end  
opts.on\_tail(“-v”, “–version”, “Version”) do  
puts “0.0.1”  
exit  
end  
opts.on\_tail(“-h”, “–help”, “Shows this message”) do  
puts opts  
exit  
end  
end

begin  
opts.parse!  
rescue  
puts $!,“\n”  
puts opts  
exit  
end

if options[‘file’] then  
begin  
options[‘text’]=File.open(options[‘file’],“r”).read  
puts “Pasting snippet from file #{options[‘file’]}”  
rescue  
puts $!,“\n”  
exit  
end  
else  
puts “Reading snippet from console, finish by entering in a  
new  
line”  
options[‘text’]=$stdin.read  
end

x=NoPaste.new(options)  
puts “Stored at: #{x.answer}”  
puts x.rubyurl if options[‘r’]  
end

---

<div class="post-metadata">

**Author:** ![fjom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/91b2a8/32.png) [@fjom](https://www.ruby-forum.com/u/fjom)\
**Post date:** [May 3, 2006, 9:36pm UTC](https://www.ruby-forum.com/t/fwd-cat2rafb-77/94431/2 "2006-05-03T21:36:07Z")

</div>

My solution is at [http://rubyurl.com//iFW](http://rubyurl.com//iFW). Sean, Craig Buchek and I  
hacked on this last night at the weekly StL.rb hacking night as our  
topic. As you can see Sean took a Ruby Golf approach, while I took the  
exact opposite approach. I tried to make mine fairly feature rich w/o  
a lot of extra baggage.

It can do the following:  
Take long or short option names  
-h, --help prints the usage statement via RDoc::usage (reads the  
comments at the head of the file : paste.rb)  
-t allows putting text or code on the command line (I.e. --text  
‘puts “hello world”’ )  
Reads options from a YAML config file: ~/.paste.yml. This can be  
expanded to work with nopaste or [rpaste.com](http://rpaste.com), each having different  
form parameters.  
Defaults to Ruby for the lang.

Currently, there is no option for tab expansion, but you can put it in  
the .paste.yml file. Seriously needs a lot of refactoring, esp around  
the response handling and the initial options gathering.

I found it interesting the different ways of handling errors from the  
HTTPResponse in the other quiz answers. Sean wanted to take the best  
of these and maybe create a RubyForge project.

Lots of fun!

Ed
