From: Francisco Ortiz Date: 2006-05-03T22:40:13+09:00 Subject: Fwd: [QUIZ] cat2rafb (#77) ------=_Part_1229_15181557.1146663609312 Content-Type: multipart/alternative; boundary="----=_Part_1230_8536093.1146663609312" ------=_Part_1230_8536093.1146663609312 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 4/28/06, Ruby Quiz wrote: > The three rules of Ruby Quiz: > > 1. Please do not post any solutions or spoiler discussion for this quiz > until > 48 hours have passed from the time on this message. > > 2. Support Ruby Quiz by submitting ideas as often as you can: > > http://www.rubyquiz.com/ > > 3. Enjoy! > > Suggestion: A [QUIZ] in the subject of emails about the problem helps > everyone > on Ruby Talk follow the discussion. Please reply to the original quiz > message, > if you can. > > > -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-= =3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D= -=3D-=3D-=3D > > by Sean Carley > > The goal of cat2rafb is to implement a command line utility that will > allow you > to submit arbitrary text to http://rafb.net/paste/ and get the URL for th= e > text > back. Bonus points for pushing the URL through http://rubyurl.com/ to > make it > more manageable. > > cat2rafb could work like cat, taking files as input or text until ^D afte= r > entering the command. Here's my solution, It's not the best, the smallest, the fastest or the anything-est, but I=B4ve 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=3D{"lang"=3D>"Ruby","nick"=3D>"Myself", "desc"=3D>"test","cvt_tabs"=3D>"No","text"=3D>"fetch","submit"=3D>"Paste" }= , uri=3D'http://rafb.net/paste/paste.php', base_uri=3D'http://rafb.net' ) @uri=3Duri @base_uri=3Dbase_uri @params=3Dparams @answer=3Dfetch_post_rafbe end def fetch_post_rafbe response=3DNet::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=3D" http://rubyurl.com/rubyurl/remote?website_url=3D#{@answer} " response=3DNet::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__ =3D=3D $0 require 'optparse' options=3D{'lang'=3D>"Ruby",'nick'=3D>"myself",'desc'=3D>"", 'text'=3D>"", 'file'=3D>nil, 'r'=3D>nil, "cvt_tabs"=3D>"No","submit"=3D>"Paste" } opts=3DOptionParser.new do |opts| opts.banner=3D "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 =3D %w{C89 C C++ C# Java Pascal Perl PHP PL/I Python Ruby Schem= e 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']=3Dlang end opts.on("-n","--nick NAME", "NAME to post under."," Default:myself") do |nick| options['nick']=3Dnick end opts.on("-d","--desc DESCRIPTION", "DESCRIPTION of snippet."," Default:blank") do |desc| options['desc']=3Ddesc end opts.on("-f","--file FILE", "FILE containing snippet."," Default: enter from console") d= o |file| options['file']=3Dfile end opts.on("-r","--rubyurl", "Send to rubyurl.com for url shortening") do |r| options['r']=3Dtrue 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']=3DFile.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 ne= w line" options['text']=3D$stdin.read end x=3DNoPaste.new(options) puts "Stored at: #{x.answer}" puts x.rubyurl if options['r'] end ------=_Part_1230_8536093.1146663609312 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
On 4/28/06, Ruby Quiz < james@grayproductions.net> wrote:
The three rules of Ruby Quiz:

1.  Please do not post any s= olutions or spoiler discussion for this quiz until
48 hours have passed = from the time on this message.

2.  Support Ruby Quiz by su= bmitting ideas as often as you can:

http://www.rubyquiz.com/
3.  Enjoy!

Suggestion:  A [QUIZ] in the su= bject of emails about the problem helps everyone
on Ruby Talk follow the discussion.  Please reply to the orig= inal quiz message,
if you can.

-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D= -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-= =3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D

by Sean Carley

The goal o= f cat2rafb is to implement a command line utility that will allow you
to= submit arbitrary text to=20 http://rafb.net/paste/ and get the U= RL for the text
back.  Bonus points for pushing the URL throug= h http://rubyurl.com/ to make it
more manageable.

cat2rafb could work like cat, taking files as input or text until ^= D after
entering the command.

Here's my solution, It's not the best, the smallest,= the fastest or the anything-est, but I=B4ve 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=3D{"lang"=3D>"Ruby","n= ick"=3D>"Myself", "desc"=3D>"test"= ,"cvt_tabs"=3D>"No","text"=3D>"fet= ch","submit"=3D>"Paste" },
            = ;          uri=3D'http://rafb.net/paste/paste.php',
&n= bsp;            = ;         base_uri=3D' http://rafb.net'         &= nbsp;        
   = ;            &n= bsp;    )
    @uri=3Duri
    @base_uri=3Dbase_ur= i
    @params=3Dparams
    @answer=3Dfe= tch_post_rafbe
  end
 
  def fetch_post_rafbe
&= nbsp;   response=3DNet::HTTP.post_form(URI.parse(@uri),@params)    begin
      raise "So= mething went wrong while posting" unless response['location']
    rescue
      puts $!,&qu= ot;\n"
      exit
    en= d
    @base_uri+response['location']
  end
&nb= sp;
  def rubyurl
    puts "Compressing at = rubyurl"
    uri=3D" http://rubyurl.com/rubyurl/remote?website_url=3D#{@answer}"
&nb= sp;   response=3DNet::HTTP.get_response(URI.parse(uri))
 =    begin
      raise "Something = went wrong while posting" unless response['location']
    rescue
      puts $!,&qu= ot;\n"
      exit
    en= d
    response['location'].gsub("rubyurl/show/"= ,"")

&n= bsp; end
end

if __FILE__ =3D=3D $0
  require 'optparse'  options=3D{'lang'=3D>"Ruby",'nick'=3D>"myself= ",'desc'=3D>"",
            = ;   'text'=3D>"", 'file'=3D>nil, 'r'=3D>nil, <= br>            =    "cvt_tabs"=3D>"No","submit"= =3D>"Paste"  }
  opts=3DOptionParser.new do |opts= |
   =20 opts.banner=3D "Posts a code snippet to rafb.net and = returns the address\n"\
       &= nbsp;           &nbs= p;  "so you can show it to your friends on IRC\n\n"\
            = ;          "Usage: postto= rafb [OPTIONS]"
    opts.separator ""
    op= ts.separator "Options:"
    opts.on("-l&qu= ot;,"--lang LANGUAGE",
      &nb= sp;       "LANGUAGE category.",&quo= t;  Default:Ruby") do |lang|
      LANGS =3D %w{C89 C C++ C# Java Pascal Pe= rl PHP PL/I Python Ruby Scheme SQL VB Plain\ Text}
   &nb= sp;  begin
        raise "I= nvalid Argument. Language must be one of:\n#{LANGS.join(" ")}&quo= t; unless=20 LANGS.include?(lang)
      rescue
 &nbs= p;      puts $!,"\n"
  &nbs= p;     exit
      end
&n= bsp;     options['lang']=3Dlang
    e= nd
    opts.on("-n","--nick NAME",            &n= bsp; "NAME to post under.","  Default:myself") do = |nick|
      options['nick']=3Dnick
  &n= bsp; end
    opts.on("-d","--desc DESCRIPT= ION",
          &= nbsp;   "DESCRIPTION of snippet.","  Default:= blank") do |desc|
      options['desc']=3D= desc
    end
    opts.on("-f",&qu= ot;--file FILE",
        &n= bsp;     "FILE containing snippet.","&nb= sp; Default: enter from console") do |file|
    = ;  options['file']=3Dfile
    end
  &nb= sp;=20 opts.on("-r","--rubyurl", "Send to rubyurl.com for url shortening") do |r|
 &= nbsp;    options['r']=3Dtrue
    end
    opts.on_tail("-v"= ;, "--version", "Version") do
      puts "0.0.1"
  &n= bsp;   exit
    end
    opts.= on_tail("-h", "--help", "Shows this message")= do
      puts opts
    =   exit
    end
  end

  begin
   =20 opts.parse!
  rescue
    puts $!,"\n"<= br>    puts opts
    exit
  end&nb= sp;

  if options['file'] then
    begin
&= nbsp;     options['text']=3DFile.open(options['file'],&= quot;r").read
      puts "Pasting sni= ppet from file #{options['file']}"
    rescue
      puts $!,&qu= ot;\n"
      exit
    en= d
  else
    puts "Reading snippet from cons= ole, finish by entering <CTRL-Z> in a new line"
  &= nbsp; options['text']=3D$stdin.read
  end

  x=3DNoPaste.new(options)
  puts "Stor= ed at: #{x.answer}"
  puts x.rubyurl if options['r']
end<= br>



------=_Part_1230_8536093.1146663609312-- ------=_Part_1229_15181557.1146663609312 Content-Type: application/octet-stream; name=77.rb Content-Transfer-Encoding: 7bit X-Attachment-Id: f_emqu7g67 Content-Disposition: attachment; filename="77.rb" #!/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}" 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 ------=_Part_1229_15181557.1146663609312--