I’m back with some Code …

Posted: July 6th, 2009 | Author: FreedomCoder | Filed under: Open Source, Programming, github, how-to | Tags: , , , | No Comments »

Well, hello again, long time since the last post. I went on vacations, work a lot and did some programming. Let’s talk abount the programming part, since it is the most interesting one. ;)

I created a small library called “Esearchy” capable of searching the internet for email addresses. Currently, we the supported search methods are engines such as Google, Bing, Yahoo, PGP servers, GoogleGroups, etc , but I intend to add many more.
Also, the library searches inside .pdf and .txt files for emails addresses and adds them to the list of found accounts.

For now, there are two main ways of performing a search, “the ruby way”

  1. Esearchy.create "domain.com" do |domain|
  2.    domain.maxhits = 500
  3.    domain.search
  4.    domain.clean {|e| e =~ /<|>/ }
  5.    domain.save_to_file "~/emails.txt"
  6.  end

and the more classic way in which users can create an Esearchy objetc and work on it

  1.  domain = Esearchy.new :query => "domain.com", :maxhits => 500
  2.   domain.search
  3.   domain.save_to_file "~/emails.txt"

For now , that’s it for now , but keep on tuned for more shitty code ajjajaa

(Via 自由編碼人.) Original Link: I’m back with some Code …


Patch en Regexp para poder usarlas como clave en un Hash

Posted: July 6th, 2009 | Author: FreedomCoder | Filed under: Open Source, Programming, how-to | Tags: , , , | No Comments »

Siguiendo mi proyecto de hacer mi wiki en Ruby, encontré un comportamiento muy raro.
Generé un hash (que se llama @rules) que no tiene un elemento que tiene. O sea, @rules[@rules.keys[2]] da nil, pero @rules.values[2] devuelve el objeto asociado a la clave @rules.keys[2]. Como este hash tiene como claves un montón de expresiones regulares, me imaginé que había un problema con el hash y el eql? de Regexp, así que los implementé de nuevo y monkeypatchié.
Este es el código:

class Regexp
 alias_method  :o ld_rapidito_inspect, :inspect

 def inspect
   @inspect = old_rapidito_inspect if @inspect.nil?
   @inspect
 end

 def eql?( other )
   false if other.class != Regexp
   self.inspect == other.inspect
 end

 alias_method :"==", :eql?

 def hash
   self.inspect.hash
 end
end

Esta corrección me anduvo con la siguiente versión de ruby:

$ ruby --version
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]

Espero que les sirva.
Happy hacking,
Aureliano.

(Via aurelianito.) Original Link: Patch en Regexp para poder usarlas como clave en un Hash


Más rapidito

Posted: July 1st, 2009 | Author: FreedomCoder | Filed under: Open Source, Programming, how-to | Tags: , , , | No Comments »

Como les estuve contando, sigo escribiendo mi wiki. Ya parsea un subconjunto interesante del lenguaje definido por trac.
Siguiendo la tradición, les cuento como está avanzando el tokenizer. Al tokenizer lo simplifiqué para que devuelva la expresión regular que matcheo junto con el match (en vez del “tipo”). Esto hizo que la interfase para definir las reglas para tokenizar sea más simple. Si no hay ninguna regla que matchee sigue devolviendo ["string", :text].
Sin más, acá el código:

module Rapidito
  class Tokenizer

    def initialize( *delimiters )
      @delimiter_list = delimiters +  [/\z/]
      @match_cache = nil
    end

    def source
      valid_cache? ? @match_cache[0].to_s + @source : @source
    end

    def source=(s)
      @match_cache = nil
      @source = s
    end

    def has_next?
      !@source.empty? || valid_cache?
    end

    def valid_cache?
      (!@match_cache.nil?) && (@match_cache[0].to_s.length > 0)
    end

    def next_match
      @delimiter_list.map {|regex| [regex.match(@source),regex]}.reject {|p| p[0].nil?}.inject do
        |better,new|
        better_pos = better[0].pre_match.length
        new_pos = new[0].pre_match.length

        if better_pos < new_pos
          better
        elsif new_pos < better_pos
          new
        elsif better[0].to_s.length > new[0].to_s.length
          better
        else
          new
        end
      end
    end

    def next_token
      if @match_cache #cached delimiter
        rv = @match_cache
        @match_cache = nil
        return rv
      end

      match = next_match
      p = match[0].pre_match.length
      @source = @source[p + match[0].to_s.length, @source.length]

      if p == 0 #delimiter
        match
      else #text
        @match_cache = match
        [match[0].pre_match, :text]
      end
    end

    def all_tokens
      tokens = []
      while has_next?
        tokens << next_token
      end
      tokens
    end
  end
end

Y si miran los tests de unidad, van a ver que también quedaron más lindos:

require 'test/unit'
require 'rapidito/tokenizer'

include Rapidito

class TokenizerTest < Test::Unit::TestCase

  def test_no_token
    tok = Tokenizer.new
    tok.source = "aaaa"
    assert_equal true, tok.has_next?
    assert_equal ["aaaa", :text], tok.next_token
    assert_equal false, tok.has_next?
  end

  def assert_all_tokens( expected, tokenizer )
    assert_equal expected,
      tokenizer.all_tokens.map { |token, kind| [token.to_s, kind] }
  end

  def test_two_delimiters
    tok = Tokenizer.new(
      /\|/, /;;/
    )

    tok.source = "aa|bbb;;;;cccc"
    assert_all_tokens \
      [ ["aa", :text], ["|", /\|/], ["bbb", :text],
        [";;", /;;/], [";;", /;;/], ["cccc", :text] ],
      tok

    tok.source = "aa;;bbb||cccc"
    assert_all_tokens \
      [ ["aa", :text], [";;", /;;/], ["bbb", :text],
        ["|", /\|/], ["|", /\|/], ["cccc", :text] ],
      tok
  end

  def test_choose_longest_match
    tok = Tokenizer.new(
      /aa/, /aaa/
    )
    tok.source = "aaaa"
    assert_all_tokens [ ["aaa", /aaa/], ["a", :text ] ], tok
  end

  def test_reset_precache
    tok = Tokenizer.new(
      /\|/, /,/
    )
    tok.source = "original start|original end"
    tok.next_token
    tok.source = "new start,new end"
    assert_equal ["new start", :text], tok.next_token
  end

  def test_almost_finished
    tok = Tokenizer.new( /!/ )
    tok.source = "bang!"
    tok.next_token
    assert_equal true, tok.has_next?
    tok.next_token
    assert_equal false, tok.has_next?
  end

  def test_carriage_return_ending
    tok = Tokenizer.new( /!/ )
    tok.source = "bang!\n"
    tok.next_token
    assert_equal true, tok.has_next?
    tok.next_token
    assert_equal true, tok.has_next?
    assert_equal "\n", tok.next_token[0].to_s
    assert_equal false, tok.has_next?
  end

  def test_transparent_caching
    tok = Tokenizer.new( /!/ )
    tok.source = "bang!pum"
    tok.next_token

    assert_equal "!pum", tok.source
  end

  def test_match_klass
    tok = Tokenizer.new( /!/ )
    tok.source = "!bang!pum"

    assert_equal \
      [MatchData, String, MatchData, String],
      tok.all_tokens.map { |tok, kind| tok.class }
  end
end

Happy hacking,
Aureliano.

(Via aurelianito.) Original Link: Más rapidito


Community Highlights: Ruby Heroes

Posted: June 13th, 2009 | Author: FreedomCoder | Filed under: Open Source, Programming | Tags: , , , | 1 Comment »

This week I’m happy to tell you about a new set of articles which will be appearing here on the Rails blog called “Community Highlights”. This new series will feature people/projects/sites from the Rails community that may deserve a little extra recognition.

This week, we’re going to start with a few people who received awards on stage at Railsconf 2009, this years Ruby Heroes.

Brian Helmkamp

Brian has been a contributing member of the Ruby community for 4 years now, but is most well known for his testing library Webrat. He’s a contributer to Rails, RSpec, Rubinius, and is a co-author on the recent RSpec Book. More recently he’s been helping out the Rails core team with Rack:Test, and Rack:Debug.

His Blog: http://www.brynary.com/
Twitter: brynary

Aman Gupta

Aman has taken over the maintenance, new features, and the recent releases of EventMachine, which is an invaluable tool for writing fast ruby applications. He’s also the author behind amqp & xmpp4em gems which are deployed far and wide.

Github: http://github.com/tmm1
Twitter: tmm1

Luis Lavena

Luis has done a lot for the Ruby community in Argentina, but he’s most well known in our community for the work he’s done for windows users maintaining the One-Click Ruby Installer. Recently he’s put up a Plegie to help get the windows installer a new home.

His Blog: http://blog.mmediasys.com/
Twitter: luislavena

Pat Allan

Pat is the mastermind behind Thinking Sphinx which has become a standard when it comes to full-text search in Rails. He’s also one of the guys that has helped create the phenomenon known as Railscamp, where I hear he makes some killer pancakes.

His Blog: http://freelancing-gods.com/
Twitter: Pat

Dan Kubb

Dan been tirelessly working on one of the hardest Ruby projects around, DataMapper. He became the official maintainer after Sam Smoot and since then has completely rewritten the test suite to give DataMapper better coverage, has come up with a viable path to completion, and is currently working on making sure DataMapper works great with Rails 3.

Github: http://github.com/dkubb
Twitter: dkubb

John Nunemaker

Although John Nunemaker has released several widely used open source libraries, like HTTParty and HappyMapper, his main contribution in my opinion comes from his blog Rails Tips. Over the past year he’s written an incredible number educational blog posts on many Ruby and Rails topics.

RailsTips: http://railstips.org/
Twitter: jnunemaker

Those are your six Ruby Hero’s for 2009. If you’re interested you can also watch a video of the award ceremony which talks more about the methodology about how they were chosen and see 5 of these guys receive their awards on stage at Railsconf 2009.

(Via Riding Rails.) Original Link: Community Highlights: Ruby Heroes


Pequeñas delicias de las expresiones regulares

Posted: June 13th, 2009 | Author: FreedomCoder | Filed under: Open Source, Programming, how-to | Tags: , , , | No Comments »

Como les conté acáacá, estoy escribiendo un tokenizador para un wiki que estoy programando. Y hoy me encontré con una cosa muy extraña de las expresiones regulares.
En ruby la función match sirve para buscar el primer match de una regex dentro de un string. Por ejemplo (usando el irb):

irb(main):001:0> m = /a/.match "babab"
=> #<MatchData "a">
irb(main):002:0> m.pre_match
=> "b"
irb(main):003:0> m[0]
=> "a"

En particular, el pre_match es lo que está antes del match en el string. También según había entendido (mal) /\Z/ matchea con el final del string. Por ejemplo:

irb(main):004:0> m = /\Z/.match "hola"
=> #<MatchData "">
irb(main):005:0> m.pre_match
=> "hola"
Pero, /\Z/ tiene un comportamiento muy extraño, aunque documentado, cuando el último caracter antes del final es un \n. Lo que pasa es que el pre_match queda ¡sin el\n del final!. Lo muestro en el irb:
irb(main):006:0> m = /\Z/.match "\n"
=> #<MatchData "">
irb(main):007:0> m.pre_match
=> ""

Para que no se manduque el \n, hay que usar /\z/ (¡en minúscula!):

irb(main):008:0> m = /\z/.match "\n"
=> #<MatchData "">
irb(main):009:0> m.pre_match
=> "\n"

Por lo tanto tuve que tocar el tokenizer, ahora la función de initialize quedó así (miren el cambio de la "Z" a "z"):

    def initialize( delimiters )
      @delimiter_list = [[/\z/, :finish]] +
        delimiters.to_a.map { |k,arr| arr.map { |re| [re, k] } }.inject([]) { |ac,ps| ac + ps }
      @match_cache = nil
    end

Y el test que captura el problema que genera usar \Z en vez de \z quedó así:

  def test_carriage_return_ending
    tok = Tokenizer.new( :a_kind => [/!/] )
    tok.source = "bang!\n"
    tok.next_token
    assert_equal true, tok.has_next?
    tok.next_token
    assert_equal true, tok.has_next?
    assert_equal "\n", tok.next_token[0].to_s
    assert_equal false, tok.has_next?
  end

Happy hacking,

Aureliano.

(Via aurelianito.) Original Link: Pequeñas delicias de las expresiones regulares


RubyInstaller: designs and deadline details

Posted: June 13th, 2009 | Author: FreedomCoder | Filed under: Open Source, Programming, Windows | Tags: , , , | No Comments »

So, guess what? There are already 3 designers running for the pledgie prize collected for One-Click Installer new home!

Check those over there:

http://wiki.github.com/oneclick/rubyinstaller/website-contest

This is excellent news. Thank you guys for participating!

Deadline

I’m aiming to close submissions June 19, 2009. So if you have a design or idea, mock it up and add to the wiki page, point to a blog post or something and let the public know you’re participating!

Logo

As Pavel mentioned over the wiki page:

I like it, but I was not sure about the shape of ruby gem.

This is something weird since Ruby users are used to see Ruby logo with the shape of a diamond, which is incorrect.

Let me explain it better: the mineral ruby can be cut in diamond shape, but is really uncommon.

More common shapes are facetted balls or emerald cuts. Take a look here for more examples.

Almost every package, tool or library associated with Ruby tends to use a diamond cut ruby as logo.

When analyzed this detail with Rodolfo Budeguer from Estudio Domo, we found that sticking to that shape would not only fall into the repetition (and be a copycat), but also move away from the goal of it.

If you pay close attention, will noticed that the emerald cut of RubyInstaller shows the 4 colors that are also present in… the Windows logo.

Why is that? Well, because is Ruby Installer for Windows ;)

Voting and Deliverables

Once again, would like to mention public voting would define the selected design. No new design will be accepted after the deadline above mentioned. So make room to do the mockups soon!

Also, once we have the final votes for the designs. Selected designer should provide generated versions of mockups in CSS and HTML content, preferable in a public Git repository.

I’m so excited about this that I’m blogging at 4am! :D

Thanks again to Silviu, Francesco and Pavel to contribute and participate. Also other people that privately sent some feedback about RubyInstaller itself, not just the website ;)

(Via DEV_MEM.dump_to(:blog) – Multimedia systems blog.) Original Link: RubyInstaller: designs and deadline details


Google Scrap con Rails

Posted: June 13th, 2009 | Author: FreedomCoder | Filed under: Open Source, Programming, Rails | Tags: , , , , | No Comments »

web_search_plugin es un nuevo plugin de Rails que hice, para poder obtener los resultados de una determinada consulta en google y mostrarla en tu sitio. Hasta el momento solo realiza búsquedas en Google, a futuro lo voy a integrar con Yahoo y Live.

Instalar web_search_plugin

# cd myapprails
# ruby script/plugin install git@github.com:chebyte/web_search_plugin.git

Uso
Como ejemplo, tenemos el modelo #link.rb

 class Link < ActiveRecord::Base
   include Chebyte::WebSearch
   web_search
 end

Ahora para obtener los resultados solo debemos llamar al método search_web

 Link.google_search(:query => "tuquito")

esto nos devolveria

=> #<Google::Response:0xb734d3cc @results=[{:domain=>"www.tuquito.org.ar", :content=>"Proyecto Linux de Tucumán, datos y temas relacionados al proyecto, foros y ayuda para usuarios de la distribución y descargas.", :title=>"Tuquito 3", :cache_url=>"http://www.google.com/search?q=cache:4NZXKc3gQA8J:www.tuquito.org.ar", :url=>"http://www.tuquito.org.ar/"}, {:domain=>"en.wikipedia.org", :content=>"Oct 11, 2008 ... Tuquito is a Debian-based operating system created in Tucumán, Argentina, by Ignacio Díaz, Chris Arenas and Mauro Torres, students of The ...", :title=>"Tuquito - Wikipedia, the free encyclopedia", :cache_url=>"http://www.google.com/search?q=cache:a1xAEvHHujUJ:en.wikipedia.org", :url=>"http://en.wikipedia.org/wiki/Tuquito"}, {:domain=>"www.slideshare.net", :content=>"Tuquito 3 Nuevo Diseño En varios idiomas (Ingles,Portugues y Español) Interfaces mas intuitivas Optimizado para la conectividad(wifi, ...", :title=>"Tuquito 3", :cache_url=>"http://www.google.com/search?q=cache:T8VfO-4iyioJ:www.slideshare.net", :url=>"http://www.slideshare.net/chebyte/tuquito-3"}, {:domain=>"www.olpcnews.com", :content=>"OLPC Tuquito's team began to work at 1st January of 2007, with the knowledge acquisition about the project One Laptop Per Child and then with development ...", :title=>"OLPC Tuquito Project Progress in Argentina - OLPC News", :cache_url=>"http://www.google.com/search?q=cache:eoGfDNbaZ8kJ:www.olpcnews.com", :url=>"http://www.olpcnews.com/countries/argentina/olpc_tuquito_project_argentina.html"}], @status=200, @size=4, @query="tuquito">

El resultado es un simple objecto hash, algunos de los campos disponibles son
* title titulo del resultado
* url Url del resultado
* domain Root url del resultado
* content contenido
* cache_url Google cache url

(Via Chebyte’s Blog.) Original Link: Google Scrap con Rails


Syntax highlighting de Ruby en mi blog

Posted: June 11th, 2009 | Author: FreedomCoder | Filed under: Open Source, Programming | Tags: , , , , | No Comments »

Tomando como base lo que dice en este post, hice un script en ruby que genera html con clases que se pueden poner coloritos con css:

#!/usr/bin/env ruby

require 'rubygems'
require 'syntax/convertors/html'

class Syntax::Convertors::HTML
  def convert( text, klass="" )
    html = "<pre class=\"#{klass}\">"
    regions = []
    @tokenizer.tokenize( text ) do |tok|
      value = html_escape(tok)
      case tok.instruction
        when :region_close then
          regions.pop
          html << "</span>"
        when :region_open then
          regions.push tok.group
          html << "<span class=\"#{tok.group}\">#{value}"
        else
          if tok.group == ( regions.last || :normal )
            html << value
          else
            html << "<span class=\"#{tok.group}\">#{value}</span>"
          end
      end
    end
    html << "</span>" while regions.pop
    html << "</pre>"
    html
  end
end

convertor = Syntax::Convertors::HTML.for_syntax "ruby"
puts convertor.convert( $stdin.read, "ruby"  )

En el script monkeypatchié un toque para que agregue la clase “ruby” al tag pre que engloba todo el código y aparte estoy usando estos estilos:

<style>
pre.ruby {
      background-color: #ffffcc;
      color: #000000;
      padding: 10px;
      font-size: 1.1em;
      overflow: auto;
      margin: 4px 0px;
      width: 95%;
      border: thin dashed;
}
.ruby .normal {}
.ruby .comment { color: #005; font-style: italic; }
.ruby .keyword { color: #A00; font-weight: bold; }
.ruby .method { color: #077; }
.ruby .class { color: #074; }
.ruby .module { color: #050; }
.ruby .punct { color: #447; font-weight: bold; }
.ruby .symbol { color: #099; }
.ruby .string { color: #944; }
.ruby .char { color: #F07; }
.ruby .ident { color: #004; }
.ruby .constant { color: #07F; }
.ruby .regex { color: #B66; }
.ruby .number { color: #D55; }
.ruby .attribute { color: #377; }
.ruby .global { color: #3B7; }
.ruby .expr { color: #227; }
</style>

Una cosa más, si estás viendo este post en otro lugar que no sea aurelianito.blogspot.com no vas a ver el resaltado de sintaxis (ya que no va a tener los estilos).

Hasta la próxima,
Aureliano

(Via aurelianito.) Original Link: Syntax highlighting de Ruby en mi blog


Configurar passenger con nginx

Posted: June 7th, 2009 | Author: FreedomCoder | Filed under: Open Source, Programming, linux | Tags: , , , | 1 Comment »

Instalar Ruby Enterprise Edition

sudo apt-get install build-essential zlib1g-dev libssl-dev libreadline5-dev
wget http://rubyforge.org/frs/download.php/51100/ruby-enterprise-1.8.6-20090201.tar.gz
tar xvfz ruby-enterprise-1.8.6-20090201.tar.gz
rm ruby-enterprise-1.8.6-20090201.tar.gz
cd ruby-enterprise-1.8.6-20090201/
sudo ./installer

Agregar ruby entrerprise al path del sistema

echo "export PATH=/opt/ruby-enterprise-1.8.6-20090201/bin:$PATH" >> ~/.profile && . ~/.profile

Nginx

sudo /opt/ruby-enterprise-1.8.6-20090201/bin/passenger-install-nginx-module

Elegir la opcion 1. Yes: download, compile and install Nginx for me. (recommended)

Script de inicio Nginx

Agregar el siguiente codigo en

/etc/init.d/nginx
sudo chown root:root /etc/init.d/nginx

Probar una aplicacion rails en nginx
agregar un virtual host

server {
    listen 80;
    # server_name www.mycook.com;
    root /home/deploy/testapp/public;
    passenger_enabled on;
}

(Via Chebyte’s Blog.) Original Link: Configurar passenger con nginx


Tokenizer de rapidito

Posted: June 7th, 2009 | Author: FreedomCoder | Filed under: Open Source, Programming | Tags: , , | No Comments »

Después de mirar un rato el estado de las bibliotecas para hacer wikis en Ruby, y descubrir que ninguna me servía, Decidí que tenía que poner cartas en el asunto y hacer la mía. Mi idea es implementar el markup de Trac, haciéndolo extensible, y agregarle un par de cositas que por ahora son un secreto :-p.

Lo importante del asunto es que puse manos en el asunto. Al final, después de un intento fallido de hackear mi camino al andar, decidí que lo mejor es armar un tokenizer y un parser que use esos tokens para generar el árbol del que extraeré el HTML.

Así que me puse a programar. Como no encontré ningún tokenizer en ruby, programé uno. El tokenizer se contruye con un montón de expresiones regulares que definen cada delimitador. Después se le setea una fuente de caracteres (un string) y separa el string en los delimitadores de arriba (que se devuelven como símbolos) y cadenas que no matchean con ninguno de los delimitadores (que devuelve como strings).

Bueno, basta de cháchara, acá tá el código:

module Rapidito
  class Tokenizer

    def initialize( *delimiters )
      @regexp = Regexp.union( *delimiters  + [/$/] )
    end

    attr_accessor :source

    def has_next?
      ! @source.empty?
    end

    def next_token
      p = (@source =~ @regexp)
      if p == 0 #delimiter
        token = nil
        @source.sub!( @regexp ) { |match| token=match.to_sym; "" }
        token
      else #text
        token = @source[0,p]
        @source = @source[p,@source.length]
        token
      end
    end

    def all_tokens
      tokens = []
      while has_next?
        tokens << next_token
      end
      tokens
    end

  end
end

Y, acá abajo la única documentación que hice hasta ahora, o sea los tests de unidad:

require 'test/unit'
require 'rapidito/tokenizer'

include Rapidito

class TokenizerTest < Test::Unit::TestCase

  def test_no_token
    tok = Tokenizer.new
    tok.source = "aaaa"
    assert_equal true, tok.has_next?
    assert_equal "aaaa", tok.next_token
    assert_equal false, tok.has_next?
  end

  def test_two_delimiters
    tok = Tokenizer.new( /\|/, /;;/ )
    tok.source = "aa|bbb;;;;cccc"
    assert_equal [ "aa", :"|", "bbb", :";;", :";;", "cccc" ], tok.all_tokens

    tok.source = "aa;;bbb||cccc"
    assert_equal [ "aa", :";;", "bbb", :"|", :"|", "cccc" ], tok.all_tokens
  end

  def test_choose_first_match
    tok = Tokenizer.new( /aa/, /aaa/ )
    tok.source = "aaa"
    assert_equal [ :aa, "a" ], tok.all_tokens
  end

end

Happy hacking,
Aureliano.

PD: ¿Prefieren que ponga el código con syntax highlighting?

(Via aurelianito.) Original Link: Tokenizer de rapidito