Fwd: cat2rafb (#77)

On 4/28/06, Ruby Q. [email protected] 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’,
base_uri=‘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=#{@answer}
"
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 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 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

My solution is at 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, 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